Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Eüler’s Approximation Mathod:

#include <stdio.h>

Int main()

Int s;

Printf(“Enter the number of approximations “);

Scanf(“%d”,&s);

Float h,x,y;

H=0.1/s;

X=0,y=1;

Printf(“the successive values are y_0= %f\n”,y);

For(int i=1;i<s+1;i++)

Y=y+h*(x+y);

X=x+h;

Printf(“the successive values are y_%d= %f\n”,I,y);

}
Eüler’s Modified Method of apporximation

#include <stdio.h>

Int main()

Int s;

Printf(“Enter the number of approximation steps “);

Scanf(“%d”,&s);
Float h,x,y,v;

Printf(“Enter the value to find “);

Scanf(“%f”,&v);

H=v/s;

Float x_0,y_0;

Printf(“x_0,y_0= “);

Scanf(“%f,%f”,&x_0,&y_0);

X=x_0,y=y_0;

Printf(“The successive values are \ny_0= %f\n”,y);

For(int i=1;i<s+1;i++)

Float z=y+h*(pow(x,2)+y);

Y=y+h*(pow(x,2)+y+pow(x+h,2)+z)/2;

X=x+h;

Printf(“y_%d= %f\n”,I,y);

}
Runge-Kutta Method:

#include <stdio.h>
Int main()

Float k_1,k_2,k_3,k_4,k;

Float h,x,y,x_0,y_0,v;

Int s;

Printf(“Enter the number of steps: “);

Scanf(“%d”,&s);

Printf(“Enter the value to find: “);

Scanf(“%f”,&v);

H=v/s;

Printf(“Enter the initial values x_0,y_0= “);

Scanf(“%f,%f”,&x_0,&y_0);

X=x_0,y=y_0;

For(int i=0;i<s;i++)

K_1=h*(x+pow(y,2));

K_2=h*((x+h/2)+pow(y+k_1/2,2));

K_3=h*((x+h/2)+pow(y+k_2/2,2));

K_4=h*((x+h)+pow(y+k_3,2));

K=(k_1+2*k_2+2*k_3+k_4)/6;

Y=y+k;

X=x+h;

Printf(“The values of k_1= %f\nThe values of k_2= %f\nThe values of k_3= %f\nThe values of k_4=
%f\nThe values of k= %f\nThe values of y= %f\n”,k_1,k_2,k_3,k_4,k,y);

}
Trapezoidal Approximation:

#include <stdio.h>
#include <stdlib.h>

Int main()
{
Int s,n;
Printf(“Enter the no. of steps “);
Scanf(“%d”,&n);
Float a,b,h,x[n],y[n];
Printf(“Enter the upper and lower limits “);
Scanf(“%f,%f”,&b,&a);
H=(b-a)/n;
Float x_0=a,y_0=1/(1+pow(a,2));
Printf(“x_0= %f, y_0=%f\n”,x_0,y_0);
For(int i=0;i<n;i++)
{
X[i]=a+(i+1)*h;
Y[i]=1/(1+pow(x[i],2));
Printf(“x_%d= %f, y_%d= %f\n”,i+1,x[i],i+1,y[i]);
}
Float f=(y_0+y[n-1])/2;
Float sum=0;
For(int i=0;i<n-1;i++)
{
Sum =sum+y[i];
}
Float k=h*(sum+f);
Printf(“The integral value is = %f\n”,k);
Printf(“The value of pi is: %f”,4*k);
}
Eüler’s Bisection Method:

Int main()
{
Float x_0,x_1,x_2,f_0,f_1,f_2;
Printf(“Enter the value of x_0,x_1=”);
Scanf(“%f,%f”,&x_0,&x_1);
Float a;
Float e=2.718281828459045;
Printf(“define epsilon:=”);
Scanf(“%f”,&a);
Int i=0;
Do
{
X_2=(x_1+x_0)/2;
F_0=pow(e,x_0)-sin(x_0)-4;//the function is f(x)=e^x-sin(x)-4;
F_1=pow(e,x_1)-sin(x_1)-4;
F_2=pow(e,x_2)-sin(x_2)-4;
If(f_2*f_0<0)
{
X_1=x_2;
}
Else
{
X_0=x_2;
}
I++;
Printf(“Iteration no. %d”,i);
Printf(“ The value of x_2=%f”,x_2);
Printf(“ f(%f)=%f\n”,x_2,f_2);
}while(fabs(f_2)>a);

}
Newton Raphson Method:

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

Int main()
{
Float x;
Printf(“Enter starting value x_0 :”);
Scanf(“%f”,&x);
Int i=0;
Float e;
Printf(“Enter the value of e:”);
Scanf(“%f”,&e);
Do
{
X=x-(pow(x,3)-3*x-5)/(3*pow(x,2)-3);
Printf(“The step:%d\n”,i+1);
Printf(“f(%f)=%f\n”,x,pow(x,3)-3*x-5);
I++;
}while(fabs((x,3)-3*x-5)>e);
}
Regula- Falsi Method:

Int main()
{
Float x_0,x_1,x_2,f_0,f_1,f_2;
Printf(“Enter the value of x_0,x_1=”);
Scanf(“%f,%f”,&x_0,&x_1);
Float a;
Printf(“define epsilon:=”);
Scanf(“%f”,&a);
Int i=0;
Do
{
F_0=pow(x_0,5)-3*x_0+1;//the function is f(x)=x^5-3x+1;
F_1=pow(x_1,5)-3*x_1+1;
X_2=x_0-(f_0*(x_1-x_0))/(f_1-f_0);
F_2=pow(x_2,5)-3*x_2+1;
If(f_2*f_0<0)
{
X_1=x_2;
}
Else
{
X_0=x_2;
}
I++;
Printf(“Iteration no. %d”,i);
Printf(“ The value of x_2=%f”,x_2);
Printf(“ f(%f)=%f\n”,x_2,f_2);
}while(fabs(f_2)>a);

You might also like