0% found this document useful (0 votes)
2 views38 pages

All Code & Chart

The document contains multiple numerical methods implemented in C for finding roots of equations and performing integration. It includes methods such as Bisection, False-Position, Secant, Successive Approximation, Newton-Raphson, Gauss-Seidel, Least Square Fit, Trapezoidal Rule, Simpson’s One-third Rule, and Gauss Quadrature. Each method is accompanied by code snippets that demonstrate how to input parameters, compute values, and output results.
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)
2 views38 pages

All Code & Chart

The document contains multiple numerical methods implemented in C for finding roots of equations and performing integration. It includes methods such as Bisection, False-Position, Secant, Successive Approximation, Newton-Raphson, Gauss-Seidel, Least Square Fit, Trapezoidal Rule, Simpson’s One-third Rule, and Gauss Quadrature. Each method is accompanied by code snippets that demonstrate how to input parameters, compute values, and output results.
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

Bisection Method

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

int main()
{ void gp(float,float,float);
float fn(float);
float bsxn(float,float,float);
float a,b,dx,allerr;
clrscr();

printf("\n\nEnter the range for checking roots and the interval -\n");
printf("a = ");
scanf("%f",&a);
printf("b = ");
scanf("%f",&b);
printf("interval = ");
scanf("%f",&dx);
printf("\nEnter the allowed error for the root - ");
scanf("%f",&allerr);

gp(a,b,dx);
bsxn(a,b,allerr);

getch();
return(0);
}

//Graph Values storing Function


void gp(float a,float b,float dx)
{
float fn(float);
float y,x;
FILE *ptr;
ptr=fopen("[Link]","w");

x=a;
while(x<b)
{
y=fn(x);
fprintf(ptr,"%f %f\n",x,y);
x=x+dx;
}
}

//Polynomial Function input


float fn(float x)
{
return((x*x*x)-(3*x)-5);
}

//Solution method
float bsxn(float x1,float x2,float eps)
{
float p,x3,x4,err;
p=fn(x1)*fn(x2);

printf("%f %f\n\n",fn(x1),fn(x2));

if(p>0)
{
printf("\n\n!! No root exist in the given range. !! \n\n");
return(0);
}

err=fabs(x2-x1);

while(err>eps)
{
x3=(x1+x2)/2;

if(fabs(fn(x3))<0.00001)
{
printf("%f is the root of the polynomial",x3);
break;
}

p=fn(x1)*fn(x3);

if(p<0)
{
x2=x3;
}
else
{
x1=x3;
}
printf("%f is root\n",x3);
err=fabs(x1-x2);
}

printf("\n\n%f is the root of the polynomial\n",x3);

return(0);
}
F(x)=x3-3x-5
False-Position Method
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{ void gp(float,float,float);
float fn(float);
float fpm(float,float,float);
float a,b,dx,allerr;
clrscr();

printf("\n\nEnter the range for checking roots and the interval -\n");
printf("a = ");
scanf("%f",&a);
printf("b = ");
scanf("%f",&b);
printf("interval = ");
scanf("%f",&dx);
printf("\nEnter the allowed error for the root - ");
scanf("%f",&allerr);

gp(a,b,dx);
fpm(a,b,allerr);

getch();
return(0);
}

//Graph Values storing Function


void gp(float a,float b,float dx)
{
float fn(float);
float y,x;
FILE *ptr;
ptr=fopen("[Link]","w");

x=a;
while(x<b)
{
y=fn(x);
fprintf(ptr,"%f %f\n",x,y);
x=x+dx;
}
}

//Polynomial Function input


float fn(float x)
{
return((x*x*x)-x-11);
}

//Solution method
float fpm(float x1,float x2,float eps)
{
float p,x3,err;
p=fn(x1)*fn(x2);

if(p>0)
{
printf("\n\n!! No root exist in the given range. !! \n\n");
return(0);
}

err=fabs(x2-x1);

while(err>eps)
{
x3=(x1*fn(x2)-x2*fn(x1))/(fn(x2)-fn(x1));

p=fn(x1)*fn(x3);

if(p<0)
{
x2=x3;
}
else
{
x1=x3;
}
err=fabs(fn(x3));
}

printf("\n\n%f is the root of the polynomial\n",x3);


return(0);
}
F(x)=x3-x-11
Secant Method

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

