0% found this document useful (0 votes)
80 views21 pages

Numerical Methods

The document contains 7 questions on numerical analysis methods. Each question provides C code to implement a different method: 1) Gauss-Seidel, 2) Euler, 3) Runge-Kutta, 4) Gauss elimination, 5) Regula-Falsi, 6) Newton-Raphson, and 7) bisection. The questions provide example problems and explanations of how each method works to find the root of equations or solve systems of equations.

Uploaded by

Rahul Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views21 pages

Numerical Methods

The document contains 7 questions on numerical analysis methods. Each question provides C code to implement a different method: 1) Gauss-Seidel, 2) Euler, 3) Runge-Kutta, 4) Gauss elimination, 5) Regula-Falsi, 6) Newton-Raphson, and 7) bisection. The questions provide example problems and explanations of how each method works to find the root of equations or solve systems of equations.

Uploaded by

Rahul Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Programi

Nam
ng In C
e :
Roll
No :
Depa
rtme
nt :
Class
:
Subj
ect :

Piyush Bansal

001311501004
Power
1st Year
Numerical
Analysis

Conten
ts

Q1. To find the the results of simultaneous linear


equations using Gauss Seidel Method
Q2.Program by Euler Method
Q3.Program on Runge-Kutta method
Q4.Program on Gauss Elimination Method
Q5.Program on Regula-Falsi Method
Q6.Program on Newton Raphson method
Q7.Program on Bisection Method

Q1. To find the the results of simultaneous linear


equations using Gauss Seidel Method
#include<stdio.h>
#define EPS 0.0005
float a[90][90],b[90],x[90];
void Seidel(float [][90],float [],int,int);
int main()
{
int i,j,n,M;
printf(" Enter the number of variables(Max 90) in
the equation :- ");
scanf("%d",&n);
printf(" Enter the coefficients of the equations :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" Enter the coefficient of variable%d of equation-%d :- ",(i+1),(j+1));
scanf("%f",&a[i][j]);
}
printf(" Enter the RHS of the equation-%d :- ",
(i+1));
scanf("%f",&b[i]);
}
printf(" Enter the maximum number of iterations :");

scanf("%d",&M);
Seidel(a,b,n,M);
printf("\n The results are :- \n");
for(i=0;i<n;i++)
printf(" x%d=%f\n",(i+1),x[i]);
return 0;
}
void Seidel(float a[90][90],float b[90],int n,int M)
{
float o[90],s1=0,s2=0;
int k=0,m1=0,i,j;
for(i=0;i<n;i++)
x[i]=0;
while(k<M)
{
k++;
for(j=0;j<n;j++)
o[j]=x[j];
for(i=0;i<n;i++)
{
s1=0;
s2=0;
for(j=0;j<i;j++)
s1= s1+(a[i][j]*x[j]);
for(j=i+1;j<n;j++)
s2=s2+(a[i][j]*o[j]);
x[i]=(b[i]-s1-s2)/a[i][i];
}

for(i=0;i<n;i++)
{
if(m1<(x[i]-o[i]))
m1=x[i]-o[i];
}
if(m1<EPS || k>=M)
break;
printf("\n After iteration- %d :-\n",k);
for(i=0;i<n;i++)
printf(" x%d=%f\n",(i+1),x[i]);
}
}

Q2.Program by Euler Method


