Experiment No. – 1 Date: 03.03.
2025
Aim of the experiment: Finding a root real root of the equation by Bisection Method.
Algorithm
Step 1: Read a, b, e
[ a, b being two initial values which brackets the desired root and epsilon is
allowable error. ]
Step 2: Compute f0=f(a)
Step 3: Compute f1=f(b)
Step 4: Check if f0*f1<0 then go to Step 5
Else stop
Step 5: Compute x= (a+b)/2
Step 6: Compute f2=f(x)
Step 7: Check if f0*f1>0 then set a=x
Else set b=x
Step 8: Check if (b-a)>e then go to step 2 else go to next step.
Step 9: Write a, b, f2
Step 10: Stop
Program Coding
// Program: Finding real roots of equation using Bisection Method
#include<stdio.h>
#include<math.h>
#define f(x) x*x*x+x*x-1
void main()
{
float a, b, x, f0, f1, f2, e;
int step = 1;
up:
printf("\nEnter the 1st initial guess: ");
scanf("%f", &a);
printf("\nEnter the 2nd initial guess: ");
scanf("%f", &b);
printf("Enter tolerable error:");
scanf("%f", &e);
f0 = f(a);
f1 = f(b);
if( f0 * f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
printf("\nStep\t\ta\t\t\tb\t\t\tx\t\t\tf(x)\n");
do
{
x = (a + b)/2;
f2 = f(x);
printf("%d\t\t%f\t%f\t%f\t%f\n",step, a, b, x, f2);
if( f0 * f2 < 0)
{
b = x;
f1 = f2;
}
else
{
a = x;
f0 = f2;
}
step = step + 1;
}while(fabs(a-b)>e);
printf("\nRoot is: %f", x);
}
Name:
Roll No:
Course:
Semester:
Experiment No. – 2 Date: 04.03.2025
Aim of the experiment: Finding a root real root of the equation by Regula Falsi Method.
Algorithm
Step 1: Read x0, x1, e
[ x0, x1 being two initial values which brackets the desired root and epsilon is
allowable error. ]
Step 2: Compute f0=f(x0)
Step 3: Compute f1=f(x1)
Step 4: For i=1 to n in steps of 1 do
begin
Step 5: Compute x2=(x0f1-x1f0)/(f1-f0)
Step 6: Compute f2=f(x2)
Step 7: if f0.f2<0 then
Begin
Set x1=x2
Set f1=f2
Else
Begin
Set x0=x2
Set f0=f2
Step 8: Write “Does not convergent in n iterations.
Step 9: Write x2, f2
Step 10: Stop
Program Coding
// Program: Finding real roots of nonlinear equation using Regula Falsi Method
#include<stdio.h>
#include<math.h>
#define f(x) x*x*x-3*x-5
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
up:
printf("\nEnter the 1st initial guess:");
scanf("%f", &x0);
printf("\nEnter the 2nd initial guess:");
scanf("%f ", &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
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);
}
Name:
Roll No:
Course:
Semester:
Experiment No. – 3 Date: 07.03.2025
Aim of the experiment: Finding a root real root of the equation by Secant Method.
Algorithm
Step 1: Read x0, x1, e
[ x0, x1 being two initial values which brackets the desired root and epsilon is
allowable error. ]
Step 2: Compute f0=f(x0)
Step 3: Compute f1=f(x1)
Step 4: For i=1 to n in steps of 1 do
begin
Step 5: Compute x2=(x0f1-x1f0)/(f1-f0)
Step 6: Compute f2=f(x2)
Step 7: Set x0=x1
Set f0=f21
Set x1=x2
Set f1=f2
Step 8: Check if (x2-1)>e then go to step 7 else go to next step.
Step 9: Write x2, f2
Step 10: Stop
Program Coding
// Program: Finding real roots of nonlinear equation using Secant Method
#include<stdio.h>
#include<math.h>
#define f(x) x*x*x-3*x-5
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
up:
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.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);
x1 = x2;
f1 = f2;
x0 = x1;
f0 = f1;
step = step + 1;
}while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
}
Name:
Roll No:
Course:
Semester:
Experiment No. – 4 Date: 10.03.2025
Aim of the experiment: Finding a root real root of the equation by Newton Raphson Method.
Algorithm
Step 1: Read x0, e
[ x0 being initial guess of a root ,epsilon is allowable error and n the maximum number of
iteration ]
Step 2: Set i= 0
Step 3: Compute f0=f(x0)
Step 4: Compute f 10=f1(x0)
Step 5: Compute x1=(x0 – f0)/f1(0)
Step 6: Compute i=i+1
Step 7: Check (x1-x0)<e, then print the root is x1, f(x1) and stop
Else if i<=n then set x0=x1 and go to step 3
Else go to step 8
Step 8: Write “ Does not converge in n iterations”
Step 9: Stop
Program Coding
/* Program: Finding real roots of nonlinear equation using Newton Raphson Method */
#include<stdio.h>
#include<math.h>
#define f(x) x*x+2*x-2
#define g(x) 2*x+2
void main()
{
float x0, x1, f0, f1, g0, 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
{
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
{
printf("Mathematical Error.");
exit(0);
}
x1 = x0 - f0/g0;
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);
Name:
Roll No:
Course:
Semester:
Experiment No. – 5 Date: 15.03.2025
Aim of the experiment: Evaluating an integral by Trapezoidal rule.
Algorithm
Step 1: Read a, b, n
Step 2: Compute h=(b – a )/n
Step 3: Set x=0
Step 4: Set Sum=0
Step 5:While x < b Do [loop for finding Sum]
Begin
Compute x=x+h
Compute Sum= Sum+f(x)
End
Step 6: Compute result=(h/2)[f(a)+f(b)+2*Sum]
Step 7: While result, n.
Program Coding
/* Program: Finding the integral value by Trapezoidal Rule*/
#include<stdio.h>
#include<math.h>
/* Define the function to be integrated here: */
#define f(x) 4*x-3*x*x
void main()
{
int n,i;
float a,b,h,x,sum=0,result;
printf("\nEnter the lower limit of the integral: ");
scanf("%f",&a);
printf("\nEnter the upper limit of the integral: ");
scanf("%f",&b);
printf("\nEnter the no. of sub-intervals: ");
scanf("%d",&n);
h=(b-a)/n;
for(i=1;i<n;i++)
{
x=a+i*h;
sum=sum+f(x);
}
result=(h/2)*(f(a)+f(b)+2*sum);
printf("\nThe integral is: %f\n",result);
}
Name:
Roll No:
Course:
Semester:
Experiment No. – 6 Date: 16.03.2025
Aim of the experiment: Evaluating an integral by Simpson’s 1/3 rule.
Algorithm
Step 1: Read a, b, n
Step 2: Compute h=(b – a )/n
Step 3: Set x=0
Step 4: Set Sum=0
Step 5:While x < b Do [loop for finding Sum]
Begin
Compute x=x+h
Is (i %2==0)
Compute Sum= Sum+2*f(x)
Else
Compute Sum= Sum+4*f(x)
End
Step 6: Compute result=(h/3)[f(a)+f(b)+Sum]
Step 7: While result, n.
Program Coding
/* Program: Finding the integral value by Simpson’s 1/3 Rule*/
#include<stdio.h>
#include<math.h>
/* Define the function to be integrated here: */
#define f(x) 1/(1+x*x)
void main()
{
int n,i;
float a,b,h,x,sum=0,result;
printf("\nEnter the lower limit of the integral: ");
scanf("%f",&a);
printf("\nEnter the upper limit of the integral: ");
scanf("%f",&b);
printf("\nEnter the no. of sub-intervals: ");
scanf("%d",&n);
h=(b-a)/n;
for(i=1;i<n;i++)
{
x=a+i*h;
if(i%2==0)
sum=sum+2*f(x);
else
sum=sum+4*f(x);
}
result=(h/3)*(f(a)+f(b)+sum);
printf("\nThe integral is: %f\n",result);
}
Name:
Roll No:
Course:
Semester:
Experiment No. – 7 Date: 17.03.2025
Aim of the experiment: Solving system of linear equation by Gauss Jacobi Method.
Algorithm
Step 1: Start
Step 2: Arrange given system of linear equations in diagonal dominant form
Step 3: Read tolerable error(e)
Step 4: Convert the first equation in terms of first variable, second equation in terms of
second variable and so on.
Step 5: Set initial guesses for x0, y0, z0 and so on
Step 6: Substitute values of x0, y0, z0 from step 5 in equation obtained in step 4 to calculate
new values x1,y1,z1 and so on
Step 7: if x1-x0 >e , y1-y0>e and z1-z0 > e and so on then go to step 9
Step 8 : Set x0=x1, y0=y1 z0=z1 and so on and go to step 6
Step 9: Print value of x1, y1, z1 and so on
Step 10: Stop
Program Coding
//Solving system of linear equation by Gauss Jacobi Method
#include<stdio.h>
#include<math.h>
/* Arrange systems of linear equations to be solved in diagonally dominant form
8x + 2y - 2z = 8
2x + y + 9z = 12
x - 8y - 3z = -4
Arranging given system of linear equations in diagonally dominant form:
8x + 2y - 2z = 8
x - 8y - 3z = -4
2x + y + 9z = 12
*/
#define f1(x,y,z) (8-2y+2z)/8
#define f2(x,y,z) (-4-x+3z)/-8
#define f3(x,y,z) (12-2x-y)/9
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(x0,y0,z0);
z1 = f3(x0,y0,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);
}
Name:
Roll No:
Course:
Semester:
Experiment No. – 8 Date: 18.03.2025
Aim of the experiment: Solving system of linear equation by Gauss Seidal Method.
Algorithm
Step 1: Start
Step 2: Arrange given system of linear equations in diagonal dominant form
Step 3: Read tolerable error(e)
Step 4: Convert the first equation in terms of first variable, second equation in terms of
second variable and so on.
Step 5: Set initial guesses for x0, y0, z0 and so on
Step 6: Substitute values of x0, y0, z0 from step 5 in 1st equation obtained in step 4 to
calculate new values x1. Use x1,y0,z0 in 2nd equation obtained in step 4 to calculate new
value of y1. Similarly use x1,y1 and z0 to find new z1 and so on
Step 7: if x1-x0 >e , y1-y0>e and z1-z0 > e and so on then go to step 9
Step 8 : Set x0=x1, y0=y1 z0=z1 and so on and go to step 6
Step 9: Print value of x1, y1, z1 and so on
Step 10: Stop
Program Coding
//Solving system of linear equation by Gauss Seidal Method
#include<stdio.h>
#include<math.h>
/* Arrange systems of linear equations to be solved in diagonally dominant form
8x + 2y - 2z = 8
2x + y + 9z = 12
x - 8y - 3z = -4
Arranging given system of linear equations in diagonally dominant form:
8x + 2y - 2z = 8
x - 8y - 3z = -4
2x + y + 9z = 12
*/
#define f1(x,y,z) (8-2y+2z)/8
#define f2(x,y,z) (-4-x+3z)/-8
#define f3(x,y,z) (12-2x-y)/9
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);
}
Name:
Roll No:
Course:
Semester: