You are on page 1of 15

Page |1

PROGRAM 1

AIM: WRITE A PROGRAM TO DEMONSTRATE NEWTON RAPHSON METHOD

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

9. x = x1 and end loop

10. Display method does not converge due to oscillation.

11. Stop

CODE

#include<iostream>

#include<cmath>

using namespace std;

#define EPSILON 0.001

double func(double x)

return x*x*x - x*x + 2;

double derivFunc(double x)

return 3*x*x - 2*x;

void newtonRaphson(double x)

double h = func(x) / derivFunc(x);

while (abs(h) >= EPSILON)

h = func(x)/derivFunc(x);
Page |3

x = x - h;

cout << "The value of the root is : " << x;

int main()

double x0 = -20; // Initial values assumed

newtonRaphson(x0);

return 0;

OUTPUT
Page |4

PROGRAM 2

AIM: WRITE A PROGRAM TO DEMONSTRATE BISECTION METHOD

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

2. Read x1, x2, e


*Here x1 and x2 are initial guesses
e is the absolute error i.e. the desired degree of accuracy*

3. Compute: f1 = f(x1) and f2 = f(x2)

4. If (f1*f2) > 0, then display initial guesses are wrong and goto (11).
Otherwise, continue.

5. x = (x1 + x2)/2

6. If ( [ (x1 – x2)/x ] < e ), then display x and goto (11).


* Here [ ] refers to the modulus sign. *

7. Else, f = f(x)

8. If ((f*f1) > 0), then x1 = x and f1 = f.

9. Else, x2 = x and f2 = f.

10. Goto (5).


*Now the loop continues with new values.*
Page |5

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

cout<<"Root is: "<<c;


return 0;
}
else if(f(a)*f(c)<0)
{
b=c;
}
else
{
a=c;
}
}while(fabs(a-b)>=0.00001);
cout<<"Root is: "<<c;
return 0;
}

OUTPUT
Page |7

PROGRAM 3

AIM: WRITE A PROGRAM TO DEMONSTRATE SECANT METHOD

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

2. Get values of x0, x1 and e


*Here x0 and x1 are the two initial guesses
e is the stopping criteria, absolute error or the desired degree of accuracy*

3. Compute f(x0) and f(x1)

4. Compute x2 = [x0*f(x1) – x1*f(x0)] / [f(x1) – f(x0)]

5. Test for accuracy of x2


If [ (x2 – x1)/x2 ] > e, *Here [ ] is used as modulus sign*
then assign x0 = x1 and x1 = x2
goto step 4
Else,
goto step 6

6. Display the required root as x2.

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

AIM: WRITE A PROGRAM TO DEMONSTRATE NEWTON INTERPOLATION METHOD

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 interpolationSearch(int arr[], int n, int x)


{
int lo = 0, hi = (n - 1);
while (lo <= hi && x >= arr[lo] && x <= arr[hi])
{
if (lo == hi)
{
if (arr[lo] == x) return lo;
return -1;
}
int pos = lo + (((double)(hi - lo) / (arr[hi] - arr[lo])) * (x - arr[lo]));
if (arr[pos] == x)
return pos;
if (arr[pos] < x)
lo = pos + 1;
else
hi = pos - 1;
}
return -1;
}

int main()
{
int n;
cout<<"Enter the size of array: ";
cin>>n;
int arr[n];
P a g e | 12

cout<<"Enter the elements of array: ";


for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int x;
cout<<"Enter the element to be searched: ";
cin>>x;
int index = interpolationSearch(arr, n, x);
if (index != -1)
cout << "Element found at index " << index;
else
cout << "Element not found.";
return 0;
}

OUTPUT
P a g e | 13

PROGRAM 5

AIM: WRITE A PROGRAM TO DEMONSTRATE LAGRANGE METHOD

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

You might also like