0% found this document useful (0 votes)
34 views

Bi Section Method

The document contains code snippets demonstrating various numerical methods for solving equations including bisection method, Newton Raphson method, Gauss elimination method, Gauss Seidel iteration method, Euler's method, trapezoidal rule, Gauss Jordan method, Runge-Kutta method, Simpson's rule, and Millne's method. Each code snippet includes the necessary #include headers, defines variables and functions, inputs values from the user, performs the numerical computations, and outputs the results.

Uploaded by

guptavikas954
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Bi Section Method

The document contains code snippets demonstrating various numerical methods for solving equations including bisection method, Newton Raphson method, Gauss elimination method, Gauss Seidel iteration method, Euler's method, trapezoidal rule, Gauss Jordan method, Runge-Kutta method, Simpson's rule, and Millne's method. Each code snippet includes the necessary #include headers, defines variables and functions, inputs values from the user, performs the numerical computations, and outputs the results.

Uploaded by

guptavikas954
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

BI SECTION

METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return ((x*x*x)-(4*x)-9);
}
void main()
{
int itr=0,maxitr;
float x,a,b,x1,aerr;
printf("enter the value a,b,aloowed error maximum iteration ");
scanf ("%f %f %f %d",&a,&b,&aerr,&maxitr);
x=(a+b)/2;
do
{
if ((f(a)*f(x))<0)
b=x;
else
a=x;
itr=itr+1;
x1=(a+b)/2;
printf("iteration no. %d,x=%6.4f\n",itr+1,x1);
if (fabs(x1-x)<aerr)
{
printf("after %d iteration root=%6.4f\n",itr+1,x1);
}
x=x1;
}
while(itr<= maxitr);
printf(" solution does not converges ,iteration is not sufficient ");
getch();
}

NEWTON RAPHSON METHOD


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return ((3*x)-cos(x)-1);
}
float df(float x)
{
return (3+sin(x));
}
void main()
{
int itr, maxitr;
float x,h,x0,x1,aerr;
printf("enter the value of x0,aerr,maxitr");
scanf ("%f %f %d",&x0,&aerr,&maxitr);
for(itr=0;itr<=maxitr;itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf(" iteration no. %d x=%6.4f\n",itr,x1);
if (fabs (x1-x0)<aerr)
{
printf ("after %d iteration root = %6.4f\n",itr,x1);
}
x0=x1;
}
printf (" iteration not sufficient, solution does not converges");
getch();
}

GAUSS ELIMINATION METHOD


#include<stdio.h>
#include<conio.h>
#define n 4
void main()
{
float a[n][n+1],x[n],t,s;
int i,j,k;
printf("enter the element of the agumented matrix row wise \n");
for(i=0;i<n;i++)
for(j=0;j<n+1;j++)
scanf("%f",&a[i][j]);
for(j=0;j<n-1;j++)
for(i=j+1;i<n;i++)
{
t=a[i][j]/a[j][j];
for(k=0;k<n+1;k++)
a[i][k]-=a[j][k]*t;
}
/* now printing the
upper triangular matrix */
printf("the upper triangular matrix is :-\n");
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
printf("%8.4f",a[i][j]);
printf("\n");
}
/* now performing back substitution */
for(i=n-1;i>=0;i--)
{
s=0;
for(j=i+1;j<n;j++)
s+=a[i][j]*x[j];
x[i]=(a[i][n]-s)/a[i][i];
}
/* now printing the result */
printf("the solution is:-\n");
for(i=0;i<n;i++)
printf("x[%3d]=%7.4f\n",i+1,x[i]);
getch();
}

GAUSS SEIDAL ITERATION METHOD


#include<stdio.h>
#include<math.h>
#include<conio.h>
#define n 3
int main()
{
float a[n][n+1],x[n],aerr,maxerr,t,s,err;
int i,j,itr,maxitr;
/* first initalizing the array x*/
for(i=0;i<n;i++)
x[i]=0;
printf("enter the element of the augumented matrix rowwise\n");
for(i=0;i<n;i++)
for(j=0;j<n+1;j++)
scanf("%f",&a[i][j]);
printf("enter the allowed error""maximum iterations\n");
scanf("%f%d",&aerr,&maxitr);
printf("iteration x[1] x[2]""x[3]\n");
for(itr=1;itr<=maxitr;itr++)
{
maxerr=0;
for(i=0;i<n;i++)
{
s=0;
for(j=0;j<n;j++)
if(j!=i) s+=a[i][j]*x[j];
t=(a[i][n]-s)/a[i][i];
err=fabs(x[i]-t);
if (err>maxerr)maxerr=err;
x[i]=t;
}
printf("%5d",itr);
for(i=0;i<n;i++)
printf("%9.4f",x[i]);
printf("\n");
if (maxerr<aerr)
{
printf("converges in %3d""iteration\n",itr);
for(i=0;i<n;i++)
printf("x[%3d]=%7.4f\n",i+1,x[i]);
return 0;
}}
printf("solution does not converge""iteration not sufficient\n");
return 1;
}

EULERS METHOD

#include<stdio.h>
float df(float x,float y)
{
return x+y;
}
main ()
{
float x0,y0,h,x,x1,y1;
puts("enter the values of x0,y0,h,x");
scanf("%f %f %f %f",&x0,&y0,&h,&x);
x1=x0;y1=y0;
while(1)
{
if(x1>x) return 1;
y1+=h*df(x1,y1);
x1+=h;
printf("whenx=%3.1f""y=%4.2f\n",x1,y1);
}}

TRAPEZOIDAL RULE

#include<stdio.h>
float y(float x)
{
return 1/(1+x*x);
}
main()
{
float x0,xn,h,s;
int i,n;
puts("enter x0,xn,no of subintervals");
scanf("%f %f %d",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn);
for (i=1;i<=n-1;i++)
s+=2*y(x0+i*h);
printf("value of integral is %6.4f\n",(h/2)*s);
}

GAUSS JORDON METHOD


#include<stdio.h>
#include<conio.h>
#define n 4
void main()
{
float a[n][n+1],t;
int i,j,k;
clrscr();
printf("enter d elements of augumented matrix row wise \n");
for(i=0;i<n;i++)
for(j=0;j<n+1;j++)
scanf("%f",&a[i][j]);
/*now calculating the values of x1, x2......xn */
for(j=0; j<n;j++)
for(i=0;i<n;i++)
if(i!=j)
{
t= a[i][j]/a[j][j] ;
for(k=0;k<n+1;k++)
a[i][k]-=a[j][k]*t;
}
/*nw printing d diagonal matrix*/
printf("the diagonal matrix is :");
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
printf("%9.4f", a[i][j]);
printf("\n");
}
/* nw printing the result*/
printf("the soln is :");
for(i=0;i<n;i++)
printf("x[%3d]=%7.4f\n",i+1,a[i][n]/a[i][i]);
getch();
}

RUNGE-KUTTA METHOD

#include<stdio.h>
float f(float x,float y)
{
return x+y*y;
}
main()
{
float x0,y0,h,xn,x,y,k1,k2,k3,k4,k;
printf("enter the value of x0,y0,h,xn\n");
scanf("%f %f %f %f",&x0,&y0,&h,&xn);
x=x0;
y=y0;
while (1)
{
if (x==xn) break;
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x+=h;
y+=k;
printf("when x=%8.4f""y=%8.4f\n",x,y);
}}

SIMPSONS RULE

#include<stdio.h>
float y(float x)
{
return 1/(1+x*x);
}
main()
{
float x0,xn,h,s;
int i,n;
puts("enter x0,xn,no of subintervals");
scanf("%f %f %d",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn)+4*y(x0+h);
for (i=3;i<=n-1;i+=2)
s+=4*y(x0+i*h)+2*y(x0+(i-xit1)*h);
printf("value of integral is %6.4f\n",(h/3)*s);
}

MILLNES METHOD

#include<stdio.h>
#include<math.h>
float x[5],y[5],h;
float f(int i)
{
return x[i]-y[i]*y[i];
}
void corect()
{
y[4]=y[2]+(h/3)*(f(2)+4*f(3)+f(4));
printf("%8.4f %8.4f \n",y[4],f(4));
}
main()
{
float xr,aerr,yc;
int i;
puts("enter the value of x0,xr,h,allowed error");
scanf("%f %f %f %f",&x[0],&xr,&h,&aerr);
puts("enter the value of y[i],i=0,3");
for(i=0;i<=3;i++)
scanf("%f",&y[i]);
for(i=1;i<=3;i++)
x[i]=x[0]+i*h;
puts(" x predicated" " corrected");
puts(" x y f""y f");
while (1)
{
if(x[3]==xr) return 1;
x[4]=x[3]+h;
y[4]=y[0]+(4*h/3)*(2*(f(1)+f(3))-f(2));
printf("%6.2f %8.4f %8.4f\n",x[4],y[4],f(4));
corect();
while(1)
{
yc = y[4];
corect();
if(fabs(yc-y[4])<=aerr) break;
}
for(i=0;i<=3;i++)
{
x[i]=x[i+1];
y[i]=y[i+1];
}}}

You might also like