0% found this document useful (0 votes)
26 views2 pages

NMCP

Uploaded by

monishabrahma40
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)
26 views2 pages

NMCP

Uploaded by

monishabrahma40
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/ 2

Modified Eular

TRAPOZODIAL
#include<stdio.h> LAGRANGE
#include<math.h> #include<stdio.h>
float f(float x,float y) #include<stdio.h> #include<math.h>
{ #include<math.h> float f(float x)
return(2*x-y*y); void main() {
} { return(x*x);
void main() float x[20],y[20],sum=0,product,a; }
{ int i,j,n; void main()
float x0,y0,x1,y1,h,xn,y11,y12,y13,e=0.0001; printf("\nEnter no. of points: "); {
printf("\nType x0 and y0: "); scanf("%d",&n); float x0,xn,sum1=0,sum,h;
scanf("%f%f",&x0,&y0); printf("\nEnter the value of x where to interpolate: int i,n;
printf("\nType h: "); "); printf("\nEnter upper and lower limits: ");
scanf("%f",&h); scanf("%f",&a); scanf("%f%f",&x0,&xn);
printf("\nType xn: "); printf("\nEnter the data: "); printf("\nEnter the no. of points: ");
scanf("%f",&xn); for(i=0;i<n;i++) scanf("%d",&n);
printf("The values by Modified Euler method are: "); { h=(xn-x0)/n;
while(x0<=xn) scanf("%f%f",&x[i],&y[i]); for(i=1;i<n;i++)
{ } {
printf("\nWhen x = %f then y = %f",x0,y0); for(i=0;i<n;i++) sum1 = sum1 + f(x0+i*h);
y11 = y0+h*f(x0,y0); { }
x1 = x0+h; product = 1; sum = (h/2)*(f(x0)+f(xn)+2*sum1);
y12=y0+h*(f(x0,y0)+f(x1,y11))/2; for(j=0;j<n;j++) printf("The result is : %f",sum);
while(fabs(y11-y12)>e) {
{ if(i!=j) }
y13 = y0+h*(f(x0,y0)+f(x1,y12))/2; {
y11 = y12; product = product * (a-x[j])/(x[i]-x[j]);
y12 = y13; } TEMP RUNNER FILE
} }
x0 = x1; sum = sum + product * y[i]; #include<stdio.h>
y0 = y12; } #include<math.h>
}} printf("\nThe required value is:%f ",sum); float f(float x)
} {
Mline PC return(exp(x*2)-12*x+3);
#include<stdio.h> EULAR }
#include<math.h> void main()
float f(float x, float y) #include<stdio.h> {
{ return (x*y+y*y);} #include<math.h> float x1,x2,x,e=0.0001;
void main() float f(float x, float y) printf("\nEnter initial guess values: ");
{ { scanf("%f%f",&x1,&x2);
float x0,y0,x1,y1,x2,y2,x3,y3,x4,y4,y41,y42,h,xn,e; return (x+y); x= (x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
printf("\nType x0 and y0: "); } while(fabs(x2-x1)>e)
scanf("%f%f",&x0,&y0); void main() {
printf("\nType x1 and y1: "); { if(f(x)*f(x1)<0)
scanf("%f%f",&x1,&y1); float x0,y0,x1,y1,h,xn; {
printf("\nType x2 and y2: "); printf("\nType x0 and y0: "); x2=x;
scanf("%f%f",&x2,&y2); scanf("%f%f",&x0,&y0); }
printf("\nType x3 and y3: "); printf("\nType h: "); else{
scanf("%f%f",&x3,&y3); scanf("%f",&h); x1 = x;
printf("\nType h: "); printf("\nType xn: "); }
scanf("%f",&h); scanf("%f",&xn); }
printf("\nType xn: "); printf("The values by Euler method are: "); x= (x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
scanf("%f",&xn); while(x0<=xn) printf("\nThe reqd value is :%f",x);
printf("Type error value: "); { }
scanf("%f",&e); printf("\nWhen x = %f then y = %f",x0,y0);
while(x3<xn) x1=x0+h;
{ y1=y0+h*f(x0,y0);
x4=x3+h; x0=x1;
y4 = y0+(4*h*(2*f(x1,y1)-f(x2,y2)+2*f(x3,y3)))/3; y0=y1;
y41 = y2+(h*(f(x2,y2)+4*f(x3,y3)+f(x4,y4)))/3; }
printf("\nPredicted value of y at x = %f is y = }
%f",x4,y41);
while(fabs(y41-y4)>e) SIMPSON 3/8
{
y42 = y2+(h*(f(x2,y2)+4*f(x3,y3)+f(x4,y41)))/3; #include<stdio.h>
y4 = y41; #include<math.h>
y41 = y42; float f(float x)
printf("\nCorrected value of y at x = %f is y = BISECTION {
%f",x4,y42); return(x*x);
} #include<stdio.h> }
printf("\nThe value of y at x = %f is y = %f",x4,y4); #include<math.h> void main()
y0=y1; float f(float x) {
y1=y2; { float x0,xn,h,sum,sum1=0,sum2=0,sum3=0;
y2=y3; return(exp(x)-4*x+1); int i,n;
y3=y4; } printf("\nEnter lower and upper interval: ");
x0=x1; void main() scanf("%f%f",&x0,&xn);
x1=x2; { printf("\nEnter no of points: ");
x2=x3; float x1,x2,x,e=0.0001; scanf("%d",&n);
x3=x4; printf("\nEnter initial guess values: "); h=(xn-x0)/n;
} scanf("%f%f",&x1,&x2); sum1=f(x0)+f(xn);
} x=(x1+x2)/2; for(i=1;i<n;i++)
while(fabs(x2-x1)>e) {
{ if(i%3==0)
if(f(x)*f(x1)<0) {
{ sum2 = sum2+f(x0+i*h);
x2 = x; }
} else{
else{ sum3 = sum3+f(x0+i*h);
x1 = x; }
} }
x = (x1+x2)/2; sum = (3*h/8)*(sum1+3*sum3+2*sum2);
} printf("The required value is %f",sum);
printf("The required root is %f",x); }
}
Rk3 IMP EULAR
SIMSON 1 BY 3 #include<stdio.h>
#include<stdio.h> #include<math.h> #include<stdio.h>
#include<math.h> float f(float x, float y) #include<math.h>
float f(float x) { float f(float x,float y)
{ return (1+x*y); {
return(x*x); } return(x+y);
} void main() }
void main() { void main()
{ float x0,y0,x1,y1,xn,k1,k2,k3,t,h; {
float x0,xn,h,sum_odd=0,sum_even=0,integral = 0; printf("\nType x0 and y0: "); float x0,y0,x1,y1,h,xn,y11;
int i,n; scanf("%f%f",&x0,&y0); printf("\nType x0 and y0: ");
printf("\nEnter lower and upper interval: "); printf("\nType h: "); scanf("%f%f",&x0,&y0);
scanf("%f%f",&x0,&xn); scanf("%f",&h); printf("\nType h: ");
printf("\nEnter no of points: "); printf("\nType xn: "); scanf("%f",&h);
scanf("%d",&n); scanf("%f",&xn); printf("\nType xn: ");
h=(xn-x0)/n; printf("\nValues calculated by RK3 are: "); scanf("%f",&xn);
for(i=1;i<n;i++) while(x0<=xn) printf("The values by Improved Euler method are:
{ { ");
if(i%2!=0) printf("\nWhen x = %f then y = %f",x0,y0); while(x0<=xn)
{ k1 = h*f(x0,y0); {
sum_odd = sum_odd+f(x0+i*h); k2 = h*f(x0+(h/2),y0+(k1/2)); printf("\nWhen x = %f then y = %f",x0,y0);
} t = y0+h*f(x0+h,y0+k1);
else{ k3 = h*f(x0+h,t); y11 = y0+h*f(x0,y0);
sum_even = sum_even+f(x0+i*h); y1 = y0+(k1+4*k2+k3)/6; x1 = x0+h;
} x1=x0+h; y1 = y0+h*(f(x0,y0)+f(x1,y11))/2;
y0=y1; x0=x1;
} x0=x1; y0=y1;
integral =
(h/3)*(f(x0)+f(xn)+4*sum_even+2*sum_odd); } }
printf("The required value is %f",integral); }
} }

REGULAR FALSI
SECANT
#include<stdio.h> #include<stdio.h>
#include<math.h> #include<math.h>
float f(float x) float f(float x)
{ {
return (x*x*x-3*x+1); return(exp(x*2)-12*x+3);
} }
void main() void main()
{ {
float x1,x2,x3,e=0.00001; float x1,x2,x,e=0.0001;
printf("\nType x1 and x2: "); printf("\nEnter initial guess values: ");
scanf("%f%f",&x1,&x2); scanf("%f%f",&x1,&x2);
x3 = (x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); x= (x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
while(fabs(f(x2)-f(x1))>e) while(fabs(x2-x1)>e)
{ {
x1 = x2; if(f(x)*f(x1)<0)
x2 = x3; {
x3 = (x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); x2=x;
} }
printf("The real root is :%f",x3); else{
x1 = x;
} }
x= (x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
}

printf("\nThe reqd value is :%f",x);

RK4
#include<stdio.h>
#include<math.h> NEWTON RALPHSON
float f(float x, float y)
{ #include<stdio.h>
return(x+y); #include<math.h>
} float f(float x)
void main() {
{ return(x*x*x-4*x+1);
float x0,y0,x1,y1,h,xn,k1,k2,k3,k4; }
printf("\nType x0 and y0: "); float fd(float x)
scanf("%f%f",&x0,&y0); {
printf("\nType h: "); return(3*x*x-4);
scanf("%f",&h); }
printf("\nType xn: "); void main()
scanf("%f",&xn); {
printf("\nValues calculated by RK3 are: "); float x1,x2,e=0.00001;
while(x0<=xn) printf("\nEnter guess value: ");
{ scanf("%f",&x1);
printf("\nWhen x = %f then y = %f",x0,y0); x2 = x1-(f(x1)/fd(x1));
k1 = h*f(x0,y0); while(fabs(x2-x1)>e)
k2 = h*f(x0+h/2,y0+k1/2); {
k3 = h*f(x1+h/2,y0+k2/2); x1 = x2;
k4 = h*f(x0+h,y0+k3); x2 = x1-(f(x1)/fd(x1));
y1 = y0 + (k1+2*k2+2*k3+k4)/6;
x1 = x0+h; }
y0=y1; printf("\nThe required value is %f",x2);
x0=x1; }
}
}

You might also like