Professional Documents
Culture Documents
PROGRAM 1
THEORY
The Newton-Raphson method is a numerical technique for finding the roots of a real-valued
function. It is based on the idea that a continuous and differentiable function can be
approximated by a straight-line tangent to it at a given point. The method iteratively improves an
initial guess of the root by using the formula:
x<sub>n+1</sub> = x<sub>n</sub> - f(x<sub>n</sub>)/f’(x<sub>n</sub>)
where x<sub>n</sub> is the current approximation, f(x<sub>n</sub>) is the value of the
function at x<sub>n</sub>, and f’(x<sub>n</sub>) is the derivative of the function at
x<sub>n</sub>. The method converges to the root if the initial guess is sufficiently close and the
function satisfies certain conditions.
ALGORITHM
1. Start
2. Read x, e, n, d
*x is the initial guess
e is the absolute error i.e the desired degree of accuracy
n is for operating loop
d is for checking slope*
3. Do for i =1 to n in step of 2
4. f = f(x)
5. f1 = f'(x)
6. If ( [f1] < d), then display too small slope and goto 11.
*[ ] is used as modulus sign*
7. x1 = x – f/f1
8. If ( [(x1 – x)/x1] < e ), the display the root as x1 and goto 11.
*[ ] is used as modulus sign*
Page |2
11. Stop
CODE
#include<iostream>
#include<cmath>
double func(double x)
double derivFunc(double x)
void newtonRaphson(double x)
h = func(x)/derivFunc(x);
Page |3
x = x - h;
int main()
newtonRaphson(x0);
return 0;
OUTPUT
Page |4
PROGRAM 2
THEORY
The Bisection Method is a simple and robust root-finding method in mathematics. It applies to
any continuous function for which one knows two values with opposite signs. This method is
also known as interval halving method, root-finding method, binary search method or dichotomy
method2. It’s a relatively slow method, but it’s very simple and robust1 . It’s often used to obtain a
rough approximation to a solution which is then used as a starting point for more rapidly
converging methods.
ALGORITHM
1. Start
4. If (f1*f2) > 0, then display initial guesses are wrong and goto (11).
Otherwise, continue.
5. x = (x1 + x2)/2
7. Else, f = f(x)
9. Else, x2 = x and f2 = f.
11. Stop
CODE
#include<iostream>
#include<cmath>
using namespace std;
float f(float x)
{
return (x*x*x - 5*x + 1);
}
int main()
{
float a,b,c;
cout<<"Enter the value of a and b: ";
cin>>a>>b;
if(f(a)*f(b)>0)
{
cout<<"Wrong choice of a and b";
return 0;
}
do
{
c=(a+b)/2;
if(f(c)==0)
{
Page |6
OUTPUT
Page |7
PROGRAM 3
THEORY
The secant method is a root-finding procedure in numerical analysis that uses a series of roots of
secant lines to better approximate a root of a function. The method can be thought of as a finite-
difference approximation of Newton’s method. The secant method converges to a root if the
initial values x0 and x1 are close enough to the root. The order of convergence is given by φ.
This solution is only valid under certain technical requirements, such as f being two times
continuously differentiable and the root being simple in the question (i.e., having multiplicity 1).
There is no certainty that the secant method will converge if the beginning values are not close
enough to the root
ALGORITHM
1. Start
7. Stop
Page |8
CODE
#include<iostream>
#include<cmath>
using namespace std;
float f(float x)
{
return (x*x*x - 2*x - 5);
}
int main()
{
float x0, x1, x2, e;
cout<<"Enter the value of x0: ";
cin>>x0;
cout<<"Enter the value of x1: ";
cin>>x1;
cout<<"Enter the value of e: ";
cin>>e;
do
{
x2 = (x0*f(x1) - x1*f(x0))/(f(x1) - f(x0));
x0 = x1;
x1 = x2;
}while(fabs(f(x2)) > e);
cout<<"The root of the equation is: "<<x2;
Page |9
return 0;
}
OUTPUT
P a g e | 10
PROGRAM 4
THEORY
The Newton interpolation method, also known as Newton’s divided differences interpolation
polynomial, is a technique used in numerical analysis and mathematics for interpolating data
points.
Given a set of k + 1 data points where no two xj are the same, the Newton interpolation
polynomial is a linear combination of Newton basis polynomials. The coefficients of the
polynomial are calculated using Newton’s divided differences method.
The Newton polynomial can be expressed in a simplified form when the data points are arranged
consecutively with equal spacing. This is called the Newton forward divided difference
formula. If the nodes are reordered, the Newton polynomial becomes the Newton backward
divided difference formula.
ALGORITHM
1. Start
2. input the number of data points
3. input the data points
4. input the value of x for which y is to be calculated
5. calculate the divided difference table
6. calculate the value of y
7. print the value of y
8. stop
CODE
#include<iostream>
using namespace std;
P a g e | 11
int main()
{
int n;
cout<<"Enter the size of array: ";
cin>>n;
int arr[n];
P a g e | 12
OUTPUT
P a g e | 13
PROGRAM 5
THEORY
Lagrange’s method, also known as the method of Lagrange multipliers, is a strategy used in
mathematical optimization to find the local maxima and minima of a function subject to equality
constraints. This means that one or more equations have to be satisfied exactly by the chosen
values of the variables.
The primary idea behind this method is to transform a constrained problem into a form so that
the derivative test of an unconstrained problem can be applied. The relationship between the
gradient of the function and gradients of the constraints leads to a reformulation of the original
problem, known as the Lagrangian function.
The Lagrangian function is defined as follows: ℒ(x, λ) = f(x) – λg(x), where ℒ is the Lagrange
function of the variable x and λ is the Lagrange multiplier 1 .
ALGORITHM
1. Start
2. Read number of data (n)
3. Read data Xi and Yi for i=1 ton n
4. Read value of independent variables say xp
whose corresponding value of dependent say yp is to be determined.
5. Initialize: yp = 0
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
P a g e | 14
Calculate yp = yp + p * Yi
Next i
7. Display value of yp as interpolated value.
8. Stop
CODE
#include<iostream>
using namespace std;
int main()
{
int n,i,j;
float x[10],y[10],a,s=1,t=1,k=0;
cout<<"Enter the number of data:\n";
cin>>n;
cout<<"Enter the data:\n";
for(i=0; i<n; i++)
{
cout<<"x["<<i<<"]=";
cin>>x[i];
cout<<"y["<<i<<"]=";
cin>>y[i];
}
cout<<"Enter the value of x to find y:\n";
cin>>a;
for(i=0; i<n; i++)
{
P a g e | 15
s=1;
t=1;
for(j=0; j<n; j++)
{
if(j!=i)
{
s=s*(a-x[j]);
t=t*(x[i]-x[j]);
}
}
k=k+((s/t)*y[i]);
}
cout<<"Interpolated value at "<<a<<" is "<<k<<endl;
return 0;
}
OUTPUT