0% found this document useful (0 votes)
7 views18 pages

Numerical Analysis

The document contains multiple programming tasks related to numerical methods for finding roots of equations and solving systems of linear equations using various methods such as Bisection, Newton-Raphson, Regula-Falsi, Gauss Elimination, and Gauss-Seidel. Each task includes C code implementations and example outputs demonstrating the functionality of the programs. Additionally, there are tasks for evaluating integrals using the Trapezoidal Rule and Simpson's Rule, complete with code and expected results.

Uploaded by

theemperorreal
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)
7 views18 pages

Numerical Analysis

The document contains multiple programming tasks related to numerical methods for finding roots of equations and solving systems of linear equations using various methods such as Bisection, Newton-Raphson, Regula-Falsi, Gauss Elimination, and Gauss-Seidel. Each task includes C code implementations and example outputs demonstrating the functionality of the programs. Additionally, there are tasks for evaluating integrals using the Trapezoidal Rule and Simpson's Rule, complete with code and expected results.

Uploaded by

theemperorreal
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/ 18

Question1: Write and run the program in any software to find a root of the equation

𝒄𝒐𝒔𝒙 − 𝒙𝒆𝒙 = 𝟎 by Bisection method correct up to 3D places.


C-Code:
/* Bisection Method */
#include<stdio.h>
#include<math.h>
#define f(x) cos(x) - x * exp(x)
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 0;
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
f0 = f(x0);
f1 = f(x1);
if(f0*f1>0)
{
printf("Incorrect Initial Guesses");
}
else
{
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = (x0 + x1)/2;
f2 = f(x2);
printf("%d\t\t%0.4f\t\t%0.4f\t\t%0.4f\t\t%0.4f\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: %0.3f", x2);
}

}
Out-Put:
Enter two initial guesses:
0 1
Enter tolerable error:
0.0001

Step x0 x1 x2 f(x2)
0 0.0000 1.0000 0.5000 0.0532
1 0.5000 1.0000 0.7500 -0.8561
2 0.5000 0.7500 0.6250 -0.3567
3 0.5000 0.6250 0.5625 -0.1413
4 0.5000 0.5625 0.5313 -0.0415
5 0.5000 0.5313 0.5156 0.0065
6 0.5156 0.5313 0.5234 -0.0174
7 0.5156 0.5234 0.5195 -0.0054
8 0.5156 0.5195 0.5176 0.0005
9 0.5176 0.5195 0.5186 -0.0024
10 0.5176 0.5186 0.5181 -0.0009
11 0.5176 0.5181 0.5178 -0.0002
12 0.5176 0.5178 0.5177 0.0002
13 0.5177 0.5178 0.5178 -0.0000

Root is: 0.518


Question2: Write and run the program in any software to find a root of the equation
𝒙𝟑 − 𝟒𝒙 + 𝟏 = 𝟎 by Newton-Raphson method between 1 and 2 correct up to 4 significant
figures.
C-Code:
/*Newton Raphson Method*/
#include<stdio.h>
#include<math.h>
#define e 0.0001
#define f(x) pow(x,3)-4*x+1
#define df(x) 3*pow(x,2)-4
int main()
{
float x0, x1, f0, f1, df0;
int step = 0;
printf("\nEnter initial guess:\n");
scanf("%f",&x0);
printf("\n\tStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
{
f0 = f(x0);
df0 = df(x0);
if(df0 == 0.0)
{
printf("Mathematical Error.");
}
x1 = x0 - f0/df0;
f1 = f(x1);
printf("\n\t%d\t\t%.4f\t\t%.4f\t\t%.4f\t\t%.4f\n",step,x0,f0,x1,f1);
x0 = x1;
step = step+1;
}while(fabs(f1)>e);
printf("\nRoot is: %.3f",x1);
}
Out-Put:
Enter initial guess:
1

Step x0 f(x0) x1 f(x1)

0 1.0000 -2.0000 -1.0000 0.0000

1 -1.0000 4.0000 3.0000 4.0000

2 3.0000 16.0000 2.3043 16.0000

3 2.3043 4.0187 1.9675 4.0187

4 1.9675 0.7462 1.8695 0.7462

5 1.8695 0.0558 1.8609 0.0558

6 1.8609 0.0004 1.8608 0.0004

Root is: 1.861


Question3: Write and run a program in any software to find a root of the equation
𝒙𝒍𝒐𝒈𝟏𝟎 𝒙 = 𝟏. 𝟐 by Regula-Falsi method, correct up to 5 significant figures.
C-Code:
/* Regula Falsi Method */
#include<stdio.h>
#include<math.h>
#define f(x) x*log10(x) - 1.2
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 0;
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
f0 = f(x0);
f1 = f(x1);
if(f0*f1>0)
{
printf("Incorrect Initial Guesses");
}
else
{
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: %0.4f", x2);
}

}
Out-Put:
Enter two initial guesses:
2
3
Enter tolerable error:
0.0001

