0% found this document useful (0 votes)
21 views

3rd Sem CC-6 Project

I want read to learn
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)
21 views

3rd Sem CC-6 Project

I want read to learn
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/ 37

CALCUTTA UNIVERSITY

COMPUTER SCIENCE (HONOURS)

• C.U ROLL NO. – 412-1111-0279-21


C.U REGISTRATION NO. – 213412-21-0036
SUBJECT – CMSA(H)
PAPER CODE – CC 6
SEMESTER – III
SESSION – 2023-24

1
INDEX
SERIAL NO. TOPICS PAGE NO. SIGNATURE

1 TRAPEZOIDAL RULE 3-5

2 SIMPSON RULE 6-8

3 REGULA FALSI 9-12

METHOD

4 NEWTON RAPHSON 13-16

METHOD

5 SECANT METHOD 17-19

6 EULER’S METHOD 20-22

7 RUNGE KUTTA 4TH 23-25

ORDER METHOD

8 GAUSS ELIMINATION 26-30

METHOD

9 GAUSS JACOBI 31-33

METHOD

10 GAUSS SEIDEL 34-37

METHOD

11 BISECTION METHOD 38-

2
𝟐 𝟐)𝒅𝒙

QUESTION 1: WRITE A PROGRAM IN C TO EVALUATE ∫𝟏 𝟏/(𝟏 + 𝒙

USING TRAPEZOIDAL RULE WITH 6 SUB INTERVALS.

ALGORITHM:
1. Input the upper limit (a), lower limit (b), and number of intervals (n).

2. Calculate the step size (h): h = (b - a) / n.

3. Calculate the function values at the endpoints: y0 = f(a), yn = f(b).

4. Initialize the variable x to a + h.

5. Initialize the odd term summation (s) to 0.

- For i = 1 to n - 1 with a step of 2:

- Calculate y = f(x).

- Add y to the summation s.

- Update x to x + 2h.

6. Initialize the even term summation (s0) to 0.

- Set x to a + 2h.

- For i = 2 to n - 2 with a step of 2:

- Calculate y = f(x).

- Add y to the summation s0.

- Update x to x + 2h.

7. Calculate the result using Simpson's 1/3 rule formula:

- r = (h / 3) * ((y0 + yn) + 4s + 2s0).

3
8. Output the result (r).

CODE:

#include<stdio.h> int

main()

float a,b,x,y,yn,y0,s,r,h;

int i,n;

float f(float);

printf("\nEnter the upper limit:");

scanf("%f", &a);

printf("\nEnter the lower limit:");

scanf("%f", &b);

printf("\nEnter the interval:");

scanf("%d",

&n); h=(b-a)/n;

y0=f(a); yn=f(b);

x=a+h;

s=0;

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

4
{

y=f(x);

s=s+y;

x=x+h;

r=(h/2)*((y0+yn)+(2*s));

printf("\nThe result is : %f", r);

return 0;

float f(float x)

return 1/(1+(x*x));

OUTPUT

5
𝟐
QUESTION 2: WRITE A PROGRAM IN C TO EVALUATE ∫𝟏 𝟏/(𝟏 + 𝒙)𝒅𝒙

USING SIMPSON 1/3RD RULE WITH 6 SUB INTERVALS.

ALGORITHM:
1. Get user input for upper limit (a), lower limit (b), and number of intervals (n).

2. Calculate the step size (h): h = (b - a) / n.

3. Compute function values at the endpoints:

- y0 = f(a)

- yn = f(b)

4. Initialize variable x to a + h.

5. Initialize summation variable (s) to 0.

a. For i = 1 to n - 1:

i. Calculate y = f(x).

ii. Add y to the summation s. iii. Update x to x + h.

6. Calculate the result using the trapezoidal rule formula:

- r = (h / 2) * ((y0 + yn) + 2s).

6
7. Output the result (r).

CODE:

#include<stdio.h> int

main()

float a,b,x,y,yn,y0,s,r,h;

int i,n;

float f(float);

printf("\nEnter the upper limit:");

scanf("%f", &a);

printf("\nEnter the lower limit:");

scanf("%f", &b);

printf("\nEnter the interval:");

scanf("%d",

&n); h=(b-a)/n;

y0=f(a); yn=f(b);

7
x=a+h;

s=0;

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

y=f(x);

s=s+y;

x=x+h;

r=(h/2)*((y0+yn)+(2*s));

printf("\nThe result is : %f", r);

return 0;

float f(float x)

return 1/(1+(x*x));

OUTPUT:

8
QUESTION 3: WRITE A C PROGRAM TO FIND A REAL ROOT OF THE

EQUATION 3x-1-cosx=0 USING REGULA FALSI METHOD.

ALGORITHM:
1. Initialize variables x0, x1, x2, f0, f1, f2. 2.

Input: Ensure x0 is such that F(x0) > 0.

a. Repeat until F(x0) <= 0:

i. Read x0 from the user.

3. Input: Ensure x1 is such that F(x1) < 0.

a. Repeat until F(x1) >= 0:

i. Read x1 from the user.

4. Output a table header for the iteration data.

5. Repeat until |F(x2)| <= ESP:

a. Compute f0 = F(x0), f1 = F(x1).

b. Compute x2 = x0 - (f0 * (x1 - x0) / (f1 - f0)).

c. Compute f2 = F(x2).

9
d. Display the current iteration data: x0, x1, x2, f0, f1, f2.

e. If f0 * f2 < 0, update x1 = x2; otherwise, update x0 = x2.

6. Output the final result: x2 as the approximate root.

CODE:
#include<stdio.h>

#include<math.h>

#define ESP 0.0001

#define F(x) 3*(x)-1-cos(x)

int main()

float x0,x1,x2,f0,f1,f2;

do

printf("Enter the value of x0 ");

scanf("%f", &x0);

while(F(x0)>0);

do

10
printf("Enter the value of x1 ");

scanf("%f", &x1);

while(F(x1)<0);

printf("\n________________________________________________________________

________\n");

printf("\n x0\t x1\t x2\t f0\t f1\t f2\t \n");

printf("\n________________________________________________________________

________\n");

do

f0=F(x0);

f1=F(x1);

x2=x0-(f0*(x1-x0)/(f1-f0));

f2=F(x2);

printf("\n %f %f %f %f %f %f", x0,x1,x2,f0,f1,f2);

if(f0*f2<0)

x1=x2;

else

11
{

x0=x2;

while(fabs(f2)>ESP);

printf("\n________________________________________________________________

________\n");

printf("\n\nApp.root= %f", x2);

OUTPUT:

12
QUESTION 4: WRITE A C PROGRAM TO FIND A REAL ROOT OF THE

EQUATION 𝒙𝟑 − 𝒙 − 𝟑 = 𝟎 USING NEWTON RAPHSON METHOD.

ALGORITHM:
function newtonRaphson(coef, u, x1, tolerance):

c=0

repeat

c=c+1

fx1 = 0

fdx1 = 0

// Calculate f(x1) and f'(x1) for i = u to 1 step -1:

fx1 = fx1 + coef[i] * (x1 ^ i)

fdx1 = fdx1 + coef[i] * (i * x1 ^ (i - 1))

fx1 = fx1 + coef[0]

// Update x1 using Newton-Raphson formula x2 = x1 - fx1 / fdx1

// Display iteration details print("Iteration:", c, "x1:", x1, "fx1:", fx1, "fdx1:", fdx1)

13
// Check for convergence if abs(x2 - x1) < tolerance:

break

// Update x1 for the next iteration x1 = x2

until false print("The root is:", x2)

CODE:
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int u,i=0,c=0,f=0; int

coef[10]={0}; float

x1=0,x2=0,t=0; float

fx1=0,fdx1=0; int

main()

printf("Newton Raphson"); printf("\nEnter

the total number of power: "); scanf("%d",

&u); for(i=0;i<=u;i++)

printf("\n\t x^%d::",i);

scanf("%d", &coef[i]);

14
printf("\n"); printf("The

polynomial\n");

for(i=u;i>=0;i--)

printf("%dx^%d ",coef[i],i);

printf("\nInitial x1-> ");

scanf("%f", &x1);

printf("\niteration x1 fx1 fdx1");

do {

c++;

fx1=fdx1=0;

for(i=u;i>=1;i--)

fx1+=coef[i]* (pow(x1,i));

fx1+=coef[0];

for(i=u;i>=0;i--)

fdx1+=coef[i] * (i*pow(x1,(i-1)));

15
}

t=x2;

x2=(x1-(fx1/fdx1));

x1=x2;

printf("\n%d %0.3f %0.3f %0.3f", c,x2,fx1,fdx1);

while((fabs(t-x1))>=0.0001);

printf("\nThe root is %f", x2);

return 0;

OUTPUT:

16
QUESTION 5: WRITE A C PROGRAM TO FIND A REAL ROOT OF THE

EQUATION 𝒙𝟐 − 𝟒𝒙 − 𝟏𝟎 = 𝟎 USING SECANT METHOD.

ALGORITHM:
function secantMethod(x1, x2, tolerance):

loop f1 = F(x1) f2 = F(x2) x3 = x2 - (f2 * (x2 - x1)

/ (f2 - f1)) print("x1:", x1, "x2:", x2, "x3:", x3, "f(x1):", f1,

"f(x2):", f2) x1 = x2 x2 = x3 if f2 < 0: t=

abs(f2) else: t = f2 if t <= tolerance:

break

print("Approximate root:", x3)

# Input values x1 = input("Enter the

value for x1: ") x2 = input("Enter the

value for x2: ") tolerance = 0.0001 #

Run the Secant Method

secantMethod(x1, x2, tolerance)

CODE:
#include<stdio.h>

17
#include<math.h>

#define ESP 0.0001

#define F(x) (x)*(x) - 4*(x) - 10 int

main()

float x1,x2,x3,f1,f2,t;

printf("Enter the value for x1:");

scanf("%f", &x1); printf("\nEnter the

value for x2:"); scanf("%f", &x2);

printf(" x1\t\t x2\t\t x3\t\t f(x1)\t\t f(x2)\t\t");

do

f1=F(x1);

f2=F(x2);

x3=x2-(f2*(x2-x1)/(f2-f1)); printf("%f\t

%f\t %f\t %f\t %f\t",x1,x2,x3,f1,f2);

x1=x2;

x2=x3;

if(f2<0)

t=fabs(f2);

18
else

t=f2;

while(t>ESP);

printf("\n\nApp.root = %f", x3);

return 0;

OUTPUT:

QUESTION 6: WRITE A C PROGRAM TO FIND AN APPROXIMATE VALUE

OF Y CORRESPONDING TO X=0.1, GIVEN THAT dy/dx =f(x,y) =(1+xy) ,

19
Y=2 WHEN X=0 AND H=0.001 USING EULER’S METHOD.

ALGORITHM:
function eulerMethod(x, y, h, xf):

n = (xf - x) / h

repeat n times

y = y + h * f(x, y) x=x+h return y function

f(x, y): return 1 + (x * y) # Input values x =

input("Enter the initial value of x: ") y = input("Enter

the initial value of f(x): ") h = input("Enter the value of

h: ") xf = input("Enter the value for which we have to

find: ")

# Run Euler's Method result =

eulerMethod(x, y, h, xf)

print("The result is:", result)

CODE:
#include<stdio.h> int

main()

float x,y,h,n,xf;

int i;

20
float f(float, float);

printf("Enter the initials of x:");

scanf("%f", &x);

printf("\nEnter the initials of f(x):");

scanf("%f", &y);

printf("\nEnter the value of h:");

scanf("%f", &h);

printf("\nEnter the value for which we have to find:");

scanf("%f", &xf); n=(xf-x)/h;

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

y=y+(h*f(x,y));

x=x+h;

printf("\nThe result is : %f", y);

return 0;

} float f(float x, float

y)

return 1+(x*y);

21
OUTPUT:

QUESTION 7: WRITE A C PROGRAM TO FIND AN APPROXIMATE VALUE

OF Y CORRESPONDING TO X=0.4, GIVEN THAT dy/dx =f(x,y) =𝟏 + 𝒚𝟐 ,

Y=0 WHEN X=0 AND H=0.2 USING RUNGE KUTTA 4TH ORDER

METHOD.

22
ALGORITHM:
function runRungeKutta(x0, y0, h, n):

repeat until x0 >= n f = F(x0, y0) k1 = h * f f = F(x0 + (h / 2), y0 + (k1

/ 2)) k2 = h * f f = F(x0 + (h / 2), y0 + (k2 / 2)) k3 = h * f f = F(x0 + h,

y0 + k3) k4 = h * f y1 = y0 + (k1 + 2*k2 + 2*k3 + k4) / 6 print("k1 =",

k1) print("k2 =", k2) print("k3 =", k3) print("k4 =", k4) print("y(",

x0 + h, ") =", y1) y0 = y1 x0 = x0 + h

# Input values x0 = input("Enter the value of x0: ") y0 = input("Enter the value of

y0: ") h = input("Enter the value of h: ") n = input("Enter the value of the last

point: ")

# Run the Runge-Kutta method runRungeKutta(x0,

y0, h, n)

CODE:
#include<stdio.h>

#include<math.h>

#define F(x,y) 1 + (y)*(y) int

main()

double x0,y0,y1,h,f,n,k1,k2,k3,k4;

printf("Enter the value of x0:");

scanf("%lf", &x0); printf("\nEnter the value

23
of y0:"); scanf("%lf", &y0);

printf("\nEnter the value of h:");

scanf("%lf", &h); printf("\nEnter the value

of last point:");

scanf("%lf", &n);

for(; x0<n; x0=x0+h)

f=F(x0,y0);

k1=h*f;

f=F(x0+(h/2),y0+(k1/2));

k2=h*f;

f=F(x0+(h/2),y0+(k2/2));

k3=h*f;

f=F(x0+h,y0+k3);

k4=h*f;

y1=y0+(k1 + (2*k2) + (2*k3) + k4)/6;

printf("\nk1= %.4lf", k1);

printf("\nk2= %.4lf", k2); printf("\nk3=

%.4lf", k3); printf("\nk4= %.4lf", k4);

printf("\ny(%.4lf) = %.3lf", x0+h, y1);

y0=y1;

24
}

return 0;

OUTPUT:

QUESTION 8: WRITE A C PROGRAM TO SOLVE THE FOLLOWING

SYSTEM OF THE LINEAR EQUATIONS USING GAUSS ELIMINATION

METHOD.

2x + y + z = 10

3x + 2y + 3z = 18

X + 4y + 9z = 16

ALGORITHM:

25
function gaussianElimination(mat, n):

for i from 0 to n-1:

print("Enter the coefficients and constant for equation", i+1)

for j from 0 to n: print("Enter the value of coefficient", j+1)

mat[i][j] = input() print("Your matrix\n") for i from 0 to n-1:

for j from 0 to n:

print(mat[i][j], end=" ")

print("\n") for i from 0 to n-

2:

temp = mat[i+1][0] / mat[i][0]

temp1 = mat[i+2][0] / mat[i][0]

for j from 0 to n:

mat[i+1][j] = mat[i+1][j] - (mat[i][j] * temp)

mat[i+2][j] = mat[i+2][j] - (mat[i][j] * temp1)

temp = mat[2][1] / mat[1][1] for j from 0 to n:

mat[2][j] = mat[2][j] - (mat[1][j] * temp)

print("Matrix after elimination\n") for i

from 0 to n-1: for j from 0 to n:

print(mat[i][j], end=" ") print("\n") z=

mat[2][3] / mat[2][2] y = (mat[1][3] - mat[1][2] * z) /

mat[1][1] x = (mat[0][3] - mat[0][2] * z - mat[0][1] * y) /

26
mat[0][0] print("\n\nx =", x) print("\ny =", y)

print("\nz =", z)

# Input value for the size of the matrix n

= input("Enter the size of the matrix: ") #

Create a matrix with size n x (n+1) mat =

createMatrix(n, n+1) # Run Gaussian

Elimination gaussianElimination(mat, n)

CODE:
#include<stdio.h>

#include<math.h> int

main()

float mat[4][4],temp,temp1,x,y,z;

int i,n,j; printf("Enter the size of the

matrix:"); scanf("%d", &n);

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

printf("\nEnter the value of %d equation", i+1);

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

printf("\nEnter the value of coefficient %d: ", j+1);

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

27
printf("\nEnter the value of the constant:");

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

printf("\n Your matrix \n\n");

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

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

printf(" %g " , mat[i][j]);

printf("\n\n");

temp=mat[1][0]/mat[0][0];

temp1=mat[2][0]/mat[0][0]; for(i=0,j=0;j<n+1;j++)

mat[i+1][j]=mat[i+1][j] - (mat[i][j]*temp);

mat[i+2][j]=mat[i+2][j] - (mat[i][j]*temp1);

temp=mat[2][1]/mat[1][1];

for(i=1,j=0;j<n+1;j++)

28
mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);

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

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

printf(" %.3f ", mat[i][j]);

printf("\n\n");

z=mat[2][3]/mat[2][2]; y=(mat[1][3]-

mat[1][2]*z)/mat[1][1]; x=(mat[0][3]-mat[0][2]*z-

mat[0][1]*y)/mat[0][0]; printf("\n\nx = %.3f",x);

printf("\n\ny = %.3f",y); printf("\n\nz = %.3f",z);

return 0;

OUTPUT:

29
QUESTION 9: WRITE A C PROGRAM TO SOLVE THE FOLLOWING

SYSTEM OF THE LINEAR EQUATIONS USING GAUSS JACOBI METHOD.

2x + y + z = 10

3x + 2y + 3z = 18

X + 4y + 9z = 16

ALGORITHM:
function jacobiIteration(x1, x2, x3):

y1 = (17 - 20*x2 + 2*x3) / 20 y2 = (-18 - 3*x1 + x3) / 20 y3 = (25 - 2*x1 + 3*x2) / 20

return y1, y2, y3

30
# Set the convergence tolerance ESP = 0.0001

# Initialize variables x1 = 0 x2 = 0 x3 = 0 i = 0

print("\n x1\t\t x2\t\t x3\n") repeat

y1, y2, y3 = jacobiIteration(x1, x2, x3) if abs(y1 - x1) < ESP and

abs(y2 - x2) < ESP and abs(y3 - x3) < ESP:

print("\n\nx1 =", y1) print("\n\nx2 =", y2) print("\n\nx3 =", y3)

i = 1 else x1 = y1 x2 = y2

x3 = y3 print("\n%f\t%f\t%f",

x1, x2, x3)

until i = 1 return 0

CODE:
#include<stdio.h>

#include<math.h>

#define ESP 0.0001

#define x1(x2,x3) ((17-20*(x2)+2*(x3))/20)

#define x2(x1,x3) ((-18-3*(x1)+(x3))/20)

#define x3(x1,x2) ((25-2*(x1)+3*(x2))/20) int

main()

double x1=0,x2=0,x3=0,y1,y2,y3;

int i=0;

31
printf("\n x1\t\t x2\t\t x3\n");

do

y1=x1(x2,x3);

y2=x2(x1,x3); y3=x3(x1,x2);

if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP)

printf("\n\nx1 = %.3lf",y1);

printf("\n\nx2 = %.3lf",y2); printf("\n\nx3 =

%.3lf",y3); i=1; }

else

x1=y1;

x2=y2;

x3=y3;

printf("\n%f\t%f\t%f", x1,x2,x3);

while(i!=1);

return 0;

32
OUTPUT:

QUESTION 10: WRITE A C PROGRAM TO SOLVE THE FOLLOWING

SYSTEM OF THE LINEAR EQUATIONS USING GAUSS SEIDEL METHOD.

2x + y + z = 10

3x + 2y + 3z = 18

X + 4y + 9z = 16

ALGORITHM:
function jacobiIteration(x1, x2, x3):

y1 = (17 - 20*x2 + 2*x3) / 20

y2 = (-18 - 3*x1 + x3) / 20 y3

= (25 - 2*x1 + 3*x2) / 20

return y1, y2, y3

33
# Set the convergence tolerance

ESP = 0.0001

# Initialize variables

x1 = 0 x2 = 0 x3 = 0

i=0

print("\n x1\t\t x2\t\t x3\n")

repeat y1, y2, y3 = jacobiIteration(x1,

x2, x3)

if abs(y1 - x1) < ESP and abs(y2 - x2) < ESP and abs(y3 - x3) < ESP:

print("\n\nx1 =", y1)

print("\n\nx2 =", y2) print("\n\nx3

=", y3)

i=1 else x1 = y1 x2 =

y2 x3 = y3

print("\n%f\t%f\t%f", x1, x2, x3)

34
until i = 1

return 0

CODE:
#include<stdio.h>

#include<math.h>

#define ESP 0.0001

#define x1(x2,x3) ((17-20*(x2)+2*(x3))/20)

#define x2(x1,x3) ((-18-3*(x1)+(x3))/20)

#define x3(x1,x2) ((25-2*(x1)+3*(x2))/20) int

main()

double x1=0,x2=0,x3=0,y1,y2,y3;

int i=0;

printf("\n x1\t\t x2\t\t x3\n");

do

y1=x1(x2,x3);

y2=x2(y1,x3); y3=x3(y1,y2);

if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP)

35
printf("\n\nx1 = %.3lf",y1);

printf("\n\nx2 = %.3lf",y2); printf("\n\nx3 =

%.3lf",y3); i=1;

else

x1=y1;

x2=y2;

x3=y3;

printf("\n%f\t%f\t%f", x1,x2,x3);

while(i!=1);

return 0;

OUTPUT:

36
37

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy