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

conm file

Modi jii

Uploaded by

AAYUSH
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)
5 views

conm file

Modi jii

Uploaded by

AAYUSH
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
You are on page 1/ 26

BISECTION METHOD

#include<stdio.h>
#include<math.h>
float fun (float x)
{
return (x*x*x - 4*x - 9);
}
void bisection (float *x, float a, float b, int *itr)
/* this function performs and prints the result of one iteration */
{
*x=(a+b)/2;
++(*itr);
printf("Iteration no. %3d X = %7.5f\n", *itr, *x);
}
Int main ()
{
int itr = 0, maxmitr;
float x, a, b, allerr, x1;
printf(“This code is written by Amisha \n”);
printf("\n Enter the values of a, b, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &a, &b, &allerr, &maxmitr);
bisection (&x, a, b, &itr);
do
{
if (fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if (fabs(x1-x) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x1);
return 0;
}
x=x1;
}
while (itr < maxmitr);
printf("The solution does not converge or iterations are not sufficient");
return 1;
}
This code is written by Amisha
Enter the values of a, b, allowed error and maximum iterations:
3
2
0.004
20
Iteration no. 1 X = 2.50000
Iteration no. 2 X = 2.75000
Iteration no. 3 X = 2.62500
Iteration no. 4 X = 2.68750
Iteration no. 5 X = 2.71875
Iteration no. 6 X = 2.70312
Iteration no. 7 X = 2.71094
Iteration no. 8 X = 2.70703
After 8 iterations, root = 2.7070

=== Code Execution Successful ===


FALSE POSITION METHOD
#include<stdio.h>
#include<math.h>
float f(float x)
{
return cos(x) - x*exp(x);
}
void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
{
*x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
}
Int main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
printf(“This code is written by Amisha\n”);
printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
regula (&x2, x0, x1, f(x0), f(x1), &itr);
do
{
if (f(x0)*f(x2) < 0)
x1=x2;
else
x0=x2;
regula (&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3-x2) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x3);
return 0;
}
x2=x3;
}
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
return 1;
}
This code is written by Amisha
Enter the values of x0, x1, allowed error and maximum iterations:
0
1
0.0005
20
Iteration no. 1 X = 0.31467
Iteration no. 2 X = 0.44673
Iteration no. 3 X = 0.49402
Iteration no. 4 X = 0.50995
Iteration no. 5 X = 0.51520
Iteration no. 6 X = 0.51692
Iteration no. 7 X = 0.51748
Iteration no. 8 X = 0.51767
After 8 iterations, root = 0.5177

=== Code Execution Successful ===


SECANT METHOD
#include<stdio.h>
#include<math.h>
#define ESP 0.0001

#define F(x) x*x - 4*x - 10


void main()
{
float x1,x2,x3,f1,f2,t;
printf(“This code is written by Amisha\n”);
printf("\nEnter the value of x1: ");
scanf("%f",&x1);
printf("\nEnter the value of x2: ");
scanf("%f",&x2);
printf("\n________________________________________\n");
printf("\n x1\t x2\t x3\t f(x1)\t f(x2)");
printf("\n______________________________________________\n");
do
{
f1=F(x1);
f2=F(x2);
x3=x2-((f2*(x2-x1))/(f2-f1));
printf("\n%f %f %f %f %f",x1,x2,x3,f1,f2);
x1=x2;
x2=x3;

if(f2<0)
{
t=fabs(f2);
}
else
{
t=f2;
}
}
while(t>ESP);
printf("\n______________________________________________\n");
printf("\n\nApp.root = %f",x3);
getch();
}

This code is written by Amisha


Enter the value of x1: 4

Enter the value of x2: 5

________________________________________

x1 x2 x3 f(x1) f(x2)
______________________________________________

4.000000 5.000000 6.000000 -10.000000 -5.000000


5.000000 6.000000 5.714286 -5.000000 2.000000
6.000000 5.714286 5.740741 2.000000 -0.204082
5.714286 5.740741 5.741661 -0.204082 -0.006859
5.740741 5.741661 5.741657 -0.006859 0.000025
___________________________________________
App.root = 5.741657

=== Code Execution Successful ===


NEWTON RAPHSON METHOD
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*log10(x) - 1.2;
}
float df (float x)
{
return log10(x) + 0.43429;
}
void main()
{
int itr, maxmitr;
float h, x0, x1, allerr;
printf(“This code is written by Amisha\n”);
printf("\nEnter x0, allowed error and maximum iterations\n");
scanf("%f %f %d", &x0, &allerr, &maxmitr);
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
if (fabs(h) < allerr)
{
printf("After %3d iterations, root = %8.6f\n", itr, x1);
return 0;
}
x0=x1;
}
printf(" The required solution does not converge or iterations are insufficient\n");
return 1;
}
This code is written by Amisha
Enter x0, allowed error and maximum iterations
3
0.005
20
At Iteration no. 1, x = 2.746148
At Iteration no. 2, x = 2.740649
At Iteration no. 3, x = 2.740646
After 3 iterations, root = 2.740646

=== Code Execution Successful ===


BIRGE VIETA METHOD
#include<stdio.h>
#include<math.h>
float p[6], ply[6],q[6];
float synth(int m, float r){
int i;
q[0] = p[0];
for(i=1;i<=m;i++){
q[i] = (q[i-1]*r)+p[i];
}

printf("\n");
for(i=0;i<m;i++){
printf("\t%f",q[i]);
}
printf("\t%f",q[m]);
return(q[m]);
}

void main(){

int m,i,flag=0;
float r, x,x1, fx, fdx;
printf("BIRGE-VIETA METHOD\n");
printf("This is written by Amisha\n");
printf("\nEnter the highest degree of the equation (max 5): ");
scanf("%d",&m);
for(i=0;i<=m;i++){
printf("\n Coefficient x[%d] = ",m-i);
scanf("%f",&p[i]);
ply[i] = p[i];
}
printf("\nEnter the initial value x0 : ");
scanf("%f",&r);
x = r;

do{
printf("\n%f\n",x);
fx = synth(m,x);
for(i=0;i<=m;i++){
p[i]=q[i];
}
fdx = synth(m-1,x);
x1 = x - (fx/fdx);

if(fabs(x1-x) <= 0.0009){


flag = 1;
}
x = x1;

for(i=0;i<=5;i++){
p[i]=ply[i];
}

}while(flag!=1);

printf("\nApproximate root = %f", x1);

}
BIRGE-VIETA METHOD
This is written by Amisha

Enter the highest degree of the equation (max 5): 3

Coefficient x[3] = 1

Coefficient x[2] = 1

Coefficient x[1] = -3

Coefficient x[0] = -3

Enter the initial value x0 : 2

2.000000

1.000000 3.000000 3.000000 3.000000


1.000000 5.000000 13.000000
1.769231

1.000000 2.769231 1.899408 0.360492


1.000000 4.538462 9.928994
1.732924

1.000000 2.732924 1.735949 0.008266


1.000000 4.465847 9.474922
Approximate root = 1.732051

=== Code Execution Successful ===


GAUSS ELIMINATION METHOD
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum=0.0;
printf(“This code is written by Amisha”);
printf("\nEnter the order of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf("A[%d][%d] : ", i,j);
scanf("%f",&A[i][j]);
}
}
for(j=1; j<=n; j++) {
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
x[n]=A[n][n+1]/A[n][n];
for(i=n-1; i>=1; i--)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
printf("\nThe solution is: \n");
for(i=1; i<=n; i++)

{
printf("\nx%d=%f\t",i,x[i]); }
return(0);
}

This code is written by Amisha


Enter the order of matrix: 3
Enter the elements of augmented matrix row-wise:

A[1][1] : 10
A[1][2] : -7
A[1][3] : 3
A[1][4] : 5
A[2][1] : -6
A[2][2] : 8
A[2][3] : 4
A[2][4] : 7
A[3][1] : 2
A[3][2] : 6
A[3][3] : 9
A[3][4] : -1
The solution is:
x1=-7.809084
x2=-8.690902
x3=7.418177

