0% found this document useful (0 votes)
36 views14 pages

Na Project

Uploaded by

victoryvibes8393
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)
36 views14 pages

Na Project

Uploaded by

victoryvibes8393
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/ 14

Ques-1- Write a program of Newton forward interpolation method.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float x[20],y[20],f,s,h,d,p;
int j,i,n;
printf("enter the value of n :");
scanf("%d",&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("Enter the value of f:");
scanf("%f",&f);
s=(f-x[1])/h;
p=1;
d=y[1];
for(i=1;i<=(n-1);i++)
{
for(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 is %f",f,d);
getch();
}
OUTPUT-

enter the value of n :5


enter the elements of x:
1891
1901
1911
1921
1931
enter the elements of y:
46
66
81
93
101
Enter the value of f:1895
For the value of x=1895.000000 THe value is 54.852798

NUMERICAL ANALYSIS Page 1


QUES-2- Write a program of Lagranges interpolation method of unequal interval.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main (){
float x[10], y[10], temp = 1, f[10], sum, p;
int i, n, j, k = 0, c;
printf ("\nhow many record you will be enter: ");
scanf ("%d", &n);
for (i = 0; i < n; i++)
{
printf ("\n\nenter the value of x%d: ", i);
scanf ("%f", &x[i]);
}
for (i = 0; i < n; i++)
{
printf ("\n\nenter the value of f(x%d): ", i);
scanf ("%f", &y[i]);
}
printf ("\n\nEnter X for finding f(x): ");
scanf ("%f", &p);
for (i = 0; i < n; i++)
{
temp = 1;
k = i;
for (j = 0; j < n; j++)
{
if (k == j) {
continue;
}
else {
temp = temp * ((p - x[j]) / (x[k] - x[j]));
}}
f[i] = y[i] * temp;
}
for (i = 0; i < n; i++)
{
sum = sum + f[i];
}
printf ("\n\n f(%.1f) = %f ", p, sum);
getch ();
}

OUTPUT-

how many record you will be enter: 4


enter the value of x0: 5
enter the value of x1: 6
enter the value of x2: 9
enter the value of x3: 11
enter the value of f(x0): 12
enter the value of f(x1): 13
enter the value of f(x2): 14
enter the value of f(x3): 16
Enter X for finding f(x): 10
f(10.0) = 14.666668

NUMERICAL ANALYSIS Page 2


Ques-3- Write a program of Trapezoidal method.

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

double f(double x)
{
return 1/(1+x);
}

void main()
{
int n,i;
double a,b,h,x,sum=0,integral;
printf("\nEnter the no. of intervals: ");
scanf("%d",&n);
printf("\nEnter the lower limit: ");
scanf("%lf",&a);
printf("\nEnter the upper limit: ");
scanf("%lf",&b);
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+f(x);
}

integral=(h/2)*(f(a)+f(b)+2*sum);

printf("\nThe integral is: %lf\n",integral);


}

OUTPUT-

Enter the no. of intervals: 4

Enter the lower limit: 0

Enter the upper limit: 1

The integral is: 0.697024

NUMERICAL ANALYSIS Page 3


QUES -4- Write a program of Simpson 3/8 method of numerical integration.

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

double f(double x){


return (1/(1+x*x));
}

void main(){
int n,i;
double a,b,h,x,sum=0,integral;
printf("\nEnter the lower limit: ");
scanf("%lf",&a);
printf("\nEnter the upper limit: ");
scanf("%lf",&b);
printf("Enter the number of intervals = ");
scanf("%d",&n);
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
if(i%3==0){
sum=sum+2*f(x);
}
else{
sum=sum+3*f(x);
}
}
integral=(3*h/8)*(f(a)+f(b)+sum);
printf("\nThe integral is: %lf\n",integral);
getch();
}

OUTPUT-

Enter the lower limit: 0


Enter the upper limit: 6
Enter the number of intervals = 6

The integral is: 1.357081

NUMERICAL ANALYSIS Page 4


Ques 5- Write a program of newton raphson method to find an exproximate root of an equation.

#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);
}
void main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
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;
}

OUTPUT-

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

NUMERICAL ANALYSIS Page 5


QUES-6- Write a program of Regula Falsi method to find an approximate root of an equation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Defining equation to be solved.
Change this equation to solve another problem. */
#define f(x) x*log10(x) - 1.2

int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
clrscr();
/* Inputs */
up:
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
/* Calculating Functional Values */
f0 = f(x0);
f1 = f(x1);
/* Checking whether given guesses brackets the root or not. */
if( f0*f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
/* Implementing Regula Falsi or False Position Method */
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = x0 - (x0-x1) * f0/(f0-f1);
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);

if(f0*f2 < 0)
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
step = step + 1;

}
while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
getch();
return 0;
}

NUMERICAL ANALYSIS Page 6


OUTPUT-

Enter two initial guesses:


2
3
Enter tolerable error:
0.000001

Step x0 x1 x2 f(x2)
1 2.000000 3.000000 2.721014 -0.017091
2 2.721014 3.000000 2.740206 -0.000384
3 2.740206 3.000000 2.740636 -0.000009
4 2.740636 3.000000 2.740646 -0.000000

Root is: 2.740646

NUMERICAL ANALYSIS Page 7


QUES-7- Write a program of Taylor series method to find the solution of ordinary differential equation.

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

int main()
{
int x,i;
int fact = 1,n;
float sum=0;

printf("\n\nEnter the value of x in the series : ");


scanf("%d",&x);

printf("\nEnter the number of terms in the series : ");


scanf("%d",&n);

for(i=1;i<n;i++)
{
fact = fact*i;
sum = sum + (pow(x,i)/fact) ;

}
sum= sum +1; //Since series starts with 1

printf("\n\nThe sum of the taylor series is : %.2f\n\n",sum);

return 0;
}

OUTPUT-

Enter the value of x in the series : 4

Enter the number of terms in the series : 5

The sum of the taylor series is : 34.33

NUMERICAL ANALYSIS Page 8


QUES-8- Write a program of Euler’s method to find the diffrential equation.

#include<stdio.h>
#include<math.h>
/*Define the RHS of the first order differential equation here(Ex: dy/dx=f(x,y)) */
double f(double x, double y){
return -2*x-y;
}
void main(){
int i;
double y,xi,yi,xf,h;
printf("Enter the initial condition for y: ");
scanf("%lf",&yi);
printf("Enter the initial condition for x: ");
scanf("%lf",&xi);
printf("Enter the value of x for which y is required: ");
scanf("%lf",&xf);
printf("Enter the step-width h: ");
scanf("%lf",&h);
//Begin Euler Routine
while(xi<xf){
y=yi+h*f(xi,yi);
yi=y;
xi=xi+h;
}
printf("The value of y is %lf\n\n",y);
getch();

OUTPUT-

Enter the initial condition for y: -1


Enter the initial condition for x: 0
Enter the value of x for which y is required: 0.4
Enter the step-width h: 0.1
The value of y is -0.768300

NUMERICAL ANALYSIS Page 9


QUES-9 Write a program Runge Kutta method to find the solution of ordinary differential equation.

#include<stdio.h>
#include<math.h>
float f(float x,float y);
int main()
{
float x0,y0,k1,k2,k3,k4,k,y,x,h,n;
printf("Enter the value of x0\n");
scanf("%f",&x0);
printf("Enter the value of y0\n");
scanf("%f",&y0);
printf("Enter the number of term\n");
scanf("%f",&n);
printf("Enter the value of h\n");
scanf("%f",&h);
x=x0;
y=y0;
printf("\n\nX\t\tY\n");
while(x<n)
{
k1=f(x0,y0);
k2=f((x0+h/2.0),(y0+k1*h/2.0));
k3=f((x0+h/2.0),(y0+k2*h/2.0));
k4=f((x0+h),(y0+k3*h));
k=((k1+2*k2+2*k3+k4)/6);
y=y+k*h;
x=x+h;
printf("%f\t%f\n",x,y);
}
}
float f(float x,float y)
{
float k;
k=x+y;
return k;
}

OUTPUT-

Enter the value of x0


0
Enter the value of y0
0
Enter the number of term
0.2
Enter the value of h
0.2
X Y
0.200000 0.021400

NUMERICAL ANALYSIS Page 10


Ques-10 Write a program of least square method to fit a straight line.

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

void main()
{
float x[100],y[100],sumx=0,sumx2=0,sumy=0,sumyx=0,b,a;
int i,n;
printf("Enter number of term : ");
scanf("%d",&n: );

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

for(i=0;i<n;i++)
{
sumx=sumx+x[i];
sumx2=sumx2+x[i]*x[i];
sumy=sumy+y[i];
sumyx=sumyx+y[i]*x[i];
}

//for y=ax+b
b=(sumx2*sumy-sumyx*sumx)/(n*sumx2-sumx*sumx);
a=(n*sumyx-sumx*sumy)/(n*sumx2-sumx*sumx);

printf("\nHence, the required eqn is y = %fx + %f",a,b);

getch();
}

OUTPUT-

Enter number of term : 5


Enter x[0] and y[0]: 0
1
Enter x[1] and y[1]: 1
1.8
Enter x[2] and y[2]: 2
3.3
Enter x[3] and y[3]: 3
4.5
Enter x[4] and y[4]: 4
6.3
Hence, the required eqn is y = 1.330000x + 0.720001

NUMERICAL ANALYSIS Page 11


QUES-11 Write a program of Picard’s method to find the solution of differential equation.

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

#define Y1(x) (1 + (x) + pow(x, 2) / 2)


#define Y2(x) (1 + (x) + pow(x, 2) / 2 + pow(x, 3) / 3 + pow(x, 4) / 8)
#define Y3(x) (1 + (x) + pow(x, 2) / 2 + pow(x, 3) / 3 + pow(x, 4) / 8 + pow(x, 5) / 15 + pow(x, 6) / 48)

int main()
{
double start_value, end_value, allowed_error, temp;
double y1[30], y2[30], y3[30];
int count;
printf("\nStart Value:\t");
scanf("%lf", &start_value);
printf("\nEnd Value:\t");
scanf("%lf", &end_value);
printf("\nAllowed Error:\t");
scanf("%lf", &allowed_error);

for(temp = start_value, count = 0; temp <= end_value; temp = temp + allowed_error, count++)
{
y1[count] = Y1(temp);
y2[count] = Y2(temp);
y3[count] = Y3(temp);
}

printf("\nX\n");
for(temp = start_value; temp <= end_value; temp = temp + allowed_error)
{
printf("%.4lf ", temp);
}

printf("\n\nY(1)\n");
for(temp = start_value, count = 0; temp <= end_value; temp = temp + allowed_error, count++)
{
printf("%.4lf ", y1[count]);
}

printf("\n\nY(2)\n");
for(temp = start_value, count = 0; temp <= end_value; temp = temp + allowed_error, count++)
{
printf("%.4lf ", y2[count]);
}

printf("\n\nY(3)\n");
for(temp = start_value, count = 0; temp <= end_value; temp = temp + allowed_error, count++)
{
printf("%.4lf ", y3[count]);
}
return 0;
}

NUMERICAL ANALYSIS Page 12


OUTPUT-

Start Value: 0
End Value: 3
Allowed Error: 0.4

X
0.0000 0.4000 0.8000 1.2000 1.6000 2.0000 2.4000 2.8000

Y(1)
1.0000 1.4800 2.1200 2.9200 3.8800 5.0000 6.2800 7.7200

Y(2)
1.0000 1.5045 2.3419 3.7552 6.0645 9.6667 15.0352 22.7205

Y(3)
1.0000 1.5053 2.3692 3.9833 7.1131 13.1333 24.3249 44.2335

NUMERICAL ANALYSIS Page 13


QUES-12 Write a program of predictor corrector method to find the solution of differential equation.

#include<stdio.h>
#include<math.h>
#define F(x,y) x+y
void main()
{
int i,n;
float x[20],y[20],h,f,f1,f2,yp,yc,xn;
printf("\n enter the value xn");
scanf("%f",&xn);
printf("\n enter the value:x[i],y[i]:\n");
for(i=0;i<=3;i++)
scanf("%f%f",&x[i],&y[i]);
h=x[1]-x[0];
n=(xn-x[0]/h);
for(i=3;i<=n;i++)
{
x[i+1]=x[i]+h;
f=F(x[i],y[i]);
f1=F(x[i-1],y[i-1]);
f2=F(x[i-2],y[i-2]);
yp=y[i-3]+4.0*h/3.0*(2.0*f2-f1+2.0*f);
yc=y[i-1]+h/3.0*(f1+4.0*f+F(x[i+1],yp));
printf("\n\n predicted Y=%f corrected Y=%f",yp,yc);
if(fabs(yp-yc)<0.00005)
goto next;
yp-yc;
next;
y[i+1]=yc;
printf("\n\n x=%f Y=%f",x[i+1],y[i+1]);
}
return;
}

Output:

enter the value xn:1


enter the value :x[i],y[i];
0.0 0.0
0.2 0.02
0.4 0.0906
0.6 0.2214
predicted Y=0.423147 corrected Y=0.429650

NUMERICAL ANALYSIS Page 14

You might also like