0% found this document useful (0 votes)
17 views32 pages

Computer Organisation Numerical Method File

Computer science file

Uploaded by

59mansikangs4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views32 pages

Computer Organisation Numerical Method File

Computer science file

Uploaded by

59mansikangs4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

1.Write a program to solve a non-linear equation using Bisection method.

Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) pow(x,3)-x-4
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
printf("Enter tolerable error:\n");
scanf("%f", &e);
up:
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
f0 = f(x0);
f1 = f(x1);
if( f0 * f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = (x0 + x1)/2;
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);
}
Output:

2. Write a program to solve a non-linear equation using False Position method.


Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) pow(x,3)-4*x-9
int main()
{
float x0, x1, x2, f0, f1, f2, c,e;
int step = 1;
printf("Enter tolerable error:\n");
scanf("%f", &e);
up:
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
f0 = f(x0);
f1 = f(x1);
if( f0*f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
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);


}
Output:

3. Write a program to solve a non-linear equation using Secant method.


Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) x*x - 4*x - 10
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
printf("\nEnter initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Enter maximum iteration:\n");
scanf("%d", &N);
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
printf("Mathematical Error.");
exit(0);
}
x2 = x1 - (x1 - x0) * f1/(f1-f0);
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2, f2);
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
step = step + 1;
if(step > N)
{
printf("Not Convergent.");
exit(0);
}
}while(fabs(f2)>e);

printf("\nRoot is: %f", x2);


}
Output:

4. Write a program to solve a non-linear equation using Newton Raphson method.


Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) x*x*x-3*x+1
#define d(x) 3*x*x-3

int main()
{
float x0, x1, f0, f1, d0, e;
int step = 1, N;
printf("\nEnter initial guess:\n");
scanf("%f", &x0);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Enter maximum iteration:\n");
scanf("%d", &N);
printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
{
d0 = d(x0);
f0 = f(x0);
if(d0 == 0.0)
{
printf("Mathematical Error.");
exit(0);
}
x1 = x0 - f0/d0;
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,x1,f1);
x0 = x1;
step = step+1;
if(step > N)
{
printf("Not Convergent.");
exit(0);
}

f1 = f(x1);

}while(fabs(f1)>e);

printf("\nRoot is: %f", x1);


}
Output:
5. Write a program to solve a system of linear equation using Gauss Elimination method.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
printf("Enter number of unknowns: ");
scanf("%d", &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(i=1;i<=n-1;i++)
{
if(a[i][i] == 0.0)
{
printf("Mathematical Error!");
exit(0);
}
for(j=i+1;j<=n;j++)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
x[n] = a[n][n+1]/a[n][n];

for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = %f\n",i, x[i]);
}
r
}
Output:
6. Write a program to solve a system of linear equation using Gauss Jordan method.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
printf("Enter number of unknowns: ");
scanf("%d", &n);
printf("Enter coefficients of Augmented Matrix:\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(i=1;i<=n;i++)
{
if(a[i][i] == 0.0)
{
printf("Mathematical Error!");
}
for(j=1;j<=n;j++)
{
if(i!=j)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
}
for(i=1;i<=n;i++)
{
x[i] = a[i][n+1]/a[i][i];
}
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x%d = %0.3f\n",i, x[i]);
}
}
Output:
7. Write a program to solve a system of linear equation using Gauss Seidal method.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f1(x,y,z) (17-y+2*z)/20
#define f2(x,y,z) (-18-3*x+z)/20
#define f3(x,y,z) (25-2*x+3*y)/20
int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int count=1;
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("\nCount\tx\ty\tz\n");
do
{
x1 = f1(x0,y0,z0);
y1 = f2(x1,y0,z0);
z1 = f3(x1,y1,z0);
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
x0 = x1;
y0 = y1;
z0 = z1;
}while(e1>e && e2>e && e3>e);
printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
}

Output:

8. Write a program to generate Newton Forward Difference table.


Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float x[20], y[20][20];
int i,j, n;
printf("Enter number of data?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i = 0; i < n ; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i][0]);
}
for(i = 1; i < n; i++)
{
for(j = 0; j < n-i; j++)
{
y[j][i] = y[j+1][i-1] - y[j][i-1];
}
}
printf("\nFORWARD DIFFERENCE TABLE\n\n");
for(i = 0; i < n; i++)
{
printf("%0.2f", x[i]);
for(j = 0; j < n-i ; j++)
{
printf("\t%0.2f", y[i][j]);
}
printf("\n");
}
}
Output:
9. Write a program to generate Newton Backward Difference table.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float x[20], y[20][20];
int i,j, n;
printf("Enter number of data?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i = 0; i < n ; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i][0]);
}
for(i = 1; i < n; i++)
{
for(j = n-1; j > i-1; j--)
{
y[j][i] = y[j][i-1] - y[j-1][i-1];
}
}
printf("\nBACKWARD DIFFERENCE TABLE\n\n");
for(i = 0; i < n; i++)
{
printf("%0.2f", x[i]);
for(j = 0; j <= i ; j++)
{
printf("\t%0.2f", y[i][j]);
}
printf("\n");
}
}