=== Code Execution Successful ===


GAUSS JORDAN METHOD
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10];
printf(“This code is written by Amisha\n”);
printf("\nEnter the size of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(" A[%d][%d]:", i,j);
scanf("%f",&A[i][j]);
}
}
/* Now finding the elements of diagonal matrix */
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
printf("\nThe solution is:\n");
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("\n x%d=%f\n",i,x[i]);
}
return(0);
}
This code is written by Amisha
Enter the size of matrix: 3

Enter the elements of augmented matrix row-wise:


A[1][1]:10-
A[1][2]:7
A[1][3]:5
A[1][4]:9
A[2][1]:5
A[2][2]:6
A[2][3]:7
A[2][4]:8
A[3][1]:9
A[3][2]:7
A[3][3]:5
A[3][4]:4

The solution is:


x1=4.999996
x2=-10.631570
x3=6.684205
=== Code Execution Successful ===
GAUSS SIEDEL METHOD
#include<stdio.h>
#include<math.h>
#define X 2
int main()
{
float x[X][X+1],a[X], ae, max,t,s,e;
int i,j,r,mxit;
for(i=0;i<X;i++) a[i]=0;
printf(“This code is written by Amsha \n”);
puts(" Enter the elements of augmented matrix row wise\n");
for(i=0;i<X;i++)
{
for(j=0;j<X+1;j++)
{
scanf("%f",&x[i][j]);
}
}
printf(" Enter the allowed error and maximum number of iteration: ");
scanf("%f%d",&ae,&mxit);
printf("Iteration\tx[1]\tx[2]\n");
for(r=1;r<=mxit;r++)
{
max=0;
for(i=0;i<X;i++)
{
s=0;
for(j=0;j<X;j++)
if(j!=i) s+=x[i][j]*a[j];
t=(x[i][X]-s)/x[i][i];
e=fabs(a[i]-t);
a[i]=t;
}
printf(" %5d\t",r);
for(i=0;i<X;i++)
printf(" %9.4f\t",a[i]);
printf("\n");
if(max<ae)
{
printf(" Converses in %3d iteration\n", r);
for(i=0;i<X;i++)
printf("a[%3d]=%7.4f\n", i+1,a[i]);
return 0;
}
}
}

This code is written by Amisha


Enter the elements of augmented matrix row wise

3
45
6
7
8
9
Enter the allowed error and maximum number of iteration:
0.001
4
Iteration x[1] x[2]
1 2.0000 -0.6250
Converses in 1 iteration
a[ 1]= 2.0000
a[ 2]=-0.6250

=== Code Execution Successful ===


NEWTON FORWARD
INTERPOLATION
#include<stdio.h>
#include<math.h>
int main(){
float h,f,p,d,s;
int i,j,n;
printf("this code is written by Amisha \n");
printf("Enter the value of n (number of terms you want to enter): ");
scanf("%d",&n);
float x[n],y[n];
printf("Enter the elements of x\n");
for(i=1;i<=n;i++){
scanf("%f",&x[i]);
}
printf("Enter the elements of y\n");
for(i=1;i<=n;i++){
scanf("%f",&y[i]);

}
h=x[2]-x[1];
printf("Please enter the value of x for which you want to print y: ");
scanf("%f",&f);
p=1;
d=y[1];
s=(f-x[1])/h;
for(int i=1;i<=n-1;i++){
for(int j=1;j<=(n-i);j++){
y[j]=y[j+1]-y[j];

}
p=p*(s-i+1)/i;
d=d+p*y[1];

}
printf("For the value of x(%f) the value of y is %0.4f",f,d);

}
This program is written by Amisha
Enter the value of n (number of terms you want to enter): 4
Enter the elements of x
4
3
4
5
Enter the elements of y
6
7
8
7
6
6
Please enter the value of x for which you want to print y: 3
For the value of x(3.000000) the value of y is 7.0000

=== Code Execution Successful ===