#include<stdio.h>
float f(float,float);
void Euler(float,float,float,float);
int main()
{
float x,y,xp,h;
printf(" Enter value of x :- ");
scanf("%f",&x);
printf(" Enter value of y :- ");
scanf("%f",&y);
printf(" Enter the value of 'x' at which 'y' is
required ");
scanf("%f",&xp);
printf(" Enter the measure of step size :- ");
scanf("%f",&h);
Euler(x,y,xp,h);
return 0;
}
float F(float x,float y)
{
float f;
f=x+y;
return f;
}
void Euler(float x,float y,float xp,float h)
{
int i,n;

float dy;
n=(int)((xp-x)/h);
for(i=1;i<=n;i++)
{

dy=h*(F(x,y));
x=x+h;
y=y+dy;
printf(" Value of y at x-%f is %f \n",x,y);
}

Q3.Program on Runge-Kutta method


#include<stdio.h>
float f(float,float);
void Euler(float,float,float,float);
int main()
{
float x,y,xp,h;
printf(" Enter value of x :- ");
scanf("%f",&x);
printf(" Enter value of y :- ");
scanf("%f",&y);
printf(" Enter the value of 'x' at which 'y' is required ");
scanf("%f",&xp);
printf(" Enter the measure of step size :- ");
scanf("%f",&h);
Euler(x,y,xp,h);
return 0;
}
float F(float x,float y)
{
float f;
f=x+y;
return f;
}
void Euler(float x,float y,float xp,float h)
{
int i,n;
float k1,k2,k3,k4,k;

n=(int)((xp-x)/h);
for(i=1;i<=n;i++)
{
k1=h*F(x,y);
k2=h*F((x+(1/2.0)*h),(y+(1/2.0)*k1));
k3=h*F((x+(1/2.0)*h),(y+(1/2.0)*k2));
k4=h*F((x+h),(y+k3));
k=(1/6.0)*(k1+(2*k2)+(2*k3)+k4);
x=x+h;
y=y+k;
printf(" Value of y at x-%f is %f \n",x,y);
}
}

Q4.Program on Gauss Elimination Method


#include<stdio.h>
#include<math.h>
/*Coefficients of the equation*/
void eliminate(int n,float a[10][10],float b[10])
{
int i,j,k,p,m;
float factor,sum;
for(k=1;k<n;k++)
{
pivot(n,a,b,k);
/*Elimination*/
for(i=k+1;i<=n;i++)
{
factor=a[i][k]/a[k][k];
for(j=k+1;j<=n;j++)
{
a[i][j]=a[i][j]-factor*a[k][j];
b[i]=b[i]-factor*b[k];
}}
void pivot(int n,float a[10][10],float b[10],int k)

{
int i,j,p;
float l,temp;
p=k;
l=fabs(a[k][k])
for(i=k+1;i<=n;i++)

{
if(fabs(a[i][k]>1)
{
l=fabs(a[i][k])
p=i;
}
}
/*Exchange rows p & k*/
if(p!=k)
{
for(j=1;j<=n;j++)
{
temp=a[p][j];
a[p][j]=a[k][j];
a[k][j]=temp;
}
temp=b[p];
b[p]=b[k];
b[k]=temp;
}
}
void backsubs(int n,float a[10][10],float b[10],float x[10])
{
int i,j,k;
float sum;
x[n]=b[n]/a[n][n];
for(k=n-1;k>=1;k--)
{

sum=0.0;
for(j=k+1;j<=n;j++)
sum=sum+a[k][j]*x[j];
x[k]=(b[k]-sum)/a[k][k];
}
}
int main()
{
int i,j,k,n;
printf("Enter values of coefficient of eqns.\n");
printf("Enter no.of variables in eqns.");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter coefficient of eqns.%dand variable %d",(i+1),(j+1));
scanf("%f",&a[i][j]);
}
printf("Enter rhs:%d",(i+1));
scanf("%f",&b[i]);
}
eliminate(n,a,b);
backsubs(n,a,b);
for(i=0;i<n;i++)

printf("x%d=%f",(i+1),x[i]);
return 0;

}
}

Q5.Program on Regula-Falsi Method


#include<stdio.h>
#include<math.h>
#define F(x) x*x*x-18
# define EPS 0.0005
float x1,x2,x0,f0,f1,f2;
void check();
float regula(float,float);
int main()
{
float root;
check();
root=regula(x1,x2);
printf("root=%f\n",root);
return 0;
}
/*To chek whether f1 & f2 are of opposite sign*/
void check()
{
while(1)
{
printf("ENTER X1\n");
scanf("%f",&x1);

printf("ENTER X2\n");
scanf("%f",&x2);

f1=F(x1);

f2=F(x2);
if((f1*f2)>0)
{
printf("WRONG INPUT ENTR NOTERE DATA\n");
continue;
}
else
break;
}
}
/*To check the false position*/
float regula(float x1,float x2)
{
int itr=0;
float x0;
while(1)
{
itr++;
x0=(x1*f2-x2*f1)/(f2-f1);
f0=F(x0);
printf("AT ITERATION=%d the VALUE X0=%f\n",itr,x0);
if(f0==0)
return x0;
if((f2*f0)<0)
x2=x0;
else
{
x1=x0;

f1=f0;
}
if(fabs((x2-x1)/x2)<EPS)
return ((x1*f2-x2*f1)/(f2-f1));
}
}

Q6.Program on Newton Raphson method


#include<stdio.h>
#include<math.h>
#define F(x) (x)*(x)-2
#define Fd(x) 2*(x)
#define EPS 0.00001
float x1,x0,f0,fd0;
/*count for iterations*/
int itr;
/*function for newton raphson method*/
float new_rap(float);
void main()
{
float x0;
printf("Enter the value of initial guess");
scanf("%f",&x0);
printf("Root =%f",new_rap(x0));
printf("\nIteration=%d",itr);
return 0;
}
float new_rap(float x0)
{
float f0,x1,fd0;
itr=0;
while(1)
{
itr++;
f0=F(x0);

fd0=Fd(x0);
x1=x0-(f0/fd0);
printf("\nitr=%d x0=%f x1=%f",itr,x0,x1);
if(fabs((x1-x0)/x1)<=EPS)
return x1;
else
x0=x1;
}
}

Q7.Program on Bisection Method


#include<stdio.h>
#include<math.h>
#define F(x) x*x*x-18
#define EPS 0.0001
float x1,x2,x0,f0,f1,f2;
void check();
float bisection(float,float);
int main()
{
float root;
check();
root=bisection(x1,x2);
printf("ROOT=%f\n",root);
return 0;
}
/*To check whether f 1 and f2 are of opposite sign*/
void check()
{
while(1)
{
printf("ENTER X1\n");
scanf("%f",&x1);

printf("ENTER X2\n");
scanf("%f",&x2);

f1=F(x1);

f2=F(x2);
if((f1*f2)>0)
{
printf("WRONG INPUT ENTR NOTERE DATA\n");
continue;
}
else
break;
}
}
/*To bisect the interval*/
float bisection(float x1,float x2)
{
int itr=0;
float x0;
while(1)
{
itr++;
x0=(x1+x2)/2;
f0=F(x0);
printf("AT ITERATION=%d the VALUE X0=%f\n",itr,x0);
if(f0==0)
return x0;
if((f1*f0)<0)
x2=x0;
else
{
x1=x0;

f1=f0;
}
if(fabs((x2-x1)/x2)<EPS)
return (x1+x2)/2.0;
}}

You might also like