1.alg newton
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float fx,dfx,x[50],acc,diff;
int n;
printf("\nnewton raphson method");
printf("\n enter the intial approxmaition:");
scanf("%f",&x[0]);
printf("\n enter the accuracy:");
scanf("%f",&acc);
n=0;
do
{
fx=pow(x[n],3)-24;
dfx=(3*pow(x[n],2));
x[++n]=x[n-1]-fx/dfx;
diff=fabs(x[n]-x[n-1]);
printf("\nstep %d:approxmiation root=%f\t fx=%.3f",n,x[n],fx);
}while(diff>=acc);
printf("/n total step is %d read root of equation is %f",n,x[n]);
getch();
}
2.bisection method
#include<stdio.h>
#include<conio.h>
#include<math.h>
float fun(float x);
void main()
{
int n,i;
float x0,x1,x2,f0,f1,f2,acc;
printf("\nenter the no of iteration:");
scanf("%d",&n);
printf("\nenter the accuracy:");
scanf("%f",&acc);
printf("\nenter the roots value:");
scanf("%f%f",&x0,&x1);
f0=fun(x0);
f1=fun(x1);
if((f0*f1)>0)
{
printf("\ninvalid value");
getch();
exit(0);
}
printf("\nvalid interval");
i=1;
while((i<=n)&&fabs(x0-x1)>=acc)
{
x2=(x0+x1)/2;
f2=fun(x2);
if((f0*f2)<0)
x1=x2;
else
x0=x2;
printf("\n%f",x2);
i++;
}
getch();
}
float fun(float x)
{
return(pow(x,3)-(4*x)-5);
}
3.legranzious
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n;
float x[20],y[20],p[20],f,k=0;
printf("\n enter the no of terms;");
scanf("%d",&n);
printf("enter the value of ;\nx y\n");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
printf("\n enter the value of x for which y is to be found");
scanf("%f",&f);
for(j=1;j<=n;j++)
{
p[j]=1.0;
for(i=1;i<=n;i++)
{
if(i==j)
continue;
p[j]=p[j]*(f-x[i])/(x[j]-x[i]);
}
}
for(i=1;i<=n;i++)
k=k+p[i]*y[i];
printf("for x=%f,y=%f",f,k);
getch();
}
4.newton rapson
#include<stdio.h>
#include<conio.h>
#include<math.h>
float fun(float x);
float dfun(float x);
void main()
{
int i,n;
float x0,x1;
printf("\nenter the number of iteration:");
scanf("%d",&n);
printf("\nenter the intial guess for root:");
scanf("%f",&x0);
for(i=1;i<=n;i++)
{
x1=x0-(fun(x0)/dfun(x0));
x0=x1;
printf("\n the requried root is %f",x0);
}
getch();
}
float fun(float x)
{
return(pow(x,3)-(2*3)-5);
}
float dfun(float x)
{
return(3*pow(x,2)-2);
}
5.secant method
#include<stdio.h>
#include<conio.h>
#include<math.h>
float secant(float x);
void main()
{
int iter=0;
float x0,x1,x2,f0,f1,f2,acc;
printf("\n enter the roots value:");
scanf("%f%f",&x0,&x1);
printf("\nenter the accuracy:");
scanf("%f",&acc);
f0=secant(x0);
f1=secant(x1);
printf("\n secant method");
printf("\n no.\t x0\t x1\t x2\t f0\t f1\t f2\n");
do{
iter++;
x2=(x0*f1-x1*f0)/(f1-f0);
f2=secant(x2);
if(fabs(f1-f0)<=acc)
{
printf("\n slope is too small");
getch();
exit(0);
}
printf("\n%3d%8.4f\t%8.4f\t%8.4f\t%8.3f%8.3f%8.3f",iter,x0,x1,f0,f1,f2);
x0=x1;
x1=x2;
f0=f1;
f1=f2;
}while(fabs(f2)>acc);
printf("\nroot=%f in %d iteration",x2,iter);
getch();
}
float secant(float x)
{
return (pow(x,3)-(5*x)-10);
}
6.simpson
#include<stdio.h>
#include<conio.h>
#include<math.h>
float sim1(float,float,int);
float sim2(float,float,int);
int i;
float sum;
float fun(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
float x0,x1,sum,result;
int n,cho;
printf("enter the lower and upper limit \n");
scanf("%f%f",&x0,&x1);
printf("enter number of intervals:\n");
scanf("%d",&n);
printf("\t enter choice\n");
printf("\n 1.for simpsons1/3rule\n");
printf("\n 2.for simpsons3/8rule\n");
scanf("%d",&cho);
switch(cho)
{
case 1:
if(n%2==0)
{
result=sim1(x0,x1,n);
}
else
{
printf("wrong choice of interval");
printf("lease enter even number");
getch();
exit(0);
}
break;
case 2:
if(n%3==0)
{
result=sim2(x0,x1,n);
}
else
{
printf("wrong choice of intervals \n");
printf("\n please enter a multiple of 3\n");
getch();
exit(0);
}break;
default: printf("\n wrong choice enter again:");
}
printf("\n the result=%f",result);
getch();
}
float sim1(float x0,float x1,int n)
{
float result,h;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++);
{
if(i%2==0)
{
sum=sum+2*fun(x0+i*h);
}
else
{
sum=sum+4*fun(x0+i*h);
}
result=sum*(h/3);
return(result);
}
}
float sim2(float x0,float x1,int n)
{
float result,h;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++);
{
if(i%3==0)
{
sum=sum+2*fun(x0+i*h);
}
else
{
sum=sum+3*fun(x0+i*h);
}
result=sum*(3*h/8);
return(result);
}
}
7.trapizoidal
#include<stdio.h>
#include<conio.h>
#include<math.h>
float fun(float x);
void main()
{
int i,n;
float x0,xn,x,h,sum=0,trap;
printf("\n trapezodial rule:");
printf("\nenter the lower and upper limts and no. of intervals:");
scanf("%f%f%d",&x0,&xn,&n);
h=(xn-x0)/n;
printf("\n\t x\t f(x)");
for(i=1;i<=n;i++)
{
x=x0+i*h;
sum=sum+fun(x);
printf("\n\t%f\t%f",x,fun(x));
}
trap=(h/2)*(fun(x0)+(2*sum)+fun(xn));
printf("\n\tvalue of intgral using trapezodial rule %f",trap);
getch();
}
float fun(float x)
{
return (1/(1+x));
}