SIMPSON’S 3/8 METHOD
#include<stdio.h>
#include<math.h>
#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, intgrl=0.0, stepSize, k;
int i, subInterval;
printf("This code is written by Amisha\n");
printf("Enter lower limit integration limit: ");
scanf("%f", &lower);
printf("Enter upper integration limit: ");
scanf("%f", &upper);
printf("Enter sub intervals: ");
scanf("%d", &subInterval);
stepSize = (upper - lower)/subInterval;
intgrl = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%3 == 0)
{
intgrl = intgrl + 2 * f(k);
}
else
{
intgrl= intgrl + 3 * f(k);
}
}
intgrl = intgrl * stepSize*3/8;
printf("The integration is: %.3f", intgrl);
return 0;
}
This code is written by Amisha
Enter lower limit integration limit: 0
Enter upper integration limit: 1
Enter sub intervals: 12
The integration is: 0.785

=== Code Execution Successful ===


RUNGE KUTTA METHOD (4TH
ORDER)

#include<stdio.h>
#include<math.h>
float f(float x,float y);
int main()
{
float x0,y0,m1,m2,m3,m4,m,y,x,h,xn;
printf(“this code is written by Amisha\n”);
printf("Enter x0,y0,xn,h:");
scanf("%f %f %f %f",&x0,&y0,&xn,&h);
x=x0;
y=y0;
printf("\n\nX\t\tY\n");
while(x<xn)
{
m1=f(x0,y0);
m2=f((x0+h/2.0),(y0+m1*h/2.0));
m3=f((x0+h/2.0),(y0+m2*h/2.0));
m4=f((x0+h),(y0+m3*h));
m=((m1+2*m2+2*m3+m4)/6);
y=y+m*h;
x=x+h;
printf("%f\t%f\n",x,y);
}
}
float f(float x,float y)
{
float m;
m=(x-y)/(x+y);
return m;
}
This code is written by Amisha
Enter x0,y0,xn,h:0
2
2
0.5

X Y
0.500000 1.621356
1.000000 1.242713
1.500000 0.864069
2.000000 0.485426

=== Code Execution Successful ===


MODIFIED EULAR METHOD
#include<stdio.h>
#include<math.h>
#include<string.h>
float fun(float,float);
int main()
{
int i,j,c;
float x[100],y[100],h,m[100],m1,m2,a,s[100],w;
printf("C program for Modified Euler Method \n");
printf("This code is written by Amisha\n");
printf("Enter the initial value of x:");
scanf("%f",&x[0]);
printf("\nEnter the value of increment h:");
scanf("%f",&h);
printf("\nEnter the final value of x:");
scanf("%f",&a);
printf("\nEnter the initial value of the variable y :");
scanf("%f",&y[0]);
s[0]=y[0];
for(i=1;x[i-1]<a;i++)
{
w=100.0;
x[i]= x[i-1]+h;
m[i]=fun(x[i-1],y[i-1]);
c=0;
while(w>0.0001)
{
m1=fun(x[i],s[c]);
m2=(m[i]+m1)/2;
s[c+1]=y[i-1]+m2*h;
w=s[c]-s[c+1];
w=fabs(w);
c=c+1;
}
y[i]=s[c];
}
printf("\nThe respective values of x and y are\n x \t y\n");
for(j=0;j<i;j++)
{
printf("%f\t%f",x[j],y[j]);
printf("\n");
}
}
float fun(float a,float b)
{
float c;
c=a*a+b;
return(c);
}

C program for Modified Euler Method

This code is written by Amisha


Enter the initial value of x:0
Enter the value of increment h:0.05
Enter the final value of x:0.1
Enter the initial value of the variable y :1
The respective values of x and y are
x y

0.000000 1.000000
0.050000 1.051345
0.100000 1.105579

=== Code Execution Successful ===


S NO DATE PARTICULARS SIGNATURES
1 Bisection
method
2 False position
method
3 Secant method
4 Newton
Raphson
method
5 Birge vieta
method
6 Gauss
elimination
7 Gauss Jordan
method
8 Gauss siedal
method
9 Newton forward
interpolation
10 Simpson’s 3/8
method
11 RK method
12 Modified eular
method

You might also like