Output:
10. Write a program to generate Newton Dividend Difference table.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float x[20], y[20][20];
int i,j, n;
printf("Enter number of data?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i = 0; i < n ; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i][0]);
}
for(i = 1; i < n; i++)
{
for(j = 0; j < n-i; j++)
{
y[j][i] = ( y[j + 1][i - 1]-y[j][i - 1]) /(x[i+j]-x[j]);
}
}
printf("\nDIVIDED DIFFERENCE TABLE\n\n");
for(i = 0; i < n; i++)
{
printf("%0.2f", x[i]);
for(j = 0; j < n-i ; j++)
{
printf("\t%0.2f", y[i][j]);
}
printf("\n");
}
}

Output:

11. Write a program to generate Newton Forward Difference interpolation polynomial.


Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float x[20], y[20][20];
int i,j, n,k;
float x0,u,y0,u0;
printf("Enter value of x for which y has to compute:\n");
scanf("%f", &x0);
printf("Enter number of data?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i = 0; i < n ; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i][0]);
}
for(i = 1; i < n; i++)
{
for(j = 0; j < n-i; j++)
{
y[j][i] = y[j+1][i-1] - y[j][i-1];
}
}
printf("\nFORWARD DIFFERENCE TABLE\n\n");
for(i = 0; i < n; i++)
{
printf("%0.2f", x[i]);
for(j = 0; j < n-i ; j++)
{
printf("\t%0.2f", y[i][j]);
}
printf("\n");
}
for(j = 0; j < n; j++)
{
if(x0<x[j]&&j==0)
{
u=(x0-x[j])/(x[j+1]-x[j]);
printf("u=%f\n",u);
u0=1;
y0=y[j][0];
for(k=1;k<n-j;k++)
{
u0=u0* (u - (k-1) )/ k;
y0=y0+u0*y[j][k];
}
printf("value of y for value of x %f\t=\t%f",x0,y0);
}
else
if(x0>x[j]&&x0<x[j+1])
{
u=(x0-x[j])/(x[j+1]-x[j]);
printf("u=%f\n",u);
u0=1;
y0=y[j][0];
for(k=1;k<n-j;k++)
{
u0=u0* (u - (k - 1) )/ k;
y0=y0+u0*y[j][k];
}
printf("value of y for value of x %f\t=\t%f",x0,y0);
}
}
}
Output:
12. Write a program to generate Newton Backward Difference interpolation polynomial.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float x[20], y[20][20];
int i,j, n,k;
float x0,y0,u0,u;
printf("Enter value of x for which y is to find\n");
scanf("%f", &x0);
printf("Enter number of data?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i = 0; i < n ; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i][0]);
}
for(i = 1; i < n; i++)
{
for(j = n-1; j > i-1; j--)
{
y[j][i] = y[j][i-1] - y[j-1][i-1];
}
}
printf("\nBACKWARD DIFFERENCE TABLE\n\n");
for(i = 0; i < n; i++)
{
printf("%0.2f", x[i]);
for(j = 0; j <= i ; j++)
{
printf("\t%0.2f", y[i][j]);
}
printf("\n");
}
for(j=n-1;j>=0;j--)
{
if(x0<x[j]&&x0>x[j-1])
{
u=(x0-x[j])/(x[j]-x[j-1]);
printf("u=%f\n",u);
u0=1;
y0=y[j][0];
for(k=1;k<=j;k++)
{
u0=u0* (u + (k -1 ))/k;
y0=y0+u0*y[j][k];
}
printf("value of y for value of x %f\t=\t%f",x0,y0);
}
else
if(x0>x[j]&&x[j+1]==NULL)
{
u=(x0-x[j])/(x[j]-x[j-1]);
printf("u=%f\n",u);
u0=1;
y0=y[j][0];
for(k=1;k<=j;k++)
{
u0=u0* (u + (k -1 ))/k;
y0=y0+u0*y[j][k];
}
printf("value of y for value of x %f\t=\t%f",x0,y0);
}

}
}
Output:
13. Write a program to generate Newton Dividend Difference interpolation polynomial.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float x[20], y[20][20];
int i,j, n,k;
float x0,y0,u0;
printf("Enter value of x for which y is to find?\n");
scanf("%f", &x0);
printf("Enter number of data?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i = 0; i < n ; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i][0]);
}
for(i = 1; i < n; i++)
{
for(j = 0; j < n-i; j++)
{
y[j][i] = ( y[j + 1][i - 1]-y[j][i - 1]) /(x[i+j]-x[j]);
}
}
printf("\nDIVIDED DIFFERENCE TABLE\n\n");
for(i = 0; i < n; i++)
{
printf("%f", x[i]);
for(j = 0; j < n-i ; j++)
{
printf("\t%f", y[i][j]);
}
printf("\n");
}
for(j = 0;j<n;j++)
{
if(x0>x[j]&&x0<x[j+1])
{
u0=x0-x[j+k];
y0=y[j][0];
for(k=1;k<n;k++)
{
y0=y0+u0*y[j][k];
u0=u0* (x0-x[j+k]);
}
printf("value of y for value of x %f\t=\t%f",x0,y0);
}
}
}

