Project 1
Project 1
#include<stdio.h>
#include<math.h>
int main( )
scanf("%f",&xt);
scanf("%f",&xa);
ea=xt-xa;
if(ea<0)
ea=-(ea);
er=ea/xt;
ep=er*100;
}
outPut
Absolute error=0.699400
Relative error=0.120815
Percentage error=12.081532%
Program no 2
Write a program of Bisection method?
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
int step = 1;
up:
scanf("%f", &e);
f0 = f(x0);
f1 = f(x1);
goto up;
}
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
x2 = (x0 + x1)/2;
f2 = f(x2);
if( f0 * f2 < 0)
x1 = x2;
f1 = f2;
else
x0 = x2;
f0 = f2;
step = step + 1;
while(fabs(f2)>e);
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
#include<math.h>
#define f(x)(x*x)-25
int main()
float x1,x2;
int i,n;
float f0,f1,f2,x0;
scanf(("%d",&n));
for(xo=1;x0=x0+0.50)
f0=f(x0);
if (f0<0)
break;
for(x1=1;x1=x1+0.50)
f1=f(x1);
if(f1>0)
break;
for(i=1;i<=n;i++)
x2=((x0*f1)-(x1*f0))/(f1-f0);
f2=f(x2);
if((f0*f2)>0)
x1=x2;
f1=f2;
else
x0=x2;
f0=f2;
if(fabs((f2)<eps)
break;
else
#include<stdio.h>
#include<math.h>
double f(double x)
return x*x;
main(){
int n,i;
double a,b,h,x,sum=0,integral;
scanf("%d",&n);
scanf("%lf",&a);
scanf("%lf",&b);
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
if(i%2==0){
sum=sum+2*f(x);
else{
sum=sum+4*f(x);
integral=(h/3)*(f(a)+f(b)+sum);
}
Output
#include<stdio.h>
#include<math.h>
double f(double x)
{
return x*x;
}
main(){
int n,i;
double a,b,h,x,sum=0,integral;
printf("\nEnter the no. of sub-intervals(MULTIPLE OF 3): ");
scanf("%d",&n);
printf("\nEnter the initial limit: ");
scanf("%lf",&a);
printf("\nEnter the final limit: ");
scanf("%lf",&b);
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
if(i%3==0){
sum=sum+2*f(x);
}
else{
sum=sum+3*f(x);
}
}
integral=(3*h/8)*(f(a)+f(b)+sum);
printf("\nThe integral is: %lf\n",integral);
}
Output
Enter the no of sub interval (multiple 3):81
Enter thhe initial limit :2
Enter the final limit:1
The interger is:6.296296
Program no :6
Write a program of trapezoidal rule?
#include<stdio.h>
#include<math.h>
double f(double x){
return x*x;
}
double trapezoidal(double f(double x), double a, double b, int
n){
double x,h,sum=0,integral;
int i;
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+f(x);
}
integral=(h/2)*(f(a)+f(b)+2*sum);
return integral;
}
main(){
int n,i=2;
double a,b,h,eps,sum=0,integral,integral_new;
printf("\nEnter the initial limit: ");
scanf("%lf",&a);
printf("\nEnter the final limit: ");
scanf("%lf",&b);
printf("\nEnter the desired accuracy: ");
scanf("%lf",&eps);
integral_new=trapezoidal(f,a,b,i);
do{
integral=integral_new;
i++;
integral_new=trapezoidal(f,a,b,i);
}while(fabs(integral_new-integral)>=eps);
printf("The integral is: %lf\n with %d
intervals",integral_new,i);
}
Output
#include<stdio.h>
#include<math.h>
#define f1(x) pow (x,4)-x-9
#define f2 $*pow(x,3)-1
void main()
float x0,x1;
int n,i;
printf("\n enter the limit of iteration:");
scanf("%d",&n);
printf("\n enter the first approx:");
scanf("%f",&x0);
for(i=0;i<n;i++)
x1=x0-(f1(x0))/(f2(x0));
printf("\n\troots are =%d",x0);
x0=x1;
output
enter vthe limit of iteration:7
enter the first approx.:1.7
roots are:1.825879
roots are:1.813520
roots are:1.815521
roots are:1.823335
roots are:1.813387
roots are:1.813387
Program no -9
Write a program to find newton interpolation?
#include<stdio.h>
#define mn 100
#define or 4
void main()
{
float ax[mn+1],diff[mn+1][or+1],nr=1,dr=1,x,p,h,ye;
int n,j,k;
printf(“enter the value of\n”);
scanf(“%d”,&n);
printf(“enter the value in the form x,y\n”);
for(i=0;i<n;i++)
scanf(“%f%f”,&a[i},a&[j];
printf(“enter the value in the of x for which the value mof
y is wanted\n”);
scanf(“%f”,&x);
h=ax[1]-ax[0];
for(i=0;i<=n-1;i++)
diff[i]=ay[i=1]-ay[i];
for(j=2;j<=or;j++)
for(i=0;i<=n;i++)
diff[i][j]=diff[i=1]-diff[i][j]-1;
i=0;
while(!(a[i]/h,ye=ay[i]);
for(k=0;k<=or;k++)
nr*=p-k+1;dr*=k;
printf(“when x=%f,y=%f\n”,x,ye);
return(0);
}
Output
Enter the value of n/n2
Enter the value in the form of x,y
2 2 4 2
Enter the value of x for which value of y
is wanted
4
When x=4.000000,y=6.000000
Program no-10
Write a program to find lagrange’s
interpoliton?
#include<stdio.h>
#include<math.h