Step x0 x1 x2 f(x2)
0 2.000000 3.000000 2.721014 -0.017091
1 2.721014 3.000000 2.740206 -0.000384
2 2.740206 3.000000 2.740636 -0.000009

Root is: 2.7406


Question4: Write and run the program in any software to solve the following system of
linear equations 𝒙 + 𝟐𝒚 − 𝒛 = 𝟑, 𝟐𝒙 + 𝟓𝒚 + 𝟐𝒛 = −𝟑, 𝟒𝒙 − 𝟐𝒚 + 𝒛 = 𝟏𝟐 by Gauss
Elimination method correct up to 3 decimal places.
C-Code:
/* Gauss Elimination Method */
#include<stdio.h>
#include<math.h>
#define L 5
int main()
{
float a[L][L], x[L], R;
int i,j,k,n;
printf("Enter number of unknowns: ");
scanf("%d", &n);
printf("Enter Elements 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-1;i++)
{
if(a[i][i] == 0.0)
{
printf("Mathematical Error!");
}
for(j=i+1;j<=n;j++)
{
R = a[j][i]/a[i][i];

for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - R*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] = %0.3f\n",i, x[i]);
}
}

Out-Put:
Enter number of unknowns: 3
Enter Elements of Augmented Matrix:
a[1][1] = 1
a[1][2] = 2
a[1][3] = -1
a[1][4] = 3
a[2][1] = 2
a[2][2] = 5
a[2][3] = 2
a[2][4] = -3
a[3][1] = 4
a[3][2] = -2
a[3][3] = 1
a[3][4] = 12

Solution:
x[1] = 3.000
x[2] = -1.000
x[3] = -2.000
Question5: Write and run the program in any software to solve the following system of
linear equations 𝟑𝒙 + 𝟐𝟎𝒚 − 𝒛 = −𝟏𝟖, 𝟐𝒙 − 𝟑𝒚 + 𝟐𝟎𝒛 = 𝟐𝟓, 𝟐𝟎𝒙 + 𝒚 − 𝟐𝒛 = 𝟏𝟕 by
Gauss Seidel Method correct up to 3D places.
C-Code:
/* Gauss-Seidel Iteration Method */
#include<stdio.h>
#include<math.h>
/* In this example we are solving
3x + 20y - z = -18, 2x - 3y + 20z = 25, 20x + y - 2z = 17 */
/* Arranging given system of linear equations in diagonally dominant form:
20x + y - 2z = 17, 3x + 20y -z = -18, 2x - 3y + 20z = 25 */
/* Equations: x = (17-y+2z)/20, y = (-18-3x+z)/20, z = (25-2x+3y)/20 */
/* Defining function */
#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=0;
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("\nCount\t\tx\t\ty\t\tz\n");
do
{
/* Calculation */
x1 = f1(x0,y0,z0);
y1 = f2(x1,y0,z0);
z1 = f3(x1,y1,z0);
printf("%d\t\t%0.4f\t\t%0.4f\t\t%0.4f\n",count, x1,y1,z1);
/* Errors */
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
/* Set value for next iteration */
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);
}
Out-Put:
Enter tolerable error:
0.0001