Output:
14. Write a program to generate Lagrage interpolation polynomial.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float x[20], y[20];
int i,j, n;
float y0,x0,k,l,u0;
printf("Enter the value of x for which y is to find.\n");
scanf("%f", &x0);
printf("Enter number of data?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i = 0; i < n ; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i]);
}
y0=0;

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


{
l=1;
k=1;
for(i = 0; i < n ; i++)
{
if(j!=i)
{
l=l*(x0-x[i]);
k=k*(x[j]-x[i]);
}
}
u0=l/k;
y0=y0+(y[j]*u0);
}
printf("value of y for value of x %f\t=\t%f",x0,y0);
}

Output:

15. Write a program to implement Trapezodial Rule for a known function.


Code:
16. Write a program to implement Simpson’s 1/3 Rule for a known function.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 2.718281828
#define f(x) 1/(1+pow(x,2))
int main()
{
float x[20], y[20];
int i,j, n;
float y0,h,O=0,X=0,E=0,a,b;
printf("Enter number of interval?\n");
scanf("%d", &n);
printf("Enter the range value of a and b:\n");
scanf("%f", &a);
scanf("%f",&b);
printf("\t x \t\t\ty\n");
h=(b-a)/n;
x[0]=a;
y[0]=f(x[0]);
for(i = 1; i <=n ; i++)
{
x[i]=x[i-1]+h;
y[i]=f(x[i]);
}
for(i = 0; i <=n ; i++)
{
printf("\t %f \t\t%f\n",x[i],y[i]);
}
h=(h/3);
X=y[0]+y[n];
for(i=1;i<n;i++)
{
if(i%2==0)
{
E=E+y[i];
}
else
{
O=O+y[i];
}
}
y0=h*(X+(4*O)+(2*E));
printf("\nresult:%f",y0);
}
Output:

17. Write a program to implement Simpson’s 3/8 Rule for a known function.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 2.718281828
#define f(x) pow(x,4)
int main()
{
float x[20], y[20];
int i,j, n;
float y0,h,O=0,X=0,E=0,a,b;
printf("Enter number of interval?\n");
scanf("%d", &n);
printf("Enter the range value of a and b:\n");
scanf("%f", &a);
scanf("%f",&b);
printf("\t x \t\t\ty\n");
h=(b-a)/n;
x[0]=a;
y[0]=f(x[0]);
for(i = 1; i <=n ; i++)
{
x[i]=x[i-1]+h;
y[i]=f(x[i]);
}
for(i = 0; i <=n ; i++)
{
printf("\t %f \t\t%f\n",x[i],y[i]);
}
h=(h*3)/8;
X=y[0]+y[n];
for(i=1;i<n;i++)
{
if(i%3==0)
{
E=E+y[i];
}
else
{
O=O+y[i];
}
}
y0=h*(X+(3*O)+(2*E));
printf("\nresult:%f",y0);
}
Output:

18. Write a program to implement Euler method to solve differential equation.


Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float x[20],y[20],h,fx1;
int i,n;
#define f(x,y) 1+pow(y,2)
int main()
{
printf("Enter the number of elements :");
scanf("%d",&n);
printf("Enter the values of x :");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
}
h=x[2]-x[1];
printf("Enter the value of y for x1\n");
scanf("%f",&y[1]);
for(i=1;i<n;i++)
{
fx1=f(x[i],y[i]);
y[i+1]=y[i]+(h*(fx1));
}
printf("Values of y for given value of x\n");
printf("\tx\t\t\ty\n");
for(i=1;i<=n;i++)
{
printf("\t%f\t\t%f\n",x[i],y[i]);
}
}
Output:

19. Write a program to implement Range Kutta Second Order method to solve the differential
equation.

Code:

You might also like