0% found this document useful (0 votes)
27 views15 pages

SJR October

The document discusses five assignments related to numerical analysis techniques: calculating error, Newton's forward and backward interpolation, Lagrange interpolation, and Simpson's 1/3 rule. Algorithms and C code implementations are provided for each assignment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views15 pages

SJR October

The document discusses five assignments related to numerical analysis techniques: calculating error, Newton's forward and backward interpolation, Lagrange interpolation, and Simpson's 1/3 rule. Algorithms and C code implementations are provided for each assignment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Assignment 1: Write a program to calculate the Absolute and Relative Error

Algorithm:

Begin
Create two variables true_val and measured_val and take data from the user
set abs_err to the unsigned difference of true_val and measured_val
set rel_err to the ratio of abs_err over tru_val
print absolute error as abs_err and relative error as rel_err
End

Implementation in C:

Code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {

double true_val, measured_val;

printf("Enter the true value: \n");


scanf("%lf", &true_val); // true value

printf("Enter the measured value: \n");


scanf("%lf", &measured_val); // measured value

// the difference in absolute


double abs_err = fabs(true_val - measured_val);
// relative error
double rel_err = abs_err / true_val;
// relative error in percentage
double rel_age = rel_err * 100;

printf("Absolute error: %.2lf\n", abs_err);


printf("Relative error: %.2lf\n", rel_err);
printf("Relative error percentage: %.2lf\n", rel_age);

}
Output

Set 2:

Enter the true value:


400
Enter the measured value:
345
Absolute error: 55.00
Relative error: 0.14
Relative error percentage: 13.75

Set 2:

Enter the true value:


376
Enter the measured value:
328
Absolute error: 48.00
Relative error: 0.13
Relative error percentage: 12.77
Assignment 2: Write a program to implement Newton’s Forward Interpolation method

Algorithm:

Begin
Declare integers N, I, J, CH
Set CH:= 30
Declare float: ARR[10][11], PX, X, Y, P, H
Set PX:= 1
Input no of data and store in N

for I=0 to less than N do


Input value and store in ARR[I][0]
set I:= I+1

end for

for I=0 to less than N do


Input value and store in ARR[I][1]
Set I:= I+1

end for

[Forming the difference table]


for J=2 to N do
for I=0 to less than N-1 do
Set ARR[I][J]:= ARR[I+1][J-1]-ARR[I][J-1]
end for
end for
Print the difference table
Input value of X for f(X) and store in X

[Calculating f(X) for X]


Set H:= ARR[1][0]-ARR[0][0]
Set P:= (X-ARR[0][0])/H
Set Y:= ARR[0][1]
for I=1 to less than N do
Set PX:= PX*(P-(I-1))
Set Y:= Y+(ARR[0][I+1])*PX)/fact(I)

end for
Print value of f(X) stored in variable Y
End
Implementation in C:

Code

#include<stdio.h>
#include<math.h>
int fact(int);
main()
{
float arr[10][11],x,h,p,y,px=1;
int i,j,n,ch=30;
printf("\nEnter the number of data:");
scanf("%d",&n);
printf("\nEnter the values of x:\n");
for(i=0;i<n;i++)
{
scanf("\n%f",&arr[i][0]);
}
printf("\nEnter the values of y:\n");
for(i=0;i<n;i++)
{
scanf("\n%f",&arr[i][1]);
}
//Forming difference table.
for(j=2;j<=n;j++)
for(i=0;i<n-1;i++)
arr[i][j]=arr[i+1][j-1]-arr[i][j-1];
//Printing table
printf("\nDifference table is:-");
printf("\n\tX\tY");
for(i=0;i<=n-2;i++)
printf("\t%c^%dY",ch,i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++)
{
printf("\t%.2f",arr[i][j]);
}
}
//Take the value of x for f(x)
printf("\nEnter the value x for function f(x):");
scanf("%f",&x);
//Calculate the value of f(x) for x
h=arr[1][0]-arr[0][0];
p=(x-arr[0][0])/h;
y=arr[0][1];
for(i=1;i<n;i++)
{
px=px*(p-(i-1));
y=y+(arr[0][i+1]*px)/fact(i);
}
printf("\nthe value of function at x=%f is :%f",x,y);
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}

Output

Enter the number of data:4

Enter the values of x:


35
45
50
55

Enter the values of y:


0.6900
0.6810
0.6711
0.6623

Difference table is:-


X Y ^1Y ^2Y ^3Y
35.00 0.69 -0.01 -0.00 0.00
45.00 0.68 -0.01 0.00
50.00 0.67 -0.01
55.00 0.66
Enter the value x for function f(x):43

the value of function at x=43.000000 is :0.682936


Assignment 3: Write a program to implement Newton’s Backward Interpolation method

Algorithm:

Begin
Declare integers N, I, J, CH
Set CH:= 30
Declare float: ARR[10][11], PX, X, Y, P, H
Set PX:= 1
Input no of data and store in N

for I=0 to less than N do


Input value and store in ARR[I][0]
set I:= I+1

end for

for I=0 to less than N do


Input value and store in ARR[I][1]
Set I:= I+1

end for

[Forming the difference table]


for J=2 to N do
for I=0 to less than N-1 do
Set ARR[I][J]:= ARR[I+1][J-1]-ARR[I][J-1]
end for
end for
Print the difference table
Input value of X for f(X) and store in X

[Calculating f(X) for X]


Set H:= ARR[N-1][0]-ARR[N-2][0]
Set P:= (X-ARR[N-1][0])/H
Set Y:= ARR[N-1][1]
for I=1 to less than N do
Set PX:= PX*(P+(I-1))
Set Y:= Y+(ARR[N-I-1][I+1])*PX)/fact(I)
end for
Print value of f(X) stored in variable Y
End
Implementation in C:

Code

#include<stdio.h>
int fact(int);
main()
{
int n,i,j,ch=30,k=1,w;
float arr[10][11],px=1,x,y,p,h;
printf("\nEnter the no of data:");
scanf("%d",&n);
w=n-1;
printf("\nEnter the values of x:\n");
for(i=0;i<n;i++)
{
scanf("\n%f",&arr[i][0]);
}
printf("\nEnter the values of y:\n");
for(i=0;i<n;i++)
{
scanf("\n%f",&arr[i][1]);
}
//forming the difference table
for(j=2;j<=n;j++)
{
for(i=0;i<n-1;i++)
arr[i][j]=arr[i+1][j-1]-arr[i][j-1];
}
//printing table
printf("\nThe difference table is:\n");
printf("\tX \tY");
for(i=0;i<n-1;i++)
printf("\t%c^%d",ch,i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++)
printf("\t%.2f",arr[i][j]);
}
printf("\nEnter the value of x for f(x):");
scanf("%f",&x);
//calculate the value of f(x) for x
h=arr[n-1][0]-arr[n-2][0];
p=(x-arr[n-1][0])/h;
y=arr[n-1][1];
for(i=1;i<n;i++)
{
px=px*(p+(i-1));
y=y+(arr[n-1-i][i+1]*px)/fact(i);
}
printf("\nThe value of f(x) at x=%f is :%f",x,y);
}
int fact(int n)
{
int f=1,i;
for(i=1;i<=n;i++)
f=f*i;
return f;
}

Output

Set 1:

Enter the no of data:4


Enter the values of x:
0
1
2
3
Enter the values of y:
1
2
11
34
The difference table is:
X Y ▲^1 ▲^2 ▲^3
0.00 1.00 1.00 8.00 6.00
1.00 2.00 9.00 14.00
2.00 11.00 23.00
3.00 34.00
Enter the value of x for f(x):0.5
The value of f(x) at x=0.500000 is :0.875000

Set 2:

Enter the no of data:5


Enter the values of x:
0
10
20
30
40
Enter the values of y:
1
0.98
0.93
0.86
0.76
The difference table is:
X Y ▲^1 ▲^2 ▲^3 ▲^4
0.00 1.00 -0.02 -0.03 0.01 -0.02
10.00 0.98 -0.05 -0.02 -0.01
20.00 0.93 -0.07 -0.03
30.00 0.86 -0.10
40.00 0.76
Enter the value of x for f(x):45
The value of f(x) at x=45.000000 is: 0.690156
Assignment 4: Write a program to implement Lagrange’s Interpolation method

Algorithm:

Begin
Declare integers I, J, SIZE
Set K=1, N = 0
Input the size of the dataset
Input the corresponding Xi and Yi values
Input the value to be interpolated and set to X
[Interpolation]
for I:=0 to less than SIZE do
set num = deno = 1
for J:=0 to less than SIZE do
if J is not equal to I then
set num = num * (X * Xj)
set deno = Xi - Xj
end of if
end of for
set N = N + ((num / deno) * Yi)
end of for
Print interpolated value
End

Implementation in C:

Code

