KEMBAR78
Scientific Computing | PDF | Mathematics | Numerical Analysis
0% found this document useful (0 votes)
13 views26 pages

Scientific Computing

The document contains several programming assignments related to numerical methods, including Newton-Raphson, Gauss Elimination, Gauss Seidel, successive approximation, Runge-Kutta, Lagrange interpolation, Trapezoidal rule, and an application for rocket motion. Each assignment includes code snippets in C, along with example outputs demonstrating the functionality of the programs. The assignments cover solving equations, finding roots, and estimating integrals using various numerical techniques.

Uploaded by

sreshthamuk96
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)
13 views26 pages

Scientific Computing

The document contains several programming assignments related to numerical methods, including Newton-Raphson, Gauss Elimination, Gauss Seidel, successive approximation, Runge-Kutta, Lagrange interpolation, Trapezoidal rule, and an application for rocket motion. Each assignment includes code snippets in C, along with example outputs demonstrating the functionality of the programs. The assignments cover solving equations, finding roots, and estimating integrals using various numerical techniques.

Uploaded by

sreshthamuk96
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/ 26

ASSIGNMENT 3

Write a program to find the root of the following equation


f(x)=x^3-4x+1=0 by Newton Raphson method. Result should
be correct upto 6 significant digits.

CODE
#include <stdio.h>

#include <stdlib.h>

#include <math.h>

float equation(float x)

float result= (x*x*x)-(4*x)+1;

return result;

float eq2(float x)

float result= (2*x*x)-4;

return result;

int Newton_Raphson()

int i,n,f=0;

float e=0.000001,a,fa,fa1,x;

printf("Enter the initial guess: \n");

scanf("%f", &a);

printf("Enter the max number of iterations: \n");


scanf("%d", &n);

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

fa=equation(a);

fa1=eq2(a);

if(fa1==0.0)

printf("Mathematical Error");

break;

x=a-(fa/fa1);

if (fabs((x-a)/x)>e)

a=x;

else

f=1;

break;

if(f==1)

printf("\nThe Approximate Root is %f in %d


interations",x,i);

else

printf("\nThe Number of Iterations is not sufficient");

return 0;

}
int main()

int a=Newton_Raphson();

return a;

OUTPUT
Enter the initial guess:

Enter the max number of iterations:

The Approximate Root is 0.254102 in 5 interations


ASSIGNMENT 4
Write a program to find the solution of a set of linear
equation using Gauss Elimination method.

CODE
#include <stdio.h>

int main()

int n, i, j, k;

printf("Enter the number of unknowns: ");

scanf("%d", &n);

float a[n][n + 1];

float x[n];

printf("Enter the coefficients of the matrix :\n");

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