Count x y z
0 0.8500 -1.0275 1.0109
1 1.0025 -0.9998 0.9998
2 1.0000 -1.0000 1.0000
3 1.0000 -1.0000 1.0000

Solution: x=1.000, y=-1.000 and z = 1.000


Question6: Write and run the program in any software to evaluate the following integral
𝟏 𝒅𝒙
∫𝟎 by Trapezoidal Rule taking 12 subintervals correct up to 4D places.
𝟏+𝒙𝟐
C-Code:
/* Trapezoidal Rule */
#include<stdio.h>
#include<math.h>
#define f(x) 1/(1+pow(x,2))
int main()
{
float a, b, I=0.0, h, k;
int i, n;
printf("Enter lower limit (a) of integration: ");
scanf("%f", &a);
printf("Enter upper limit (b) of integration: ");
scanf("%f", &b);
printf("Enter number (n) of sub intervals: ");
scanf("%d", &n);
h = (b - a)/n;
I = f(a) + f(b);
for(i=1; i<= n-1; i++)
{
k = a + i*h;
I = I + 2 * f(k);
}
I = I * h/2;
printf("\nRequired value of Integration (I) is: %0.4f",I);
}

Out-Put:
Enter lower limit (a) of integration: 0
Enter upper limit (b) of integration: 1
Enter number (n) of sub intervals: 12

Required value of Integration (I) is: 0.7851


𝟐 𝒅𝒙
Question7: Write and run the program in any software to compute the value of ∫𝟏
√𝟏+𝒙𝟐
𝟏
taking 14 subintervals by Simpson’s 𝟑 −Rule correct up to 4 decimal places.
C-Code:
/*Simpson 1/3 Rule*/
#include<stdio.h>
#include<math.h>
#define f(x) 1/sqrt(1+x*x)
int main()
{
float a, b, I=0.0, h, k;
int i, n;
printf("Enter lower limit (a) of integration: ");
scanf("%f", &a);
printf("Enter upper limit (b) of integration: ");
scanf("%f", &b);
printf("Enter number (n) of sub intervals: ");
scanf("%d", &n);
h = (b - a)/n;
I = f(a) + f(b);
for(i=1; i<= n-1; i++)
{
k = a + i*h;
if(i%2==0)
{
I = I + 2 * f(k);
}
else
{
I = I + 4 * f(k);
}
}
I = I * h/3;
printf("\nRequired value of Integration (I) is: %0.4f", I);
}

Out-Put:
Enter lower limit (a) of integration: 1
Enter upper limit (b) of integration: 2
Enter number (n) of sub intervals: 14

Required value of Integration (I) is: 0.5623


Question8: Write and run the program in any software to find an approximate value of
𝒅𝒚
𝒚(𝟏) from the following initial value problem = 𝒙 + 𝒚 with 𝒚(𝟎) = 𝟏 and taking 𝒉 =
𝒅𝒙
𝟎. 𝟏 by Euler’s method correct up to 4 decimal places.
C-Code:
/* Euler Method */
#include<stdio.h>
#define f(x,y) x+y
int main()
{
float x0, y0, xn, h, yn, slope;
int i, n, step=0;
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(n): ");
scanf("%d", &n);
h = (xn-x0)/n;
printf("\nStep\tx0\ty0\tslope\tyn\n");
printf("--------------------------------------------\n");
for(i=0; i < n; i++)
{
slope = f(x0, y0);
yn = y0 + h * slope;
printf("%d\t%.4f\t%.4f\t%0.4f\t%.4f\n",step,x0,y0,slope,yn);
y0 = yn;
x0 = x0+h;
step=step+1;
}
printf("\nValue of y at x = %0.4f is %0.4f",xn, yn);
}
Out-Put:
Enter Initial Condition
x0 = 0
y0 = 1
Enter calculation point xn = 1
Enter number of steps(n): 10

