EX.
NO: 1
LAGRANGE INTERPOLATION
DATE:
Aim:
Write a C program Lagrange Interpolation.
Algorithm:
Step 1: Start
Step 2: Read number of data (n)
Step 3: Read data Xi and Yi for i=1 ton n
Step 4: Read value of independent variables say xp
whose corresponding value of dependent say yp is to be determined.
Step 5: Initialize: yp = 0
Step 6: For i = 1 to n
Set p = 1
For j =1 to n
If i ≠ j then
Calculate p = p * (xp - Xj)/(Xi - Xj)
End If
Next j
Calculate yp = yp + p * Yi
Next i
Step 6: Display value of yp as interpolated value.
Step 7: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float x[100], y[100], xp, yp=0, p;
int i,j,n;
clrscr();
/* Input Section */
printf("Enter number of data: ");
scanf("%d", &n);
printf("Enter data:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = ", i);
scanf("%f", &x[i]);
printf("y[%d] = ", i);
scanf("%f", &y[i]);
}
printf("Enter interpolation point: ");
scanf("%f", &xp);
/* Implementing Lagrange Interpolation */
for(i=1;i<=n;i++)
{
p=1;
for(j=1;j<=n;j++)
{
if(i!=j)
{
p = p* (xp - x[j])/(x[i] - x[j]);
}
}
yp = yp + p * y[i];
}
printf("Interpolated value at %.3f is %.3f.", xp, yp);
getch();
}
OUTPUT:
Enter number of data: 5 ↲
x[1] = 5 ↲
Enter data:
y[1] = 150 ↲
x[2] = 7 ↲
y[2] = 392 ↲
x[3] = 11 ↲
y[3] = 1452 ↲
x[4] = 13 ↲
y[4] = 2366 ↲
x[5] = 17 ↲
y[5] = 5202 ↲
Enter interpolation point: 9 ↲
Interpolated value at 9.000 is 810.000.
Note: ↲ indicates ENTER is pressed.
RESULT:
Thus the Lagrange Interpolation program is Executed Successfully.
EX.NO: 2
NEWTON’S FORWARD INTERPOLATION
DATE:
Aim :
Write a C program Newton’s Forward Interpolation.
Algorithm:
Step 1: Start the program
Step 2: Read n (No. of arguments)
Step 3: For i = 0 to n − 1
Read x i &y i [0]
End i
Step 4: Construct the Forward Difference Table
For j = 1 to n − 1
For i = 0 to n − 1 − j
y i [j] = y[i + 1][j − 1] − y[i][j − 1]
End i
End j
Step 5: Print the Forward Difference Table
For i = 0 to n − 1
For j = 0 to n − 1 − i
Print y i [j]
End j
Next Line
End i
Step 6: Read a (Point of Interpolation)
Step 7: Assign h = x[1] − x[0] (Step Length)
Step 8: Assign u = (a − x[0])/h
Step 9: Assign sum = y 0 [0] & p = 1.0
Step 10: For j = 1 to n − 1
p = p ∗ (u − j + 1)/j
sum = sum + p ∗ y 0 [j]
End j
Step 11: Display a & sum
Step 12: Stop
Program :
// Newton's Interpolation
#include <stdio.h>
void forward(float x[4], float y[4][4], int n);
int main()
{
int i, j;
int n = 4; // number of arguments
float x[4] = { 0, 1, 2, 3 };
float y[4][4] = {
{ 1, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 10, 0, 0, 0 },
};
forward(x, y, n);
return 0;
}
void forward(float x[4], float y[4][4], int n)
{
int i, j;
float a = 0.5; // interpolation point
float h, u, sum, p;
for (j = 1; j < n; j++) {
for (i = 0; i < n - j; i++) {
y[i][j] = y[i + 1][j - 1] - y[i][j - 1];
}
}
printf("\n The forward difference table is:\n");
for (i = 0; i < n; i++) {
printf("\n");
for (j = 0; j < n - i; j++) {
printf("%f\t", y[i][j]);
}
}
p = 1.0;
sum = y[0][0];
h = x[1] - x[0];
u = (a - x[0]) / h;
for (j = 1; j < n; j++) {
p = p * (u - j + 1) / j;
sum = sum + p * y[0][j];
}
printf("\nThe value of y at x=%0.1f is %0.3f", a, sum);
}
OUTPUT:
The forward difference table is:
1.000000 -1.000000 2.000000 6.000000
0.000000 1.000000 8.000000
1.000000 9.000000
10.000000
The value of y at x=0.5 is 0.625
RESULT:
Thus the Newton’s Forward Interpolation program is Executed Successfully.
EX.NO: 3
NEWTON’S BACKWARD INTERPOLATION
DATE:
Aim :
Write a C program Newton’s Backward Interpolation.
Algorithm:
Step 1: Start the program
Step 2: Read n (No. of arguments)
Step 3: For i = 0 to n − 1
Read x i &y i [0]
End i
Step 4: Construct the Backward Difference Table
For j = 1 to n − 1
For i = j to n − 1
y i [j] = y[i][j − 1] − y[i − 1][j − 1]
End i
End j
Step 5: Print the Backward Difference Table
For i = 0 to n − 1
For j = 0 to i
Print y i [j]
End j
End i
Step 6: Read a (Point of Interpolation)
Step 7: Assign h = x[1] − x[0] (Step Length)
Step 8: Assign u = (a − x[n − 1])/h
Step 9: Assign sum = y n − 1 [0] & p = 1.0
p = p ∗ (u + j − 1)/j
Step 10: For j = 1 to n − 1
sum = sum + p ∗ y n − 1 [j]
End j
Step 11: Display a & sum
Step 12: Stop
Program :
#include <stdio.h>
void BackWard(float x[4], float y[4][4], int n);
int main()
{
int i, j;
int n = 4; // number of arguments
float x[4] = { 0, 1, 2, 3 };
float y[4][4] = {
{ 1, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 10, 0, 0, 0 },
};
BackWard(x, y, n);
return 0;
}
void BackWard(float x[4], float y[4][4], int n)
{
int i, j;
float a = 0.5; // interpolation point
float h, u, sum, p;
for (j = 1; j < n; j++) {
for (i = j; i < n; i++) {
y[i][j] = y[i][j - 1] - y[i - 1][j - 1];
}
}
printf("\nThe backward difference table is:\n");
for (i = 0; i < n; i++) {
printf("\n");
for (j = 0; j <= i; j++) {
printf("%f\t", y[i][j]);
}
}
p = 1.0;
sum = y[n - 1][0];
h = x[1] - x[0];
u = (a - x[n - 1]) / h;
for (j = 1; j < n; j++) {
p = p * (u + j - 1) / j;
sum = sum + p * y[n - 1][j];
}
printf("\nThe value of y at x=%0.1f is %0.3f", a, sum);
}
OUTPUT:
The backward difference table is:
1.000000
0.000000 -1.000000
1.000000 1.000000 2.000000
10.000000 9.000000 8.000000 6.000000
The value of y at x=0.5 is 0.625
RESULT:
Thus the Newton’s Backward Interpolation program is Executed Successfully.
EX.NO: 4
DATE: CURVE FITTING :LEAST SQUARES FITTING
Aim :
Write a C program Least Squares fitting.
Algorithm:
Step 1: Start the program.
Step 2: Read Number of Data (n)
Step 3: For i=1 to n:
Read Xi and Yi
Next i
Step 4: Initialize:
sumX = 0
sumX2 = 0
sumY = 0
sumXY = 0
Step 5: Calculate Required Sum
For i=1 to n:
sumX = sumX + Xi
sumX2 = sumX2 + Xi * Xi
sumY = sumY + Yi
sumXY = sumXY + Xi * Yi
Next i
Step 6:. Calculate Required Constant a and b of y = a + bx:
b = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX)
a = (sumY - b*sumX)/n
Step 7: Display value of a and b
Step 8: Stop the program
Program :
#include<stdio.h>
#define S 50
int main()
{
int n, i;
float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0, a, b;
/* Input */
printf("How many data points?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i=1;i<=n;i++)
{
printf("x[%d]=",i);
scanf("%f", &x[i]);
printf("y[%d]=",i);
scanf("%f", &y[i]);
}
/* Calculating Required Sum */
for(i=1;i<=n;i++)
{
sumX = sumX + x[i];
sumX2 = sumX2 + x[i]*x[i];
sumY = sumY + y[i];
sumXY = sumXY + x[i]*y[i];
}
/* Calculating a and b */
b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
a = (sumY - b*sumX)/n;
/* Displaying value of a and b */
printf("Values are: a=%0.2f and b = %0.2f",a,b);
printf("\nEquation of best fit is: y = %0.2f + %0.2fx",a,b);
return(0);
}
OUTPUT:
How many data points?
4
Enter data:
x[1] = 0↲
y[1] = -1↲
x[2] = 2↲
y[2] = 5↲
x[3] = 5↲
y[3] = 12↲
x[4] = 7↲
y[4] = 20↲
Values are: a=-1.14 and b=2.90
Equation of best fit is: y = -1.14 + 2.90x
Note: ↲ indicates ENTER is pressed.
RESULT:
Thus the Least Squares fitting program is Executed Successfully.
EX.NO: 5
NUMERICAL INTEGRATION BY THE TRAPEZOIDAL
DATE: RULE.
Aim :
Write a C program Numerical Integration by the Trapezoidal rule.
Algorithm:
Step 1: Start
Step 2: Define function f(x)
Step 3: Read lower limit of integration, upper limit of
integration and number of sub interval
Step 4: Calcultae: step size = (upper limit - lower limit)/number of sub interval
Step 5: Set: integration value = f(lower limit) + f(upper limit)
Step 6: Set: i = 1
Step 7: If i > number of sub interval then goto
Step 8: Calculate: k = lower limit + i * h
Step 9: Calculate: Integration value = Integration Value + 2* f(k)
Step 10: Increment i by 1 i.e. i = i+1 and go to step 7
Step 11: Calculate: Integration value = Integration value * step size/2
Step 12: Display Integration value as required answer
Step 13: Stop the program
Program :
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+pow(x,2))
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
integration = integration + 2 * f(k);
}
integration = integration * stepSize/2;
printf("\nRequired value of integration is: %.3f", integration);
getch();
return 0;
}
OUTPUT:
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6
Required value of integration is: 0.784
RESULT:
Thus the Numerical Integration by the Trapezoidal rule program is Executed
Successfully.
EX.NO: 6 NUMERICAL INTEGRATION BY THE SIMPSON’S RULE.
DATE:
Aim :
Write a C program Numerical Integration by the Simpson’s rule.
Algorithm:
Step 1: Start the program.
Step 2: Define function f(x)
Step 3: Read lower limit of integration, upper limit of
integration and number of sub interval
Step 4: Calcultae: step size = (upper limit - lower limit)/number of sub interval
Step 5: Set: integration value = f(lower limit) + f(upper limit)
Step 6: Set: i = 1
Step 7: If i > number of sub interval then goto
Step 8: Calculate: k = lower limit + i * h
Step 9: If i mod 2 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 4 * f(k)
End If
Step 10: Increment i by 1 i.e. i = i+1 and go to step 7
Step 11: Calculate: Integration value = Integration value * step size/3
Step 12: Display Integration value as required answer
Step 13: Stop the program.
Program :
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%2==0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 4 * f(k);
}
}
integration = integration * stepSize/3;
printf("\nRequired value of integration is: %.3f", integration);
getch();
return 0;
}
OUTPUT:
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6
Required value of integration is: 0.785
RESULT:
Thus the Numerical Integration by the Simpson’s rule program is Executed
Successfully.
EX.NO: 7 ORDINARY DIFFERENTIAL EQUATION BY THE EULER’S
DATE: METHOD
Aim :
Write a C program Ordinary differential equation by the Euler’s method.
Algorithm:
Step 1: Start
Step 2: Define function f(x,y)
Step 3: Read values of initial condition(x0 and y0),
number of steps (n) and calculation point (xn)
Step 4: Calculate step size (h) = (xn - x0)/b
Step 5: Set i=0
Step 6: Loop
yn = y0 + h * f(x0 + i*h, y0)
y0 = yn
i=i+1
While i < n
Step 7: Display yn as result
Step 8: Stop the program.
Program :
#include<stdio.h>
#include<conio.h>
#define f(x,y) x+y
int main()
{
float x0, y0, xn, h, yn, slope;
int i, n;
clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);
/* Calculating step size (h) */
h = (xn-x0)/n;
/* Euler's Method */
printf("\nx0\ty0\tslope\tyn\n");
printf("------------------------------\n");
for(i=0; i < n; i++)
{
slope = f(x0, y0);
yn = y0 + h * slope;
printf("%.4f\t%.4f\t%0.4f\t%.4f\n",x0,y0,slope,yn);
y0 = yn;
x0 = x0+h;
}
/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);
getch();
return 0;
}
OUTPUT:
Enter Initial Condition
x0 = 0
y0 = 1
Enter calculation point xn = 1
Enter number of steps: 10
x0 y0 slope yn
------------------------------
0.0000 1.0000 1.0000 1.1000
0.1000 1.1000 1.2000 1.2200
0.2000 1.2200 1.4200 1.3620
0.3000 1.3620 1.6620 1.5282
0.4000 1.5282 1.9282 1.7210
0.5000 1.7210 2.2210 1.9431
0.6000 1.9431 2.5431 2.1974
0.7000 2.1974 2.8974 2.4872
0.8000 2.4872 3.2872 2.8159
0.9000 2.8159 3.7159 3.1875
Value of y at x = 1.00 is 3.187
RESULT:
Thus the Ordinary differential equation by the Euler’s method program is
Executed Successfully.
EX.NO: 8 ORDINARY DIFFERENTIAL EQUATION BY THE RUNGE – KUTTA
DATE: METHOD
Aim :
Write a C program Ordinary differential equation by the Runge – Kutta method.
Algorithm:
Step 1: Start
Step 2: Define function f(x,y)
Step 3: Read values of initial condition(x0 and y0),
number of steps (n) and calculation point (xn)
Step 4: Calculate step size (h) = (xn - x0)/n
Step 5: Set i=0
Step 6: Loop
k1 = h * f(x0, y0)
k2 = h * f(x0+h/2, y0+k1/2)
k3 = h * f(x0+h/2, y0+k2/2)
k4 = h * f(x0+h, y0+k3)
k = (k1+2*k2+2*k3+k4)/6
yn = y0 + k
i=i+1
x0 = x0 + h
y0 = yn
While i < n
Step 7: Display yn as result
Step 8: Stop
Program:
#include<stdio.h>
#include<conio.h>
#define f(x,y) (y*y-x*x)/(y*y+x*x)
int main()
{
float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
int i, n;
clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);
/* Calculating step size (h) */
h = (xn-x0)/n;
/* Runge Kutta Method */
printf("\nx0\ty0\tyn\n");
for(i=0; i < n; i++)
{
k1 = h * (f(x0, y0));
k2 = h * (f((x0+h/2), (y0+k1/2)));
k3 = h * (f((x0+h/2), (y0+k2/2)));
k4 = h * (f((x0+h), (y0+k3)));
k = (k1+2*k2+2*k3+k4)/6;
yn = y0 + k;
printf("%0.4f\t%0.4f\t%0.4f\n",x0,y0,yn);
x0 = x0+h;
y0 = yn;
}
/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);
getch();
return 0;
}
Output:
Enter Initial Condition
x0 = 0
y0 = 1
Enter calculation point xn = 0.4
Enter number of steps: 2
x0 y0 yn
0.0000 1.0000 1.1960
0.2000 1.1960 1.3753
Value of y at x = 0.40 is 1.375
RESULT:
Thus the Ordinary differential equation by the Runge – Kutta program is
Executed Successfully.
EX.NO: 9
DATE: POLYNOMIAL-BISECTION METHOD
Aim :
Write a C program finding roots of a Polynomial-Bisection Method.
Algorithm:
Step 1: start
Step 2: Define function f(x)
Step 3: Choose initial guesses x0 and x1 such that f(x0)f(x1) < 0
Step 4: Choose pre-specified tolerable error e.
Step 5: Calculate new approximated root as x2 = (x0 + x1)/2
Step 6: Calculate f(x0)f(x2)
a. if f(x0)f(x2) < 0 then x0 = x0 and x1 = x2
b. if f(x0)f(x2) > 0 then x0 = x2 and x1 = x1
c. if f(x0)f(x2) = 0 then goto (8)
Step 7: if |f(x2)| > e then goto (5) otherwise goto (8)
Step 8: Display x2 as root.
Step 9: Stop
Program :
#include<stdio.h>
#include<conio.h>
#include<math.h>
/*
Defining equation to be solved.
Change this equation to solve another problem.
*/
#define f(x) cos(x) - x * exp(x)
void main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
clrscr();
/* Inputs */
up:
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
/* Calculating Functional Value */
f0 = f(x0);
f1 = f(x1);
/* Checking whether given guesses brackets the root or not. */
if( f0 * f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
/* Implementing Bisection Method */
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = (x0 + x1)/2;
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);
if( f0 * f2 < 0)
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
step = step + 1;
}while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
getch();
}
OUTPUT:
Enter two initial guesses:
0
1
Enter tolerable error:
0.0001
Step x0 x1 x2 f(x2)
1 0.000000 1.000000 0.500000 0.053222
2 0.500000 1.000000 0.750000 -0.856061
3 0.500000 0.750000 0.625000 -0.356691
4 0.500000 0.625000 0.562500 -0.141294
5 0.500000 0.562500 0.531250 -0.041512
6 0.500000 0.531250 0.515625 0.006475
7 0.515625 0.531250 0.523438 -0.017362
8 0.515625 0.523438 0.519531 -0.005404
9 0.515625 0.519531 0.517578 0.000545
10 0.517578 0.519531 0.518555 -0.002427
11 0.517578 0.518555 0.518066 -0.000940
12 0.517578 0.518066 0.517822 -0.000197
13 0.517578 0.517822 0.517700 0.000174
14 0.517700 0.517822 0.517761 -0.000012
Root is: 0.517761
RESULT:
Thus the Polynomial-Bisection Method program is Executed Successfully.
EX.NO: 10
DATE: POLYNOMIAL-NEWTON RAPHSON METHOD
Aim :
Write a C program finding roots of a Polynomial- Newton Raphson Method.
Algorithm:
Step 1: start
Step 2: Define function f(x)
Step 3: Define first derivative of f(x) as g(x)
Step 4: Input initial guess (x0), tolerable error (e)
and maximum iteration (N).
Step 5: Initialize iteration counter i = 1
Step 6: If g(x0) = 0 then print "Mathematical Error"
and goto (12) otherwise goto (7)
Step 7: Calcualte x1 = x0 - f(x0) / g(x0)
Step 8: Increment iteration counter i = i + 1
Step 9: If i >= N then print "Not Convergent"
and goto (12) otherwise goto (10)
Step 10: If |f(x1)| > e then set x0 = x1
and goto (6) otherwise goto (11)
Step 11: Print root as x1
Step 12: Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
/* Defining equation to be solved.
Change this equation to solve another problem. */
#define f(x) 3*x - cos(x) -1
/* Defining derivative of g(x).
As you change f(x), change this function also. */
#define g(x) 3 + sin(x)
void main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
clrscr();
/* Inputs */
printf("\nEnter initial guess:\n");
scanf("%f", &x0);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Enter maximum iteration:\n");
scanf("%d", &N);
/* Implementing Newton Raphson Method */
printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
{
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
{
printf("Mathematical Error.");
exit(0);
}
x1 = x0 - f0/g0;
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,x1,f1);
x0 = x1;
step = step+1;
if(step > N)
{
printf("Not Convergent.");
exit(0);
}
f1 = f(x1);
}while(fabs(f1)>e);
printf("\nRoot is: %f", x1);
getch();
}
OUTPUT:
Enter initial guess:
1
Enter tolerable error:
0.00001
Enter maximum iteration:
10
Step x0 f(x0) x1 f(x1)
1 1.000000 1.459698 0.620016 0.000000
2 0.620016 0.046179 0.607121 0.046179
3 0.607121 0.000068 0.607102 0.000068
Root is: 0.607102
RESULT:
Thus the Polynomial- Newton Raphson Method program is Executed Successfully.