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

CONM C Program Lab Code

The document provides an overview of various numerical methods implemented in C language, including the Bisection Method, Regula-Falsi Method, Newton-Raphson Method, and Muller's Method. Each method is accompanied by flowcharts and example code, detailing the steps to find roots of equations and the necessary parameters such as allowed error and maximum iterations. The document emphasizes the importance of iteration tracking and convergence criteria in these numerical approaches.

Uploaded by

sbdbdshdbd274
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 views8 pages

CONM C Program Lab Code

The document provides an overview of various numerical methods implemented in C language, including the Bisection Method, Regula-Falsi Method, Newton-Raphson Method, and Muller's Method. Each method is accompanied by flowcharts and example code, detailing the steps to find roots of equations and the necessary parameters such as allowed error and maximum iterations. The document emphasizes the importance of iteration tracking and convergence criteria in these numerical approaches.

Uploaded by

sbdbdshdbd274
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

674 • NUMERICAL METHODS IN ENGINEERING AND SCIENCE

bers at any one time. Like structures, union can be declared using the key-
word union as follows:
union item
{
int m; float p; char c;
}
code;
PROGRAMS OF STANDARD METHODS IN “C” LANGUAGE

14.3 Bisection Method (Section 2.7)


Flow-chart

Start

Define function f(x)

Define function bisect

Get the values of


a, b, aerr, maxitr

Initialize itr

Call function bisect


with x, a, b, itr B
10
Yes Is No
b=x f(a)*f(x) a =x
<0?

Call function Bisect


with x1, a, b, itr B

Yes Is
20 fabs (x1 – x)
< aerr ?
No
x = x1 B

No Is Yes x = (a + b)/2.0
itr < maxitr ? 10

Print‘solution itr = itr + 1


does not converge’
20
Print itr, x1
Stop

Print itr, x1 Return


NUMERICAL METHODS USING C LANGUAGE • 675

NOTES: a, b are the limits in which the root lies


aerr is the allowed error
itr is a counter which keeps track of the number of iterations performed
maxitr is the maximum number of iterations to be performed
x is the value of root at the nth iteration
x1 is the value of root at (n + 1)th iteration.
Function Bisect:
Purpose: Performs and prints the result of one iteration
Variables: x is the result of the current iteration.
Program
/* Bisection Method */
#include <stdio.h>
#include <math.h>
float f(float x)
{
return (x*x*x - 4*x - 9);
}
void bisect(float *x,float a,float b,int *itr)
{
*x = (a + b)/2;
++(*itr);
printf("Iteration no. %3d X = %7.5f\n",*itr,*x);
}
main()
{
int itr = 0, maxitr;
float x, a, b, aerr, x1;
printf("Enter the values of a,b,"
"allowed error, maximum iterations\n");
scanf("%f %f %f %d",&a,&b,&aerr,&maxitr);
bisect(&x,a,b,&itr);
do
{
if (f(a)*f(x) < 0)
b = x;
else
a = x;
bisect (&x1,a,b,&itr);
if (fabs(x1-x) < aerr)
{
printf("After %d iterations, root <169>
"= %6.4f\n",itr,x1);
676 • NUMERICAL METHODS IN ENGINEERING AND SCIENCE

return 0;
}
x = x1;
} while (itr < maxitr);
printf("Solution does not converge,"
"iterations not sufficient");
return 1;
}
Computer Solution of Example 2.15 (a)
Enter the values of a, b, allowed error, maximum iterations
3 2.0001 20
Iteration No. 1 X = 2.50000
Iteration No. 2 X = 2.75000
Iteration No. 3 X = 2.62500
Iteration No. 4 X = 2.68750
Iteration No. 5 X = 2.71875
Iteration No. 6 X = 2.70313
Iteration No. 7 X = 2.71094
Iteration No. 8 X = 2.70703
Iteration No. 9 X = 2.70508
Iteration No. 10 X = 2.70605
Iteration No. 11 X = 2.70654
Iteration No. 12 X = 2.70630
Iteration No. 13 X = 2.70642
Iteration No. 14 X = 2.70648
After 14 iterations, root = 2.7065

14.4 Regula-Falsi Method (Section 2.8)


Flow-chart
NOTES: f(x) = 0 is the equation whose root is to be found
x0, x1 are units in which root lies
aerr is allowed error
maxitr is maximum number of iterations to be performed
itr is a counter which keeps track of the number of iterations performed
x2 is value of root at nth iteration
x3 is value of root at (n + 1)th iteration
Function Regula:
Purpose: Performs and prints the results of one iteration.
Variables: x is value of root at nth iteration
fx0, fx1 are values of f(x) at x0 and x1, respectively.
NUMERICAL METHODS USING C LANGUAGE • 677

Start

Define function f(x)

Define function regula

Get the values of


x0, x1, aerr, maxitr

Initialize itr

Callfunction Regula R
with x2, x0, x1, f(x0), f(x1), itr
10

Yes Is No
x1=x2 f(x0)*f(x2) x0=x2
<0

Call function Regula


