Professional Documents
Culture Documents
0 - CM Practical File - PDF - 20231122 - 130527 - 0000 3
0 - CM Practical File - PDF - 20231122 - 130527 - 0000 3
COMPUTATIONAL METHODS
ES-251
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Total Marks
obtained out of
100=k
Marks out of
10,L=k/10
Marks out of
40,M=L*4
PROGRAM 1
AIM
Program for finding roots of f(x)=0 by bisection method.
THEORY
The bisection method is used to find the roots of a polynomial equation. It
separates the interval and subdivides the interval in which the root of the
equation lies. The principle behind this method is the intermediate
theorem for continuous functions. It works by narrowing the gap between
the positive and negative intervals until it closes in on the correct answer.
This method narrows the gap by taking the average of the positive and
negative intervals. It is a simple method and it is relatively slow. The
bisection method is also known as interval halving method, root-finding
method, binary search method or dichotomy method.
Let us consider a continuous function “f” which is defined on the closed
interval [a, b], is given with f(a) and f(b) of different signs. Then by
intermediate theorem, there exists a point x belong to (a, b) for which f(x) = 0.
Advantages of Bisection Method:
● Guaranteed convergence. The bracketing approach is known as the
bisection method, and it is always convergent.
● Errors can be managed. Increasing the number of iterations in the
bisection method always results in a more accurate root.
● The bisection method is simple and straightforward to programme on a
computer.
● In the case of several roots, the bisection procedure is quick
Disadvantages of Bisection Method:
● Although the Bisection method's convergence is guaranteed, it is often
slow.
● Choosing a guess that is close to the root may necessitate numerous
iterations to converge.
● Its rate of convergence is linear. It is incapable of determining complex
roots.
● If the guess interval contains discontinuities, it cannot be used.
● It cannot be applied over an interval where the function returns values
of the same sign.
ALGORITHM
Step-1. Start of the program.
Step-2. Input the variable x1, x2 for the task.
Step-3. Check f(x1)*f(x2)<0
Step-4.If yes proceed
Step-5.If no exit and print error message
Step-6.Repeat 7-11 if condition not satisfied
Step-7. x0=(x1+x2)/2
Step-8. If f(x0)*f(x1)<0
Step-9. x2=x0
Step- . Else
10 . x1=x0
Step- . Condition:
11 . if | (x1-x2)/x1) | < maximum possible error or f(x0)=0
Step- . Print output
12 . End of program.
Step-
13
Step-
14
Step-
15
CODE:
#include <math.h>
#include <stdio.h>
printf("%lf\t", mid);
if (fabs(f_mid) <tol) {
return mid;
}
if (f(x1) * f_mid< 0) {
return bisection(x1, mid, tol);
} else {
return bisection(mid, x2, tol);
}
}
int main() {
double x1, x2;
printf("enter first assumption x1: ");
scanf("%lf", &x1);
printf("enter first assumption x2: ");
scanf("%lf", &x2);
double tol = 1e-8; // tolerance for the root
printf("\n#program by Yash");
return 0;
}
OUTPUT
RESULT
Hence, we found roots of f(x)=0 with the help of bisection method.
GRAPH
Equation: x^2-30 =0
PROGRAM 2
AIM:
To find the roots of f(x)=0 using secant method.
THEORY:
Secant method is also a recursive method for finding the root for the
polynomials by successive approximation. It’s similar to the Regular-falsi
method but here we don’t need to check f(x1)f(x2)<0 again and again
after every approximation. In this method, the neighbourhoods roots are
approximated by secant line or chord to the function f(x). It’s also
advantageous of this method that we don’t need to differentiate the given
function f(x), as we do in Newton raphson method.
double f(double x) {
return x*x*x - x -1;
}
double secant(double x1, double x2, double tol) {
double x3 = x2 - f(x2) * ((x2 - x1)/(f(x2) - f(x1)));
printf("%lf\n", x3);
if(fabs(f(x3)) <tol) {
return x3;
} else {
return secant(x2, x3, tol);
}
}
int main() {
double x1, x2;
printf("enter first assumption x1: ");
scanf("%lf", &x1);
printf("enter first assumption x2: ");
scanf("%lf", &x2);
RESULT:
We found roots of f(x)=0 using secant method.
GRAPH:
PROGRAM 3
AIM:
Program for finding roots of f(x)=0 by Newton Raphson Method.
THEORY:
Newton Raphson Method or Newton Method is a powerful technique for
solving equations numerically. It is most commonly used for
approximation of the roots of the real-valued functions. Newton Rapson
Method was developed by Isaac Newton and Joseph Raphson, hence the
name Newton Rapson Method.
Newton Raphson Method involves iteratively refining an initial guess to
converge it toward the desired root. However, the method is not
efficient to calculate the roots of the polynomials or equations with
higher degrees but in the case of small-degree equations, this method
yields very quick results.
In the general form, the Newton-Raphson method formula is written as
follows:
xn = xn-1 – f(xn-1)/f'(xn-1)
Where,
● xn-1 is the estimated (n-1)th root of the function,
● f(xn-1) is the value of the equation at (n-1)th estimated root, and
● f'(xn-1) is the value of the first order derivative of the equation or
function at xn-1.
ALGORITHM:
ST EP-. Start
1 ST. Define function as f(x)
EP- 2. Define first derivative of f(x) as g(x)
ST EP-. Input initial guess (x0), tolerable error (e) and maximum iteration (N)
3 ST. Initialize iteration counter i = 1
EP- 4
ST EP-
5
STEP-6. If g(x0) = 0 then print "Mathematical Error" and goto (12) otherwise
goto (7)
STEP-7. Calculate x1 = x0 - f(x0) / g(x0)
STEP-8. Increment iteration counter i = i + 1
STEP-9. If i>= N then print "Not Convergent" and goto (12) otherwise goto (10)
STEP-10. If |f(x1)| > e then set x0 = x1 and goto (6) otherwise goto (11) STEP-
11. Print root as x1
STEP-12. Stop
CODE:
#include <math.h>
#include <stdio.h>
int main() {
double x0, x1, e;
intmaxIterations;
// Newton-Raphson method
int iteration = 0;
printf("\niterations\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do {
x1 = x0 - f(x0) / df(x0);
return 0;
}
OUTPUT:
RESULT:
Hence, we found roots of f(x)=0 with the help of Newton raphson
method.
GRAPH:
PROGRAM 4
AIM: To Implement Lagrange’s Interpolation formula.
THEORY:
Interpolation is a method of finding new data points within the range of a
discrete set of known data points In other words interpolation is the
technique to estimate the value of a mathematical function, for any
intermediate value of the independent variable.
we can apply the Lagrange’s interpolation formula to get our solution.
The Lagrange’s Interpolation formula:
If, y = f(x) takes the values y0, y1, … ,yn corresponding to x = x0, x1 , … ,
xn
then,
ALGORITHM:
STEP-1. Start
STEP-2. Read number of data (n)
STEP-3 Read data Xi and Yi for i=1 to n
STEP-4. Read value of independent variables say xp whose corresponding
value of dependent say yp is to be determined.
STEP-5. Initialize: yp = 0
STEP-6. For i = 1 to n
Set p = 1
For j =1 to n
If i ≠ j then
Calculate p = p * (xp - Xj)/(Xi - Xj)
End If
Next j
Calculate yp = yp + p * Yi
Next i
STEP-7. Display value of yp as interpolated value
STEP-8. Stop.
CODE:
#include<stdio.h>
void main()
{
printf("\ncode written by yash\n");
float x[100], y[100], xp, yp=0, p;
int i,j,n;
printf("Enter number of data: ");
scanf("%d", &n);
printf("Enter data:\n");
for(i=1;i<=n;i++){
printf("x[%d] = ", i);
scanf("%f", &x[i]);
printf("y[%d] = ", i);
scanf("%f", &y[i]);}
printf("Enter interpolation point: ");
scanf("%f", &xp);
for(i=1;i<=n;i++){
p=1;
for(j=1;j<=n;j++) {
if(i!=j){
p = p* (xp - x[j])/(x[i] - x[j]);}
}
yp = yp + p * y[i];
}
printf("Interpolated value at %.3f is %.3f.", xp, yp);
}
OUTPUT:
RESULT:
Hence, we found the value of y(i) at x(i) i.e interpolation point using
Lagrange Interpolation Formulae.
PROGRAM 5
method THEORY
Newton’s Divided Difference Formula eliminates the drawback of
recalculation and recomputation of interpolation coe cients by using
Newton’s general interpolation formula which uses “divided differences”.
Before going through the source code for Newton Divided Difference in C,
here’s a brief explanation of what divided differences are with the formula
for divided differences.
ALGORITHM
Step-1. Input:
x: an array of distinct x-values (data points).
y: an array of corresponding y-values (function values).
n: the number of data points (size of arrays).
Step-2. Initialize: Create an array f of size n, where f[i] will represent the
divided difference coefficients.
Step-3. Forward Divided Difference:
For i in the range n, set f[i] equal to y[i].
Step-4. Compute Divided Difference Coefficients:
For i from 1 to n-1, do the following:
For j from n-1 to i, update f[j]
Step-5. Interpolation:
To interpolate a value at a point x0, calculate the result result as
follows: Initialize result to f[0].
For i from 1 to n-1
Step-6. Output:
result will be the interpolated value at x0.
CODE
#include<stdio.h>
void main()
{
printf("Yash jain\n");
printf("Roll no.05114803122");
int x[10], y[10], p[10];
int k,f,n,i,j=1,f1=1,f2=0;
printf("\nEnter the number of observations:\n");
scanf("%d", &n);
f=y[1];
printf("\nEnter the value of 'k' in f(k) you want to evaluate:\n");
scanf("%d", &k);
do
{
for (i=1;i<=n-1;i++)
{
p[i] = ((y[i+1]-y[i])/(x[i+j]-x[i]));
y[i]=p[i];
}
f1=1;
for(i=1;i<=j;i++)
{
f1*=(k-x[i]);
}
f2+=(y[1]*f1);
n--;
j++;
}
while(n!=1);
f+=f2;
printf("\nf(%d) = %d", k , f);
}
OUTPUT
RESULT
● Hence, for f(7) the answer is 10 using newtons divided difference method
PROGRAM 6
int main()
{
printf("Yash Jain\n");
printf("Roll no.05114803122\n");
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
RESULT
● Hence Result is 0.784
PROGRAM 7
THEORY
It is also known as Simpson's Rule where the rule says:
where f(x) is the integrand, a is the lower limit, and b is the upper limit of integration in
the expression.
ALGORITHM
Step-1. Start of the program.
Step-2. Define Function f(x)
Step-3. Input lower limit, upper limit, sub_interval
Step-4. Calculate: step_size = (lower_limit-upper limit)/sub_interval
Step-5. Calculate: integration f(lower limit) += f(lower_limit) f(upper limit)
Step-6. Set: i=1
int main()
{
printf("Yash jain\n");
printf("Roll no.05114803122\n");
float lower, upper, integration=0.0, stepSize,
k; int i, subInterval;
RESULT
● Hence required value of integration is 0.785