int main()
{ void gp(float,float,float);
float fn(float);
float sec(float,float,float);
float a,b,dx,allerr;
clrscr();

printf("\n\nEnter the range for checking roots and the interval -\n");
printf("a = ");
scanf("%f",&a);
printf("b = ");
scanf("%f",&b);
printf("interval = ");
scanf("%f",&dx);
printf("\nEnter the allowed error for the root - ");
scanf("%f",&allerr);

gp(a,b,dx);
sec(a,b,allerr);

getch();
return(0);
}

//Graph Values storing Function


void gp(float a,float b,float dx)
{
float fn(float);
float y,x;
FILE *ptr;
ptr=fopen("[Link]","w");

x=a;
while(x<b)
{
y=fn(x);
fprintf(ptr,"%f %f\n",x,y);
x=x+dx;
}
}

//Polynomial Function input


float fn(float x)
{
return(sin(x)-x*x+11);
}

//Solution method
float sec(float x0,float x1,float eps)
{
float y0,y1,y2,err,x2;

y0=fn(x0);
y1=fn(x1);

err=fabs(x0-x1);

while(err>eps)
{
x2=(x0*y1-x1*y0)/(y1-y0);

y2=fn(x2);

x0=x1;
x1=x2;
y0=y1;
y1=y2;

err=fabs(x0-x1);
}

printf("%f is the root of the given polynomial\n",x2);

return(0);
}
F(x)=sin(x)-x2+11
Successive Approximation Method
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
float fn(float);
float phi(float);
void gp();
float sa(float,float);
float x0,eps;

clrscr();

printf("Enter the initial guess and the allowed error : \n");


printf("Xo = ");
scanf("%f",&x0);
printf("eps = ");
scanf("%f",&eps);

sa(x0,eps);
gp();

getch();
return(0);
}

//Polynomial Function
float fn(float x)
{
float y;
y=cos(x)-(3*x)+1;
return(y);
}

float phi(float x)
{
float y;
y=(cos(x)+1)/3;
return(y);
}

//Graph Function
void gp()
{
float x1,x2,dx;
FILE *ptr;

ptr=fopen("[Link]","w");

printf("Enter the interval for graph : \n");


printf("x1 = ");
scanf("%f",&x1);
printf("x2 = ");
scanf("%f",&x2);
printf("dx = ");
scanf("%f",&dx);

while(x1<x2)
{
fprintf(ptr,"%f %f %f\n",x1,fn(x1),phi(x1));

x1=x1+dx;
}
}

//Successive Approximation Method


float sa(float x0,float eps)
{
float err,x1;

err=1.0;

while(err>eps)
{
x1=phi(x0);
err=fabs((x1-x0));

x0=x1;
getch();
}

printf("%f is the root of the given equation\n",x1);

return(0);
}
F(x)=(cos(x)+1)/3
Newton-Raphson Method
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
float fn(float);
float dfn(float);
void gp();
float nr(float,float);
float x0,eps;

clrscr();

printf("Enter the initial guess and the allowed error : \n");


printf("Xo = ");
scanf("%f",&x0);
printf("eps = ");
scanf("%f",&eps);

nr(x0,eps);
gp();

getch();
return(0);
}

//Polynomial Function
float fn(float x)
{
return(x*x*x-x+11);
}

float dfn(float x)
{
return(3*x*x-1);
}

//Graph Function
void gp()
{
float x1,x2,dx;
FILE *ptr;

ptr=fopen("[Link]","w");

printf("\n Enter the interval for graph : \n");


printf("x1 = ");
scanf("%f",&x1);
printf("x2 = ");
scanf("%f",&x2);
printf("dx = ");
scanf("%f",&dx);
while(x1<x2)
{
fprintf(ptr,"%f %f\n",x1,fn(x1));
x1=x1+dx;
}
}

//Newton Raphson Method


float nr(float x0,float eps)
{
float err,x1;

err=10.0;

while(err>eps)
{
x1=x0-(fn(x0)/dfn(x0));
err=fabs((x1-x0));

x0=x1;
}

printf("\n%f is the root of the given equation\n",x1);

return(0);
}
F(x)=3x2-1
Gauss-Seidal Method
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int i,j,n;
float a[3][4],x[3],prx[3],eps,sum,maxerr,err;
clrscr();

printf("Enter the number of variables :");


scanf("%d",&n);
printf("\nEnter the values of the coefficients in the row major
order :\n");

for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
scanf("%f",&a[i][j]);
}
}

printf("\nEnter the initial approx values for variables : \n");


for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
prx[i]=x[i];
}

printf("\nEnter the allowed error : ");


scanf("%f",&eps);

maxerr=10.0;

while(maxerr>eps)
{
maxerr=0.0;

for(i=0;i<n;i++)
{
sum=0.0;
for(j=0;j<n;j++)
{
if(i==j)
{
continue;
}
else
{
sum=sum+(a[i][j]*x[j]);
}
}
x[i]=(a[i][n]-sum)/a[i][i];
err=fabs(prx[i]-x[i]);
prx[i]=x[i];

if(err>maxerr)
{
maxerr=err;
}
printf("x[%d]=%f\n",i,x[i]);
getch();
}
}
printf("\n");

//output
for(i=0;i<n;i++)
{
printf("x[%d] = %f ,\t",i+1,x[i]);
}

getch();
return(0);
}
Least Square Fit
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float x[10],y[10],sx,sx2,sy,sxy,a,b,d;
int i,j,n;
void gp(float,float);
FILE *p;
p=fopen("[Link]","w");

clrscr();

printf("Enter the number of data to be entered : ");


scanf("%d",&n);
printf("Enter the data : \n x y\n");
for(i=0;i<n;i++)
{
scanf("%f %f",&x[i],&y[i]);
fprintf(p,"%f %f\n",x[i],y[i]);
}

sx=0;
sy=0;
sx2=0;
sxy=0;

for(i=0;i<n;i++)
{
sx+=x[i];
sy+=y[i];
sx2+=x[i]*x[i];
sxy+=x[i]*y[i];
}

d=(n*sx2)-(sx*sx);

a=((n*sxy)-(sx*sy))/d;
b=((sy*sx2)-(sx*sxy))/d;

printf("\nThe calculated value of the coefficients are :-


\na=%f\nb=%f\n",a,b);
printf("The equation of regression fucntion is :- y=%fx+%f\n",a,b);
gp(a,b);

getch();
return(0);
}

void gp(float a,float b)


{
float Y,x,x1,x2,dx;

FILE *ptr;
ptr=fopen("[Link]","w");

printf("Enter the interval and increment for graph plotting


(x1,x2,dx) :- ");
scanf("%f %f %f",&x1,&x2,&dx);

x=x1;

while(x<=x2)
{
Y=a*x+b;
fprintf(ptr,"%f %f\n",x,Y);
x=x+dx;
}
}
x Y=ax+b y
-4 0.980889 0.98
-3 1.487889 1.55
-2 1.994889 1.96
-1 2.501889 2.44
0 3.008889 3.10
1 3.515889 3.40
2 4.022889 4.10
3 4.529889 4.45
4 5.036889 5.10
Trapezoidal Rule
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int i,n;
float x,a,b,h,sum,I;
float f(float);
FILE *ptr;

clrscr();

ptr=fopen("[Link]","w");

printf("Enter the lower limit :- ");


scanf("%f",&a);
printf("Enter the upper limit :- ");
scanf("%f",&b);
printf("Enter the number of sub-intervals :- ");
scanf("%d",&n);

h=(b-a)/n;
sum=(f(a)+f(b))/2;
printf("h=%f\n",h);
x=a;

for(i=0;i<n-1;i++)
{
x=x+h;
sum=sum+f(x);
fprintf(ptr,"%f %f\n",x,f(x));
}

I=h*sum;

printf("\n The Integration of given function from %f to %f is :-


%f",a,b,I);
getch();
return(0);
}

float f(float x)
{
return(exp(x)-(x*x));
}
F(x)=ex-x2
Simpson’s One-third Rule
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int i,n,p;
float x,a,b,h,sum,I;
float f(float);
FILE *ptr;

clrscr();

ptr=fopen("[Link]","w");

printf("Enter the lower limit :- ");


scanf("%f",&a);
printf("Enter the upper limit :- ");
scanf("%f",&b);
printf("Enter the number of sub-intervals :- ");
scanf("%d",&n);

h=(b-a)/n;
sum=f(a)+f(b);
printf("h=%f\n",h);
x=a;
p=4;

for(i=0;i<n-1;i++)
{
x=x+h;
sum=sum+(p*f(x));
fprintf(ptr,"%f %f\n",x,f(x));
p=6-p;
}

I=(h/3)*sum;

printf("\n The Integration of given function from %f to %f is :- %f",a,b,I);


getch();
return(0);
}

float f(float x)
{
return(exp(x)-(x*x));
}
F(x)=ex-x2
Gauss Quadrature Rule
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n;
float a,b;
double u[10],r[10],x,I,sum;
float fn(float);
FILE *ptr;

clrscr();

ptr=fopen("[Link]","w");

printf("Enter the lower limit of the interval :-");