Step x0 y0 slope yn
-----------------------------------------------
0 0.0000 1.0000 1.0000 1.1000
1 0.1000 1.1000 1.2000 1.2200
2 0.2000 1.2200 1.4200 1.3620
3 0.3000 1.3620 1.6620 1.5282
4 0.4000 1.5282 1.9282 1.7210
5 0.5000 1.7210 2.2210 1.9431
6 0.6000 1.9431 2.5431 2.1974
7 0.7000 2.1974 2.8974 2.4872
8 0.8000 2.4872 3.2872 2.8159
9 0.9000 2.8159 3.7159 3.1875

Value of y at x = 1.0000 is 3.1875


Question9: Write and run the program in any software to find an approximate value of
𝒅𝒚
𝒚(𝟎. 𝟐) , in steps of 0.1, of the IVP = 𝒙 + 𝒚𝟐 𝒈𝒊𝒗𝒆𝒏 𝒚 = 𝟏 𝒘𝒉𝒆𝒏 𝒙 = 𝟎 by the Runge-
𝒅𝒙
Kutta 2nd order method correct up to 3D places.
C-Code:
/* Runge Kutta Method 2nd order */
#include<stdio.h>
#define f(x,y) (x+y*y)
int main()
{
float x0, y0, xn, h, yn, k1, k2, k;
int i, n, step=0;
printf("Enter Initial Values\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps(n): ");
scanf("%d", &n);
h = (xn-x0)/n;
printf("\n-----------------------------\n");
printf("step\tx0\ty0\tyn\n");
for(i=0; i < n; i++)
{
k1 = h * (f(x0, y0));
k2 = h * (f((x0+h), (y0+k1)));
k = (k1+k2)/2;
yn = y0 + k;
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",step,x0,y0,yn);
x0 = x0+h;
y0 = yn;
step=step+1;
}
printf("\nValue of y at x = %0.4f is %0.3f",xn, yn);
}
Out-Put:
Enter Initial Values
x0 = 0
y0 = 1
Enter calculation point xn = 0.2
Enter number of steps(n): 2

-----------------------------
step x0 y0 yn
0 0.0000 1.0000 1.1155
1 0.1000 1.1155 1.2708

Value of y at x = 0.2000 is 1.271


Question10: Write and run the program in any software to find an approximate value of
𝒅𝒚
𝒚(𝟎. 𝟐) , in steps of 0.1, of the IVP = 𝒙 + 𝒚𝟐 𝒈𝒊𝒗𝒆𝒏 𝒚 = 𝟏 𝒘𝒉𝒆𝒏 𝒙 = 𝟎 by the Runge-
𝒅𝒙
Kutta 4th order method correct up to 3D places.
Code:
/* Runge Kutta Method 4th order */
#include<stdio.h>
#define f(x,y) (x+y*y)
int main()
{
float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
int i, n, step=0;
printf("Enter Initial Values\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps(n): ");
scanf("%d", &n);
h = (xn-x0)/n;
printf("\n-----------------------------\n");
printf("step\tx0\ty0\tyn\n");
for(i=0; i < n; i++)
{
k1 = h * (f(x0, y0));
k2 = h * (f((x0+h/2), (y0+k1/2)));
k3 = h * (f((x0+h/2), (y0+k2/2)));
k4 = h * (f((x0+h), (y0+k3)));
k = (k1+2*k2+2*k3+k4)/6;
yn = y0 + k;
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",step,x0,y0,yn);
x0 = x0+h;
y0 = yn;
step=step+1;
}
printf("\nValue of y at x = %0.4f is %0.3f",xn, yn);
}
Out-Put:
Enter Initial Values
x0 = 0
y0 = 1
Enter calculation point xn = 0.2
Enter number of steps(n): 2

-----------------------------
step x0 y0 yn
0 0.0000 1.0000 1.1165
1 0.1000 1.1165 1.2736

Value of y at x = 0.2000 is 1.274

You might also like