#include<stdio.h>
int main()
{
float x[10],y[10],num=1,deno=1,n=0,p;
int size;
int i,j,k=1;
printf("Enter the number of terms:\n");
scanf("%d",&size);
printf("Enter the values of x and y\n");
for(i=0;i<size;i++)
{
scanf("%f",&x[i]);
scanf("%f",&y[i]);
}
printf("TABLE");
for(i=0;i<size;i++)
{
printf("%0.3f\t%0.3f",x[i],y[i]);
printf("\n");
}
while(k==1)
{
printf("Enter value of x to be interpolated: ");
scanf("%f",&p);
for(i=0;i<size;i++)
{
num=deno=1;
for(j=0;j<size;j++)
{
if(j!=i)
{
num*=p-x[j];
deno*=x[i]-x[j];
}
}
n=n+((num/deno)*y[i]);
}
printf("Interpolated value: %0.3f",n);
}
}

Output

Enter the number of terms:


5
Enter the values of x and y
5 150
7 392
11 1452
13 2366
17 5202
TABLE
5.000 150.000
7.000 392.000
11.000 1452.000
13.000 2366.000
17.000 5202.000
Enter value of x to be interpolated: 9
Interpolated value: 810.000
Assignment 5: Write a program to implement Simpson’s ⅓ rule

Algorithm:

Begin
Input function f(x);
Read a,b,n; // the lower and upper limits and number of sub-intervals
Set h=(b-a)/n;
Set Sum = [f(a)-f(a+nh)];
for i=1 to n-1 step 2 do
Set sum = sum + 4*f(a+ih)+2*f(a+(i+1)h);
end for
Set result = sum * h/3;
Print result;
End

Implementation in C:

Code

#include <stdio.h>

double f(double x) {
return x*x;
}

int main() {
double a, b; int n;
printf("Enter the lower and upper limits: ");
scanf("%lf %lf",&a,&b);
printf("Enter number of intervals: ");
scanf("%d", &n);
double h = (b - a)/n;
double x = a + h;
double odd_sum = 0.0;
double even_sum = 0.0;
printf("Table:\n");
printf("X F(X)\n");
printf("%lf %lf\n", a, f(a));
for(int i=1; x<b; x+=h, i++) {
printf("%lf %lf\n", x, f(x));
if (i%2 == 1)
odd_sum+=f(x);
else
even_sum+=f(x);
}
printf("%lf %lf\n", b, f(b));
double res = h/3.0 * (2.0*even_sum + 4.0*odd_sum + f(a) +
f(b));
printf("Result of Integration by using Simpson's One-Third
Rule: %lf\n", res);
return 0;
}

Output

Enter the lower and upper limits: 0


12
Enter number of intervals: 8
Table:
X F(X)
0.000000 0.000000
1.500000 2.250000
3.000000 9.000000
4.500000 20.250000
6.000000 36.000000
7.500000 56.250000
9.000000 81.000000
10.500000 110.250000
12.000000 144.000000
Result of Integration by using Simpson's One-Third Rule:
576.000000

Assignment 5: Write a program to implement Simpson’s ⅓ rule

Algorithm:

Begin

Step 2. Define function f(x)


Step 3. Read lower limit of integration, upper limit of integration and number of sub
interval
Step 4. Calculate: 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

End
Implementation in C:

Code

#include <stdio.h>
#include <math.h>

double f(double x) {
return x*x;
}

int main() {
double a, b; int n;
printf("Enter the lower and upper limits: ");
scanf("%lf %lf",&a,&b);
printf("Enter number of intervals: ");
scanf("%d", &n);
double h = (b - a)/n;
double x = a + h;
double sum = 0.0;
printf("Table:\n");
printf("X F(X)\n");
printf("%lf %lf\n",a, f(a));
for(; x<b; x+=h) {
printf("%lf %lf\n",x,f(x));
sum+=f(x);
}
printf("%lf %lf\n",b, f(b));
sum = h/2.0 * (2.0*sum + f(a) + f(b));
printf("Result: %lf\n", sum);
return 0;
}

Output

Enter the lower and upper limits: 0


12
Enter number of intervals: 20
Table:
X F(X)
0.000000 0.000000
0.600000 0.360000
1.200000 1.440000
1.800000 3.240000
2.400000 5.760000
3.000000 9.000000
3.600000 12.960000
4.200000 17.640000
4.800000 23.040000
5.400000 29.160000
6.000000 36.000000
6.600000 43.560000
7.200000 51.840000
7.800000 60.840000
8.400000 70.560000
9.000000 81.000000
9.600000 92.160000
10.200000 104.040000
10.800000 116.640000
11.400000 129.960000
12.000000 144.000000
12.000000 144.000000
Result: 663.120000

You might also like