{printf("\n");

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

printf("a[%d][%d] = ", i, j);

scanf("%f", &a[i][j]);

printf("\nSystem of linear system is:\n");

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

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


{

printf("%f ", a[i][j]);

printf("\n");

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

for (k = i + 1; k < n; k++)

if (a[i][i] == 0)

printf("Mathematical Error: Division by zero\


n");

return -1;

int factor = a[k][i] / a[i][i];

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

a[k][j] = a[k][j] - factor * a[i][j];

for (i = n - 1; i >= 0; i--)

x[i] = a[i][n];

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 = 0; i < n; i++)

printf("x[%d] = %.3f\n", i, x[i]);

return 0;

OUTPUT
Enter the number of unknowns: 2

Enter the coefficients of the matrix :

a[0][0] = 1

a[0][1] = 2

a[0][2] = 2

a[1][0] = 1

a[1][1] = -2

a[1][2] = 6

System of linear system is:

1.000000 2.000000 2.000000

1.000000 -2.000000 6.000000

Solution:

x[0] = 4.000

x[1] = -1.000
ASSIGNMENT 5
Write a program to find the solution of a set of linear
equation using Gauss Seidel method.

CODE
#include <stdio.h>

int main()

int n, i, j, k, m;

float sum = 0.0;

printf("Enter the number of unknowns: ");

scanf("%d", &n);

printf("Enter the number of iterations: ");

scanf("%d", &m);

float a[n][n];

float b[n];

float x[n];

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

x[i] = 0.0;

printf("Enter the coefficients of the matrix :\n");

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

printf("\n");

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


{

printf("a[%d][%d] = ", i, j);

scanf("%f", &a[i][j]);

printf("Enter the solutions of the equations :\n");

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

printf("b[%d]= ", i);

scanf("%f", &b[i]);

printf("\nInitial Solution:\n");

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

printf("x[%d] = %.3f\t", i, x[i]);

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

printf("\n");

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

sum = b[j];

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

if (k != j)

sum = sum - (a[j][k] * x[k]);


}

x[j] = sum / a[j][j];

for (int i1 = 0; i1 < n; i1++)

printf("x[%d] = %.3f\t", i1, x[i1]);

printf("\nFinal Solution:\n");

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

printf("x[%d] = %.3f\t", i, x[i]);

return 0;

OUTPUT
Enter the number of unknowns: 2

Enter the number of iterations: 5

Enter the coefficients of the matrix :

a[0][0] = 1

a[0][1] = 3

a[1][0] = 2

a[1][1] = 8

Enter the solutions of the equations :

b[0]= 6

b[1]= -12
Initial Solution:

x[0] = 0.000 x[1] = 0.000

x[0] = 6.000 x[1] = -3.000

x[0] = 15.000 x[1] = -5.250

x[0] = 21.750 x[1] = -6.938

x[0] = 26.812 x[1] = -8.203

x[0] = 30.609 x[1] = -9.152

Final Solution:

x[0] = 30.609 x[1] = -9.152


ASSIGNMENT 6
Write a program to solve any equation using successive
approximation method. Equation - 1. x^3-x^2-1=0

CODE
#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#define g(x) 1 / sqrt(x + 1)

int main()

double x0, x1, e;

int n;

printf("Enter initial guess : \n");

scanf("%lf", &x0);

printf("Enter allowed error : \n");

scanf("%lf", &e);

printf("Enter number of iterations : \n");

scanf("%d", &n);

printf("\n\nI\t\t x0\t\t\t x1\t\t x1 - x0\n");

for (int i = 1; i <= n; i++)

x1 = g(x0);

printf("%d\t | %.6f\t | %.6f\t | %.6f \n", i, x0, x1,


fabs(x1 - x0));

if (fabs(x1 - x0) <= e)


{

printf("\nConverges to a root %f , in %d
iterations\n", x1, i);

exit(0);

else

x0 = x1;

printf("\nDoes not converge to a root in %d iterations",


n);

return 0;

}
OUTPUT
Enter initial guess :

0.5

Enter allowed error :

0.000001

Enter number of iterations :

15

I x0 x1 x1 - x0

1 | 0.500000 | 0.816497 | 0.316497

2 | 0.816497 | 0.741964 | 0.074533

3 | 0.741964 | 0.757671 | 0.015707

4 | 0.757671 | 0.754278 | 0.003393

5 | 0.754278 | 0.755007 | 0.000729

6 | 0.755007 | 0.754850 | 0.000157

7 | 0.754850 | 0.754884 | 0.000034

8 | 0.754884 | 0.754876 | 0.000007

9 | 0.754876 | 0.754878 | 0.000002

10 | 0.754878 | 0.754878 | 0.000000

Converges to a root 0.754878 , in 10 iterations


ASSIGNMENT 7
Write e a program to solve differential equation using Runge-
kutta method.Problem - Given dy/dx = x+y, y(0)=1. Find Y at
x=0.2,taking h=0.1 . [use 2nd order Runge Kutta method].

CODE
#include <stdio.h>

float f(float x, float y)

return x + y;

int main()

float x0, y0, xn, h, k1, k2, k;

int n;

printf("\nEnter initial value of x (x0): ");

scanf("%f", &x0);

printf("\nEnter initial value of y (y0): ");

scanf("%f", &y0);

printf("\nEnter the point at which you want the solution


(xn): ");

scanf("%f", &xn);

printf("\nEnter step size (h): ");

scanf("%f", &h);

n = (xn - x0) / h;

printf("\nStep-by-step Runge-Kutta 2nd Order Computation:\


n");
for (int i = 1; i <= n; i++)

k1 = h * f(x0, y0);

k2 = h * f(x0 + h, y0 + k1);

k = (k1 + k2) / 2;

y0 = y0 + k;

x0 = x0 + h;

printf("x = %.2f,\ty = %.5f\n", x0, y0);

printf("\nApproximate value of y at x = %.2f is %.5f\n",


xn, y0);

return 0;

OUTPUT
Enter initial value of x (x0): 0

Enter initial value of y (y0): 1

Enter the point at which you want the solution (xn): 0.2

Enter step size (h): 0.1

Step-by-step Runge-Kutta 2nd Order Computation:

x = 0.10, y = 1.11000

x = 0.20, y = 1.24205

Approximate value of y at x = 0.20 is 1.24205


ASSIGNMENT 8
Write a program to use Lagrange Interpolation Method to
find a polynomial which takes on certain values of at arbitary
points.

CODE
#include <stdio.h>

int main()

int i, n;

float p, yn, xn;

printf("Enter the number of function values: ");

scanf("%d", &n);

float x[n];

float y[n];

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

printf("Enter the values of x[%d]=",i);

scanf("%f", &x[i]);

printf("Enter the values of y[%d]=",i);

scanf("%f", &y[i]);

printf("Enter the value of x for which you want to find


the value of y:");

scanf("%f", &xn);

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

p = 1;

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

if (i != j)

p = p * (xn - x[j]) / (x[i] - x[j]);

yn = yn + (y[i] * p);

printf("The value of y at x = %f is %f", xn, yn);

OUTPUT
Enter the number of function values: 4

Enter the values of x[0]=1

Enter the values of y[0]=-3

Enter the values of x[1]=3

Enter the values of y[1]=0

Enter the values of x[2]=4

Enter the values of y[2]=30

Enter the values of x[3]=6

Enter the values of y[3]=132

Enter the value of x for which you want to find the value of
y:5

The value of y at x = 5.000000 is 75.000000


ASSIGNMENT 9
Write a program to use Trapezoidal rule to estimate the
value of a definite integral.

CODE
#include <math.h>

#include <stdio.h>

float value(float a)

float x= a*a;

return x;

int main()

float a,b,n;

float x,y,sum=0,h;

printf("Enter the 2 limits of integration: ");

scanf("%f", &a);

scanf("%f", &b);

printf("Enter the number of subintervals: ");

scanf("%f", &n);

h = (b-a)/n;

x=a;

y=value(x);

sum=y;

for(int i = 2; i <= n; i++)


{

x=x+h;

y=value(x);

sum = sum + (2*y);

x=x+h;

y=value(x);

sum=sum+y;

sum = sum*h/2;

printf("The integral is: %f", sum);

return 0;

OUTPUT
Enter the 2 limits of integration: 0 1

Enter the number of subintervals: 4

The integral is: 0.343750


ASSIGNMENT 10
A Rocket is launched fro the ground. Its acceleration a is
measured every 5 seconds as tabulated below.
t) 0 5 10 15 20 25 30 35 40
40. 61.
a(t) 0 42.25 48.50 51.25 54.35 59.48 5 64.3 68.7

Find the velocity and position of the rocket at t=40 s using


Trapezoidal Rule

CODE
#include <stdio.h>

int main() {

int n;

printf("Enter the number of intervals: ");

scanf("%d", &n);

float t[n];

float a[n];

printf("Enter the time values (t0 to t%d):\n", n);

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

scanf("%f", &t[i]);

printf("Enter the accelaration values (t0 to t%d):\n",


n);

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


scanf("%f", &a[i]);

float h = 5.0;

float velocity = a[0] + a[n];

for (int i = 1; i < n; i++) {

velocity += 2 * a[i];

velocity = (h / 2) * velocity;

printf("\nVelocity at 40s: %.4f m/s\n", velocity);

float position = velocity * t[n];

printf("\nPosition at 40s: %.4f m\n", position);

return 0;

OUTPUT
Enter the number of intervals: 8

Enter the time values (t0 to t8):

0 5 10 15 20 25 30 35 40

Enter the accelaration values (t0 to t8):

40.0 42.25 48.50 51.25 54.35 59.48 61.5 64.3 68.7

Velocity at 40s: 2179.9001 m/s

Position at 40s: 87196.0078 m


ASSIGNMENT 11
Write a program to use Simpsons 1/3 rd rule to approximate
the value of a definite integral.

CODE
#include <math.h>

#include <stdio.h>

float value(float a)

float root =sqrt(a);

return root;

int main()

int a,b,n;

float x,y,sum,h;

printf("Enter the 2 limits of integration: ");

scanf("%d", &a);

scanf("%d", &b);

printf("Enter the number of subintervals [must be even]:


");

scanf("%d", &n);

h = (b-a)/n;

x=a;

y=value(x);
sum=y;

for(int i = 2; i <= n; i++)

x=x+h;

y=value(x);

if (i%2==0)

sum = sum + 4*y;

else

sum = sum + 2*y;

x=x+h;

y=value(x);

sum=sum+y;

sum = sum*h/3;

printf("The integral is: %f", sum);

return 0;

OUTPUT

Enter the 2 limits of integration: 0 8

Enter the number of subintervals [must be even]: 4

The integral is: 14.855494


ASSIGNMENT 12
A Rocket is launched fro the ground. Its acceleration a is
measured every 5 seconds as tabulated below.
t 0 5 10 15 20 25 30 35 40
a(t) 40.0 42.25 48.50 51.25 54.35 59.48 61.5 64.3 68.7

Find the velocity and positionof the rocket at t=40 s using


Simpsons 1/3rd Rule
CODE
#include <stdio.h>

int main() {

int n;

printf("Enter the number of intervals (must be even): ");

scanf("%d", &n);

if (n % 2 != 0)

printf("Number of intervals must be even!\n");

return 1;

float t[n];

float a[n];

printf("Enter the time values (t0 to t%d):\n", n);

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

scanf("%f", &t[i]);
}

printf("Enter the accelaration values (t0 to t%d):\n",


n);

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

scanf("%f", &a[i]);

float h = 5.0;

float velocity = a[0] + a[n];

for (int i = 1; i < n; i++) {

if (i % 2 == 0)

velocity += 2 * a[i];

else

velocity += 4 * a[i];

velocity = (h / 3) * velocity;

printf("\nVelocity at 40s: %.4f m/s\n", velocity);

float position = velocity * t[n];

printf("\nPosition at 40s: %.4f m\n", position);

return 0;

OUTPUT
Enter the number of intervals (must be even): 8

Enter the time values (t0 to t8):

0 5 10 15 20 25 30 35 40

Enter the accelaration values (t0 to t8):

40.0 42.25 48.50 51.25 54.35 59.48 61.5 64.3 68.7

Velocity at 40s: 2177.5332 m/s


Position at 40s: 87101.3281 m

You might also like