R
with x3, x0 ,x1, f(x0), f((x1), itr

x=x0–((x1–x0)/
Is Yes (fx1–fx0))*fx0
fabs (x3–x2) 20
<aerr?
Print itr, x
No

x2=x3
Return

Yes Is
10 itr<maxitr?
20
No
Print ‘Solution does
Print solution
not converge’

Stop Stop

Program
/* Regula Falsi Method */
#include <stdio.h>
#include <math.h>
float f(float x)
678 • NUMERICAL METHODS IN ENGINEERING AND SCIENCE

{
return cos(x)-x*exp(x);
}
void regula (float *x, float x0, float x1,
float fx0, float fx1, int *itr)
{
*x = x0-((x1-x0)/(fx1-fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f\n",
*itr,*x);
}
main()
{
int itr=0, maxitr;
float x0,x1,x2,x3,aerr;
printf("Enter the values for x0,x1,"
"allowed error,maximum iterations\n•);
scanf("%f %f %f %d",&x0,&x1,&aerr,&maxitr);
regula(&x2,x0,x1,f(x0),f(x1),&itr);
do
{
if (f(x0)*f(x2) < 0)
x1 = x2;
else
x0 = x2;
regula(&x3,x0,x1,f(x0),f(x1),&itr);
if (fabs(x3-x2) < aerr)
{
printf("After %d iterations,"
"root = %6.4f\n", itr,x3);
return 0;
}
x2=x3;
} while(itr < maxitr);
printf("Solution does not converge,"
"iterations not sufficient\n");
return 1;
}
Computer Solution of Example 2.20
Enter the values for x0, x1, allowed error, maximum iterations
0 1.0001 20
NUMERICAL METHODS USING C LANGUAGE • 679

Iteration No. 1 X = 0.31467


Iteration No. 2 X = 0.44673
Iteration No. 3 X = 0.49402
Iteration No. 4 X = 0.50995
Iteration No. 5 X = 0.51520
Iteration No. 6 X = 0.51692
Iteration No. 7 X = 0.51748
Iteration No. 8 X = 0.51767
Iteration No. 9 X = 0.51773
After 9 iterations, root = 0.5177

14.5 Newton Raphson Method (Section 2.11)


Flow-chart

Start

Define function f(x)

Define function df(x)

Get the values of


x0, aerr, maxitr

Loop for it r = 1 to maxitr

h = f(x0)/df(x0)
x1 = x0 – h

Print itr, x1

Is Yes
fabs (h) < aerr Print Solution

No
Stop
x0 = x1

End Loop (itr)

Print ‘Solution
does not converge’.

Stop
680 • NUMERICAL METHODS IN ENGINEERING AND SCIENCE

NOTES: F(x) = 0 is the equation whose root is to be found


df(x) is the derivatives of f(x) w.r.t. x
x0 is value of root of nth iteration
x1 is value of root of (n + 1)th iteration
aerr is allowed error
maxitr is maximum number of iterations to be performed
itr is a counter which keeps track of the number of iterations performed.
Program
/* Newton Raphson Method */
#include <stdio.h>
#include <math.h>
float f(float x)
{
return x*log10(x)-1.2;
}
float df(float x)
{
return log10(x) + 0.43429;
}
main()
{
int itr,maxitr;
float h,x0,x1,aerr;
printf("Enter x0,allowed error,"
"maximum iterations\n");
scanf("%f %f %d",&x0,&aerr,&maxitr);
for (itr=1;itr<=maxitr;itr++)
{
h = f(x0)/df(x0);
x1 = x0-h;
printf("Iteration no. %3d,"
"x = %9.6f\n",itr,x1);
if (fabs(h) < aerr)
{
printf("After %3d iterations,"
"root = %8.6f\n", itr,x1);
return 0;
}
x0 = x1;
}
printf("Iterations not sufficient,"
NUMERICAL METHODS USING C LANGUAGE • 681

"solution does not converge\n");


return 1;
}
Computer Solution of Example 2.32
Enter x0, allowed error, maximum iterations
2.000001 10
Iteration No. 1 X = 2.813170
Iteration No. 2 X = 2.741109
Iteration No. 3 X = 2.740646
Iteration No. 4 X = 2.740646
After 4 iterations, root = 2.740646

14.6 Muller’s Method (Section 2.13)


Flow-chart
Start

Define function y(x)

Get initial approxi-


mations in array x

Get values of
aerr, maxitr

Loop for itr = 1 to maxitr

Calculate li, di, mu, s

Yes
Is mu < 0 1 = (2*y(x[i])*di)/(– mu + s)

No
1 = (2*y(x[I])*di)/(– mu – s)

x[1 +1] = x[I] + 1*(x[I] – x[I – 1])

Print itr, x[I + 1]

Is
Yes
fabs (x[I + 1] – x[I]) 20
< aerr ?
No
Loop for i = I – 2 to 2

x[i] = x[i + 1]

20
End Loop (i)

End Loop (itr)


Print
Solution
Print ‘Solution
does not converge’.

Stop

You might also like