Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views19 pages

Na Project File

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)
22 views19 pages

Na Project File

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/ 19

Ques-1- Newton Gregory formula for forward interpolation .

#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 ANALYCIS Page 1


Ques -2-Newton Gregory backward interpolation formula.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float x[20],y[20],f,s,d,h,p;
int j,i,k,n;
printf("enter the value of the elements :");
scanf("%d",&n);
printf("enter the value of x:\n");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("enter the value of y:\n");
for(i=1;i<=n;i++)
{
scanf("%f",&y[i]);
}
h=x[2]-x[1];
printf("enter the searching point f:");
scanf("%f",&f);
s=(f-x[n])/h;
d=y[n];
p=1;
for(i=n,k=1;i>=1,k<n;i--,k++)
{
for(j=n;j>=1;j--)
{
y[j]=y[j]-y[j-1];
}
p=p*(s+k-1)/k;
d=d+p*y[n];
}
printf("for f=%f ,ans is=%f",f,d);
getch();
}

OUTPUT-

enter the value of the elements :5


enter the value of x:
1891
1901
1911
1921
1931
enter the value of y:
46
66
81
93
101
enter the searching point f:1925
for f=1925.000000 ,ans is=96.836800

NUMERICAL ANALYCIS Page 2


QUES-3-Lagrange’s interpolation formula.

#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 ANALYCIS Page 3


QUES-4 Newton’s Devide difference formula.

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

void main()
{
float x[10],y[10][10],sum,p,u,temp;
int i,n,j,k=0,f,m;
printf("\nhow many record you will be enter: ");
scanf("%d",&n);

for(i=0; i<n; i++)


{
printf("\nenter the value of x%d: ",i);
scanf("%f",&x[i]);
}

for(i=0; i<n; i++)


{
printf("\nenter the value of f(x%d): ",i);
scanf("%f",&y[k][i]);
}

printf("\nEnter X for finding f(x): ");


scanf("%f",&p);
for(i=1;i<n;i++)
{
k=i;
for(j=0;j<n-i;j++)
{
y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
k++;
}
}

sum=0;
for(i=0;i<n-1;i++)
{
k=f;
temp=1;
for(j=0;j<i;j++)
{
temp = temp * (p - x[k]);
k++;
}
sum = sum + temp*(y[i][f]);
}
printf("\n f(%.2f) = %f ",p,sum);

getch();
}

NUMERICAL ANALYCIS Page 4


OUTPUT-

how many record you will be enter: 6


enter the value of x0: 4
enter the value of x1: 5
enter the value of x2: 7
enter the value of x3: 10
enter the value of x4: 11
enter the value of x5: 13

enter the value of f(x0): 48


enter the value of f(x1): 100
enter the value of f(x2): 294
enter the value of f(x3): 900
enter the value of f(x4): 1210
enter the value of f(x5): 2028

Enter X for finding f(x): 8


f(8.00) = 448.000000

NUMERICAL ANALYCIS Page 5


QUES-5-Gauss’s forward center difference interpolation formula.

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

void main()
{
int n,i,j;
float ax[10],ay[10],diff[20][20];
float x,y=0,y1,y2,y3,y4,h,p;

printf("\n Enter the no. of terms : ");


scanf("%d",&n);

printf("\n Enter the value in the form of x : "); // Input Sequel for array X
for(i=0;i<n;i++)
{
printf("\n Enter the value of x%d : ",i+1);
scanf("%f",&ax[i]);
}

printf("\n Enter the value in the form of y : "); // Input sequel for array Y.
for(i=0;i<n;i++)
{
printf("\n Enter the value of y%d : ",i+1);
scanf("%f",&ay[i]);
}

printf("\n Enter the value of x for which u want the value of y : ");
scanf("%f",&x);

h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
diff[i][1]=ay[i+1]-ay[i];
for(j=2;j<=4;j++)
for(i=0;i<n-j;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
i=0;
do {
i++;
}

while(ax[i]<x);
i--;
p=(x-ax[i])/h;
y1=p*diff[i][1];
y2=p*(p-1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+1)*p*(p-1)*(p-2)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
printf("\n When x = %6.4f , y = %6.8f",x,y);

getch();
}

NUMERICAL ANALYCIS Page 6


OUTPUT-

Enter the no. of terms : 6

Enter the value in the form of x :

Enter the value of x1 : 2.5


Enter the value of x2 : 3.0
Enter the value of x3 : 3.5
Enter the value of x4 : 4.0
Enter the value of x5 : 4.5
Enter the value of x6 : 5.0

Enter the value in the form of y :

Enter the value of y1 : 24.145


Enter the value of y2 : 22.043
Enter the value of y3 : 20.225
Enter the value of y4 : 18.644
Enter the value of y5 : 17.262
Enter the value of y6 : 16.047

Enter the value of x for


which u want the value of y : 3.75

When x = 3.7500 , y = 19.40781212

NUMERICAL ANALYCIS Page 7


QUES-6-Gauss’s backward center difference interpolation formula.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
float x[20],y[20],f,s,d,h,p;
int j,i,k,n;
printf("enter the value of the elements :");
scanf("%d",&n);
printf("enter the value of x:\n");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("enter the value of y:\n");
for(i=1;i<=n;i++)
{
scanf("%f",&y[i]);
}
h=x[2]-x[1];
printf("enter the searching point f:");
scanf("%f",&f);
s=(f-x[n])/h;
d=y[n];
p=1;
for(i=n,k=1;i>=1,k<n;i--,k++)
{
for(j=n;j>=1;j--)
{
y[j]=y[j]-y[j-1];
}
p=p*(s+k-1)/k;
d=d+p*y[n];
}
printf("for f=%f ,ans is=%f",f,d);
getch();
}
OUTPUT-
enter the value of the elements :6
enter the value of x:
1931
1941
1951
1961
1971
1981
enter the value of y:
12
15
20
27
39
52
enter the searching point f:1966
for f=1966.000000 ,ans is=32.343750

NUMERICAL ANALYCIS Page 8


Ques-7- Trapezoidal Rule.

#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 ANALYCIS Page 9


QUES -8- Simpson’s 1/3rd rule.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x);
float f(float x)
{
return(1/(1+x));
}
int main()
{
int n,i;
float s1=0,s2=0,sum,a,b,h;
printf("Enter the lower limit = ");
scanf("%f",&a);
printf("Enter the upper limit = ");
scanf("%f",&b);
printf("Enter the number of intervals = ");
scanf("%d",&n);
h=(b-a)/n;
if(n%2==0)
{
for(i=1;i<=n-1;i++)
{
if(i%2==0)
{
s1=s1+f(a+i*h);
}
else
{
s2=s2+f(a+i*h);
}
}
sum=h/3*(f(a)+f(b)+4*s2+2*s1);
printf("The value is = %f",sum);
}
getch();
}

OUTPUT-

Enter the lower limit = 0


Enter the upper limit = 1
Enter the number of intervals = 4
The value is = 0.693254

NUMERICAL ANALYCIS Page 10


QUES -9- Simpson’s 3/8th Rule.

#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 ANALYCIS Page 11


QUES-10-Weddle’s formula.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
float f(float x)
{
float y;
y=1/(1+x*x);
return(y);
}
void main()
{
float a,b,h,s1=0,s2=0,s=0;
int i,n,m;
printf("Enter the value of lower limit= ");
scanf("%f",&a);
printf("Enter the value of upper limit= ");
scanf("%f",&b);
printf("Enter the number of intervals =");
scanf("%d",&n);
h=(b-a)/n;
printf("h= %f",h);
m=n/6;
s=0;
if(n%6==0)
{
for(i=1;i<=m;i++)
{
s=s+((3*h/10)*(f(a)+f(a+2*h)+5*f(a+h)+6*f(a+3*h)+f(a+4*h)+5*f(a+5*h)+f(a+6*h)));
a=a+6*h;
}
printf("Result is : %f",s);
}
else
{
printf(" Weddle's rule is not applicable");
}
getch();
}

OUTPUT-

Enter the value of lower limit= 0


Enter the value of upper limit= 6
Enter the number of intervals =6
h= 1.000000
Result is : 1.373448

NUMERICAL ANALYCIS Page 12


Ques-11- Bi-section 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);
}
void main ()
{
int itr = 0, maxmitr;
float x, a, b, allerr, x1;
printf("\nEnter 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;
}
OUTPUT-
Enter the values of a, b, allowed error and maximum iterations:
3
2
0.0005
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
Iteration no. 9 X = 2.70508
Iteration no. 10 X = 2.70605
Iteration no. 11 X = 2.70654
After 11 iterations, root = 2.7065

NUMERICAL ANALYCIS Page 13


Ques 12- Regula falsi 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);
}
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 ANALYCIS Page 14


QUES-13- Euler Method.

#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 ANALYCIS Page 15


QUES-14- Euler modified method

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

#define f(x,y) x+y

void main()
{
float x0, y0, xn, h, yn, slope;
int i, n;
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);

/* Calculating step size (h) */

h = (xn-x0)/n;

/* Euler's Method */

printf("\nx0\ty0\tslope\tyn\n");

printf("------------------------------\n");
for(i=0; i < n; i++)
{
slope = f(x0, y0);
yn = y0 + h * slope;
printf("%.4f\t%.4f\t%0.4f\t%.4f\n",x0,y0,slope,yn);
y0 = yn;
x0 = x0+h;
}

/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);

getch();

OUTPUT-

Enter Initial Condition


x0 = 0
y0 = 1

Enter calculation point xn = 1

Enter number of steps: 10

NUMERICAL ANALYCIS Page 16


x0 y0 slope yn
------------------------------
0.0000 1.0000 1.0000 1.1000
0.1000 1.1000 1.2000 1.2200
0.2000 1.2200 1.4200 1.3620
0.3000 1.3620 1.6620 1.5282
0.4000 1.5282 1.9282 1.7210
0.5000 1.7210 2.2210 1.9431
0.6000 1.9431 2.5431 2.1974
0.7000 2.1974 2.8974 2.4872
0.8000 2.4872 3.2872 2.8159
0.9000 2.8159 3.7159 3.1875

Value of y at x = 1.00 is 3.187

NUMERICAL ANALYCIS Page 17


QUES-15_ Picards method.

#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;
}

OUTPUT-

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

NUMERICAL ANALYCIS Page 18


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 ANALYCIS Page 19

You might also like