scanf("%f",&a);
printf("Enter the upper limit of the interval :-");
scanf("%f",&b);
printf("Enter the number of gaussian points :-");
scanf("%d",&n);
printf("Enter the value of gaussian points and their weights :-\n u
R\n");
for(i=0;i<n;i++)
{
scanf("%lf %lf",&u[i],&r[i]);
}

sum=0.0;

for(i=0;i<n;i++)
{
x=(((b-a)/2)*u[i])+((b+a)/2);
sum=sum+(r[i]*fn(x));
}

I=((b-a)*sum)/2;
printf("The integral of given function in the interval (%1.0f,%1.0f) is
:- %lf",a,b,I);

x=a;
while(x<=b)
{
fprintf(ptr,"%lf %lf\n",x,fn(x));
x=x+0.1;
}
getch();
return(0);
}
float fn(float x)
{
return((4*x*x*x)-(4*x)+1);
}

F(x)=4x3-4x+1
Runge-Kutta II order
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n;
float x0,y0,x,y,xn,h,x1,y1,s0,s1,S;
float fxy(float,float);
FILE *ptr;

clrscr();

ptr=fopen("[Link]","w");

printf("Enter the known value of x and y0 :-\n Xo = ");


scanf("%f",&x0);
printf(" Y0 = ");
scanf("%f",&y0);
printf("Enter the x at which solution is to be found :-\n Xn = ");
scanf("%f",&xn);
printf("Enter the number of steps to be taken :- \n n = ");
scanf("%d",&n);

h=(xn-x0)/n;
printf("\nh = %f\n",h);
while(x0<=xn)
{
s0=fxy(x0,y0);
x1=x0+h;
y1=y0+h*s0;
s1=fxy(x1,y1);

S=(s0+s1)/2;
y1=y0+h*S;

x0=x1;
y0=y1;

printf("%f %f\n",x0,y0);
fprintf(ptr,"%f %f\n",x0,y0);
}

printf("The value of given ODE at point %f is :- %f",xn,y0);

getch();
return(0);
}

float fxy(float x,float y)


{
return(x+y);
}
Runge-Kutta IV Order
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n;
float x0,y0,x,y,xn,h,x1,y1,s0,s1,s2,s3,S,x2,y2,yd;
float fxy(float,float);
FILE *ptr;
clrscr();
ptr=fopen("[Link]","w");

printf("Enter the known value of x and y0 :-\n Xo = ");


scanf("%f",&x0);
printf(" Y0 = ");
scanf("%f",&y0);
printf("Enter the x at which solution is to be found :-\n Xn = ");
scanf("%f",&xn);
printf("Enter the number of steps to be taken :- \n n = ");
scanf("%d",&n);

h=(xn-x0)/n;
printf("\nh = %f\n",h);
while(x0<=xn-h)
{
s0=fxy(x0,y0);
x1=x0+h/2;
y1=y0+h*s0/2;
s1=fxy(x1,y1);
yd=y0+(h*s1)/2;
s2=fxy(x1,yd);
x2=x0+h;
y2=y0+h*s2;
s3=fxy(x2,y2);

S=(s0+2*s1+2*s2+s3)/6;
y2=y0+h*S;

x0=x2;
y0=y2;

printf("%f %f\n",x0,y0);
fprintf(ptr,"%f %f\n",x0,y0);
}
printf("The value of given ODE at point %f is :- %f",xn,y0);
getch();
return(0);
}

float fxy(float x,float y)


{
return(x+y*y);
}
RK IV Method
dy/dx=x+y2
Euler’s Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n;
float x0,y0,x,y,xn,h;
float fxy(float,float);
FILE *ptr;

clrscr();

ptr=fopen("[Link]","w");

printf("Enter the known value of x and y0 :-\n Xo = ");


scanf("%f",&x0);
printf(" Y0 = ");
scanf("%f",&y0);
printf("Enter the x at which solution is to be found :-\n Xn = ");
scanf("%f",&xn);
printf("Enter the number of steps to be taken :- \n n = ");
scanf("%d",&n);

h=(xn-x0)/n;
x=x0;
y=y0;

printf("\nh = %f\n",h);
while(x<=xn)
{
y=y+h*fxy(x,y);
x=x+h;

printf("%f %f\n",x,y);
fprintf(ptr,"%f %f\n",x,y);
}

printf("The value of given ODE at point %f is :- %f",xn,y);

getch();
return(0);
}

float fxy(float x,float y)


{
return(x+y);
}

You might also like