0% found this document useful (0 votes)
10 views21 pages

PRACTICAL Merged

The document discusses various numerical methods for finding the roots of equations, including the bisection method, Newton-Raphson method, secant method, and LU decomposition. Examples are provided to demonstrate each method applied to different functions.
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)
10 views21 pages

PRACTICAL Merged

The document discusses various numerical methods for finding the roots of equations, including the bisection method, Newton-Raphson method, secant method, and LU decomposition. Examples are provided to demonstrate each method applied to different functions.
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/ 21

BISECTION METHOD

In[29]:= ClearAll
a1 = 1; b1 = 2; f[x_] = x ^ 3 + 2 x ^ 2 - 3 x - 1; nmax = 10; tol
Fori = 1, i ≤ nmax, i ++,
p1 = (a1 + b1)  2;
If[f[a1] * f[p1] < 0, b1 = p1,
a1 = p1] ×
IfAbs(b1 - a1)  2 < tol, Break[] ×
Print"Approximate Root of f=", N[p1];
Print"Number of Iteration=", i - 1;
Out[29]=

ClearAll
Out[30]=

tol

Approximate Root of f=1.5


Approximate Root of f=1.25
Approximate Root of f=1.125
Approximate Root of f=1.1875
Approximate Root of f=1.21875
Approximate Root of f=1.20313
Approximate Root of f=1.19531
Approximate Root of f=1.19922
Approximate Root of f=1.19727
Approximate Root of f=1.19824
Number of Iteration=10
2

NEWTON-RAPSON METHOD

In[33]:= ClearAll;
f[x_] := x ^ 2 - 2;
h[x_] = D[f[x], x];
pold = 1;
Fori = 1, i ≤ 20, i ++,
pnew = pold - f[pold]  h[pold];
Print"Approximate Root =", N[pold];
pold = pnew;

Approximate Root =1.

Approximate Root =1.5

Approximate Root =1.41667

Approximate Root =1.41422

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421

Approximate Root =1.41421


3

In[42]:= f[x_] := x ^ 3 + x ^ 2 - 3 x - 3;
h[x_] = D[f[x], x];
pold = - 2;

Fori = 1, i ≤ 20, i ++,


pnew = pold - f[pold]  h[pold];
Print"Approximate Root =", N[pold];
pold = pnew;

Approximate Root =- 2.

Approximate Root =- 1.8

Approximate Root =- 1.73846

Approximate Root =- 1.73212

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Approximate Root =- 1.73205

Cloud: This computation has exceeded the time limit for your plan.
Out[45]=

$Aborted

NEWTON METHOD WITH STOPPING


CRITERIA
4

In[61]:= ClearAll;
f[x_] := x ^ 3 + x ^ 2 - 3 x - 3;
h[x_] = D[f[x], x];
pold = 1;
tol = 0.0001;
maxiteration = 20;
Fori = 1, i ≤ maxiteration, i ++,
pnew = pold - f[pold]  h[pold];
Print"Approximate Root =", N[pold];
pold = pnew;
If[Abs[pold - sqrt[3]] ≤ tol, Break[]];
Print"No. of iteration=", i
5

Approximate Root =1.

No. of iteration=1
Approximate Root =3.

No. of iteration=2
Approximate Root =2.2

No. of iteration=3
Approximate Root =1.83015

No. of iteration=4
Approximate Root =1.7378

No. of iteration=5
Approximate Root =1.73207

No. of iteration=6
Approximate Root =1.73205

No. of iteration=7
Approximate Root =1.73205

No. of iteration=8
Approximate Root =1.73205

No. of iteration=9
Approximate Root =1.73205

No. of iteration=10
Approximate Root =1.73205

No. of iteration=11
Approximate Root =1.73205

No. of iteration=12
Approximate Root =1.73205

No. of iteration=13
Approximate Root =1.73205

No. of iteration=14
Approximate Root =1.73205

No. of iteration=15
Approximate Root =1.73205

No. of iteration=16
6

LU DECOMPOSITION METHOD
In[51]:=

ClearAll;
A = 2, 7, 5, 6, 20, 10, 4, 3, 0;
b = 0, 4, 1;
lu, p, c = LUDecomposition[A];
d = DiagonalMatrix[Diagonal[lu]];
l = LowerTriangularize[lu] - d + IdentityMatrix[3];
u = UpperTriangularize[lu];
l.u;
(*Ax=b, lux=b, ux=y,ly=b*)
y = Inverse[l].b;
x = Inverse[u].y;
Print"Exact Solution:", x

1 7 43
Exact Solution:- , , - 
3 9 45

In[62]:= d
Out[62]=

{{2, 0, 0}, {0, - 1, 0}, {0, 0, 45}}

In[64]:= l
Out[64]=
{{1, 0, 0}, {3, 1, 0}, {2, 11, 1}}

In[66]:= lu
Out[66]=

{{2, 7, 5}, {3, - 1, - 5}, {2, 11, 45}}


7

In[67]:= ClearAll;
A = 1, 1, 2, - 1, 0, 2, 3, 2, - 1;

b = 3, - 1, 4;
lu, p, c = LUDecomposition[A];
d = DiagonalMatrix[Diagonal[lu]];
l = LowerTriangularize[lu] - d + IdentityMatrix[3];
u = UpperTriangularize[lu];
l.u;
(*Ax=b, lux=b, ux=y,ly=b*)
y = Inverse[l].b;
x = Inverse[u].y;
Print"Exact Solution:", x

Exact Solution:{3, - 2, 1}

In[43]:=

Syntax: "For i = 1, i ≤ n - 1, i ++, ndd1[[i]] = y[[i + 1]] - y[[i]]   x[[i + 1]] - x[[i]] " is incomplete; more input is
needed.
SECANT METHOD
PRACTICAL 3 SECANT METHOD
In[21]:= ClearAll;
f[x_] := x ^ 3 + x ^ 2 - 3 x - 3;
(*Roots[x^3+x^2-3x-3⩵0,x]*)
n = 10;
p = Table[0, {n}];
p[[0]] = 1;
p[[1]] = 2;
Fori = 1, i ≤ n, i ++,
p[[i + 1]] = p[[i]] - f[p[[i]]] p[[i]] - p[[i - 1]]  f[p[[i]]] - f[p[[i - 1]]];
Print["Approximate Roots=", N[p[[i]]]]
Approximate Roots=2.

Approximate Roots=1.57143

Approximate Roots=1.70541

Approximate Roots=1.73514

Approximate Roots=1.732

Approximate Roots=1.73205

Approximate Roots=1.73205

Approximate Roots=1.73205

Approximate Roots=1.73205

Set: Part 11 of 1[2, 8, (1772762731014126765724956554866944396186126657459441173534143 3180


78195806697857195724486927615195445512243358693626844352260721) / (
1023505039960342219153283912324349782390507384249863370301344 3180
17752559206480027808513072384804554487756641306373155647739279)] does not
exist.

Approximate Roots=1.73205

In[35]:= ClearAll;
f[x_] := x ^ 3 + 2 x ^ 2 - 3 x - 1;
(*Roots[x^3+2x^2-3x-1==0,x]*)
n = 10;
p = Table[0, {n}];
p[[0]] = 2;
p[[1]] = 1;
Fori = 1, i ≤ n, i ++,
p[[i + 1]] = p[[i]] - f[p[[i]]] p[[i]] - p[[i - 1]]  f[p[[i]]] - f[p[[i - 1]]];
Print["Approximate Roots=", N[p[[i]]]]
2 manyap3.nb

Approximate Roots=1.

Approximate Roots=1.1

Approximate Roots=1.22173

Approximate Roots=1.19649

Approximate Roots=1.19865

Approximate Roots=1.19869

Approximate Roots=1.19869

Approximate Roots=1.19869

Approximate Roots=1.19869

Set: Part 11 of 2[1, 8, (2221381049862664379002571315291950102013657489024555906823381 3309


90334341018276216922478735356652881503565979731619141504460949) / (
1853172000612031647803487739033840808682923632196319642937256 3309
43084840321130349546781428297569728536846484828186186374586333)] does not
exist.

Approximate Roots=1.19869

In[42]:= ClearAll;
f[x_] := x ^ 3 + 2 x ^ 2 - 3 x - 1;
(*Roots[x^3+2x^2-3x-1==0,x]*)
n = 10;
p = Table[0, {n}];
p[[0]] = 2;
p[[1]] = 1;
Fori = 1, i ≤ n, i ++,
p[[i + 1]] =
p[[i - 1]] × f[p[[i]]] - p[[i]] × f[p[[i - 1]]]  f[p[[i]]] - f[p[[i - 1]]];
Print["Approximate Roots=", N[p[[i]]]]
Approximate Roots=1.

Approximate Roots=1.1

Approximate Roots=1.22173

Approximate Roots=1.19649

Approximate Roots=1.19865

Approximate Roots=1.19869

Approximate Roots=1.19869

Approximate Roots=1.19869

Approximate Roots=1.19869

Set: Part 11 of 2[1, 8, (2221381049862664379002571315291950102013657489024555906823381 3309


90334341018276216922478735356652881503565979731619141504460949) / (
1853172000612031647803487739033840808682923632196319642937256 3309
43084840321130349546781428297569728536846484828186186374586333)] does not
exist.

Approximate Roots=1.19869

SECANT-SIMPLE
manyap3.nb 3

In[61]:= ClearAll;
f[x_] := x ^ 3 + x ^ 2 - 3 x - 3;
p0 = 1; (*First Approximation*)
p1 = 2; (*Second Approximation*)
Fori = 1, i ≤ 7, i ++,
p2 = p1 - f[p1] p1 - p0  f[p1] - f[p0];
Print["Approximate Roots=", N[p2]];
p0 = p1;
p1 = p2;

Approximate Roots=1.57143

Approximate Roots=1.70541

Approximate Roots=1.73514

Approximate Roots=1.732

Approximate Roots=1.73205

Approximate Roots=1.73205

Approximate Roots=1.73205
GAUSS-JACOBI METHOD METHOD
PRACTICAL 5 GAUSS-JACOBI
ClearAll;
A = {{2, - 1, 0}, {- 1, 4, 2}, {0, 2, 6}};
b = {- 1, 3, 5};
d = DiagonalMatrix[Diagonal[A]];
l = LowerTriangularize[A] - d;
u = UpperTriangularize[A] - d;
t = - Inverse[d].l + u;
c = Inverse[d].b;
n = 10;
xold = {0, 0, 0};
For[i = 1, i ≤ n, i ++,
xnew = t.xold + c;
Print["Approximate value:", N[xnew]];
xold = xnew;]
Approximate value:{-0.5, 0.75, 0.833333}

Approximate value:{-0.125, 0.208333, 0.583333}

Approximate value:{-0.395833, 0.427083, 0.763889}

Approximate value:{-0.286458, 0.269097, 0.690972}

Approximate value:{-0.365451, 0.332899, 0.743634}

Approximate value:{-0.33355, 0.28682, 0.722367}

Approximate value:{-0.35659, 0.305429, 0.737727}

Approximate value:{-0.347286, 0.291989, 0.731524}

Approximate value:{-0.354005, 0.297417, 0.736004}

Approximate value:{-0.351292, 0.293497, 0.734194}

In[ ]:= A
Out[ ]= {{2, - 1, 0}, {- 1, 4, 2}, {0, 2, 6}}

In[ ]:= b
Out[ ]= {- 1, 3, 5}

In[ ]:= d
Out[ ]= {{2, 0, 0}, {0, 4, 0}, {0, 0, 6}}

In[ ]:= l
Out[ ]= {{0, 0, 0}, {- 1, 0, 0}, {0, 2, 0}}

In[ ]:= u
Out[ ]= {{0, - 1, 0}, {0, 0, 2}, {0, 0, 0}}

In[ ]:= t
1 1 1 1
Out[ ]= 0, , 0,  , 0, - , 0, - , 0
2 4 2 3
2 manyap5.nb

In[ ]:= c
1 3 5
Out[ ]= - , , 
2 4 6

QUESTION 2(Tut. Sheet 8)


In[63]:= ClearAll;
A = {{2, 7, 5}, {6, 20, 10}, {4, 3, 0}};
A1 = {{2, 7, 5}, {4, 3, 0}, {6, 20, 10}};
b = {0, 4, 1};
b1 = {0, 1, 4};
d = DiagonalMatrix[Diagonal[A1]];
l = LowerTriangularize[A1] - d;
u = UpperTriangularize[A1] - d;
t = - Inverse[d].l + u;
c = Inverse[d].b1;
n = 10;
xold = {0, 0, 0};
For[i = 1, i ≤ n, i ++,
xnew = t.xold + c;
Print["Approximate value:", N[xnew]];
xold = xnew;]
Approximate value:{0., 0.333333, 0.4}

Approximate value:{-2.16667, 0.333333, -0.266667}

Approximate value:{-0.5, 3.22222, 1.03333}

Approximate value:{-13.8611, 1., -5.74444}

Approximate value:{10.8611, 18.8148, 6.71667}

Approximate value:{-82.6435, -14.1481, -43.7463}

Approximate value:{158.884, 110.525, 78.2824}

Approximate value:{-582.542, -211.512, -315.98}

Approximate value:{1530.24, 777.057, 772.95}

Approximate value:{-4652.07, -2039.99, -2471.86}

This Iterative method is not converging for this particular problem.


manyap5.nb 3

In[219]:= ClearAll;
A = {{1, 1, 2}, {- 1, 0, 2}, {3, 2, - 1}};
A1 = {{1, 1, 2}, {3, 2, 1}, {- 1, 0, 2}};
b = {3, - 1, 4};
b1 = {3, 4, - 1};
d = DiagonalMatrix[Diagonal[A1]];
l = LowerTriangularize[A1] - d;
u = UpperTriangularize[A1] - d;
t = - Inverse[d].l + u;
c = Inverse[d].b1;
n = 10;
xold = {0, 0, 0};
For[i = 1, i ≤ n, i ++,
xnew = t.xold + c;
Print["Approximate value:", N[xnew]];
xold = xnew;]
Approximate value:{3., 2., -0.5}

Approximate value:{2., -2.25, 1.}

Approximate value:{3.25, -1.5, 0.5}

Approximate value:{3.5, -3.125, 1.125}

Approximate value:{3.875, -3.8125, 1.25}

Approximate value:{4.3125, -4.4375, 1.4375}

Approximate value:{4.5625, -5.1875, 1.65625}

Approximate value:{4.875, -5.67188, 1.78125}

Approximate value:{5.10938, -6.20313, 1.9375}

Approximate value:{5.32813, -6.63281, 2.05469}


GAUSS-SEIDEL METHOD METHOD
PRACTICAL 6 GAUSS-SEIDEL
In[17]:= ClearAll;
A = {{2, - 1, 0}, {- 1, 4, 2}, {0, 2, 6}};
b = {- 1, 3, 5};
d = DiagonalMatrix[Diagonal[A]];
l = LowerTriangularize[A] - d;
u = UpperTriangularize[A] - d;
t = - Inverse[d + l].(u);
c = Inverse[d + l].b;
n = 10;
xold = {0, 0, 0};
For[i = 1, i ≤ n, i ++,
xnew = t.xold + c;
Print["Approximate value:", N[xnew]];
xold = xnew;]
Approximate value:{-0.5, 0.625, 0.625}

Approximate value:{-0.1875, 0.390625, 0.703125}

Approximate value:{-0.304688, 0.322266, 0.725911}

Approximate value:{-0.338867, 0.302327, 0.732558}

Approximate value:{-0.348836, 0.296512, 0.734496}

Approximate value:{-0.351744, 0.294816, 0.735061}

Approximate value:{-0.352592, 0.294321, 0.735226}

Approximate value:{-0.352839, 0.294177, 0.735274}

Approximate value:{-0.352911, 0.294135, 0.735288}

Approximate value:{-0.352933, 0.294123, 0.735292}

QUESTION 2(Tut. Sheet 8)


In[89]:= ClearAll;
A = {{2, 7, 5}, {6, 20, 10}, {4, 3, 0}};
A1 = {{2, 7, 5}, {4, 3, 0}, {6, 20, 10}};
b = {0, 4, 1};
b1 = {0, 1, 4};
d = DiagonalMatrix[Diagonal[A1]];
l = LowerTriangularize[A1] - d;
u = UpperTriangularize[A1] - d;
t = - Inverse[d + l].(u);
c = Inverse[d + l].b1;
n = 10;
xold = {0, 0, 0};
For[i = 1, i ≤ n, i ++,
xnew = t.xold + c;
Print["Approximate value:", N[xnew]];
xold = xnew;]
2 manyap6.nb

Approximate value:{0., 0.333333, -0.266667}

Approximate value:{-0.5, 1., -1.3}

Approximate value:{-0.25, 0.666667, -0.783333}

Approximate value:{-0.375, 0.833333, -1.04167}

Approximate value:{-0.3125, 0.75, -0.9125}

Approximate value:{-0.34375, 0.791667, -0.977083}

Approximate value:{-0.328125, 0.770833, -0.944792}

Approximate value:{-0.335938, 0.78125, -0.960938}

Approximate value:{-0.332031, 0.776042, -0.952865}

Approximate value:{-0.333984, 0.778646, -0.956901}

In[206]:= ClearAll;
A = {{1, 1, 2}, {- 1, 0, 2}, {3, 2, - 1}};
A1 = {{1, 1, 2}, {3, 2, 1}, {- 1, 0, 2}};
b = {3, - 1, 4};
b1 = {3, 4, - 1};
d = DiagonalMatrix[Diagonal[A1]];
l = LowerTriangularize[A1] - d;
u = UpperTriangularize[A1] - d;
t = - Inverse[d + l].(u);
c = Inverse[d + l].b1;
n = 10;
xold = {0, 0, 0};
For[i = 1, i ≤ n, i ++,
xnew = t.xold + c;
Print["Approximate value:", N[xnew]];
xold = xnew;]
Approximate value:{3., -2.5, 1.}

Approximate value:{3.5, -3.75, 1.25}

Approximate value:{4.25, -5., 1.625}

Approximate value:{4.75, -5.9375, 1.875}

Approximate value:{5.1875, -6.71875, 2.09375}

Approximate value:{5.53125, -7.34375, 2.26563}

Approximate value:{5.8125, -7.85156, 2.40625}

Approximate value:{6.03906, -8.26172, 2.51953}

Approximate value:{6.22266, -8.59375, 2.61133}

Approximate value:{6.37109, -8.8623, 2.68555}


FIXED POINT PROBLEM AND ROOT FIND-
FIXED POINT ITERATION
ING PROBLEM COMBINED
In[ ]:= ClearAll;
pold = 1;
g1[x_] := x ^ 3 + x ^ 2 - 3  3;
For[i = 1, i ≤ 10, i ++,
pnew = g1[pold];
pold = pnew;
Print["fixed point iteration=", N[pnew]]]
fixed point iteration=-0.333333

fixed point iteration=-0.975309

fixed point iteration=-0.992171

fixed point iteration=-0.997431

fixed point iteration=-0.999148

fixed point iteration=-0.999717

fixed point iteration=-0.999906

fixed point iteration=-0.999969

fixed point iteration=-0.99999

fixed point iteration=-0.999997

In[ ]:= ClearAll;


pold = 1;
g2[x_] := - 1 + 3 x + 3  x ^ 2;
For[i = 1, i ≤ 20, i ++,
pnew = g2[pold];
pold = pnew;
Print["fixed point iteration=", N[pnew]]]
2 MANYA PRACTICE.NB

fixed point iteration=5.

fixed point iteration=-0.28

fixed point iteration=26.551

fixed point iteration=-0.882754

fixed point iteration=-0.548625

fixed point iteration=3.49893

fixed point iteration=0.102454

fixed point iteration=314.08

fixed point iteration=-0.990418

fixed point iteration=-0.970695

fixed point iteration=-0.906696

fixed point iteration=-0.659513

fixed point iteration=1.34842

fixed point iteration=2.87479

fixed point iteration=0.406555

fixed point iteration=24.5293

fixed point iteration=-0.872712

fixed point iteration=-0.498618

fixed point iteration=5.04999

fixed point iteration=-0.288303

In[ ]:= ClearAll;


pold = 1;
3
g3[x_] := 3 + 3 x - x ^ 2 ;
For[i = 1, i ≤ 20, i ++,
pnew = g3[pold];
pold = pnew;
Print["fixed point iteration=", N[pnew]]]
MANYA PRACTICE.NB 3

fixed point iteration=1.70998

fixed point iteration=1.73313

fixed point iteration=1.73199

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

In[ ]:= ClearAll;


pold = 1;
g4[x_] := 3 + 3 x - x ^ 2  x ^ 1  2;
For[i = 1, i ≤ 20, i ++,
pnew = g4[pold];
pold = pnew;
Print["fixed point iteration=", N[pnew]]]
fixed point iteration=2.23607

fixed point iteration=1.45106

fixed point iteration=1.90168

fixed point iteration=1.63581

fixed point iteration=1.78834

fixed point iteration=1.69976

fixed point iteration=1.75077

fixed point iteration=1.72127

fixed point iteration=1.73828

fixed point iteration=1.72845

fixed point iteration=1.73413

fixed point iteration=1.73085

fixed point iteration=1.73274


4 MANYA PRACTICE.NB

In[ ]:= ClearAll;


pold = 1;
g5[x_] := x - x ^ 3 + x ^ 2 - 3 x - 3  3 x ^ 2 + 2 x - 3;
For[i = 1, i ≤ 20, i ++,
pnew = g5[pold];
pold = pnew;
Print["fixed point iteration=", N[pnew]]]
fixed point iteration=3.

fixed point iteration=2.2

fixed point iteration=1.83015

fixed point iteration=1.7378

fixed point iteration=1.73207

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

fixed point iteration=1.73205

In[ ]:= f[x_] = x ^ 3 + x ^ 2 + 3 x - 3;


Solve[x ^ 3 + x ^ 2 - 3 x - 3 == 0, x]
Out[ ]= {x → - 1}, x → - 3 , x → 3 
NEWTON INTERPOLATION
PRACTICAL 8 NEWTON INTERPOLATION
NEWTON DIVIDED DIFFERENCE
(*Tutorial Question 1(a)*)

ClearAll;
x = {- 1, 0, 1, 2};
y = {3, - 1, - 3, 1};
n = Length[x];
ndd1 = Table[0, {n - 1}];
ndd2 = Table[0, {n - 2}];
ndd3 = Table[0, {n - 3}];
Fori = 1, i ≤ n - 1, i ++,
ndd1[[i]] = y[[i + 1]] - y[[i]]  x[[i + 1]] - x[[i]]
Fori = 1, i ≤ n - 2, i ++,
ndd2[[i]] = ndd1[[i + 1]] - ndd1[[i]]  x[[i + 2]] - x[[i]]
Fori = 1, i ≤ n - 3, i ++,
ndd3[[i]] = ndd2[[i + 1]] - ndd2[[i]]  x[[i + 3]] - x[[i]]
nddc = Table[0, {n}];
nddc[[1]] = y[[1]];
nddc[[2]] = ndd1[[1]];
nddc[[3]] = ndd2[[1]];
nddc[[4]] = ndd3[[1]];
(*creating polynomial*)
sum = 0;
pr = 1;
Fori = 1, i ≤ n, i ++,
sum = sum + nddc[[i]] * pr;
pr = pr * t - x[[i]];
Print["NEWTON DIVIDED DIFFERENCE POLYNOMIAL= ", Expand[sum]]
NEWTON DIVIDED DIFFERENCE POLYNOMIAL= 3

NEWTON DIVIDED DIFFERENCE POLYNOMIAL= -1 - 4 t

NEWTON DIVIDED DIFFERENCE POLYNOMIAL= -1 - 3 t + t 2


11 t 2 t3
NEWTON DIVIDED DIFFERENCE POLYNOMIAL= -1 - + t2 +
3 3

In[ ]:= ndd1


Out[ ]= {- 4, - 2, 4}

In[ ]:= ndd2


Out[ ]= {1, 3}

In[ ]:= ndd3


2
Out[ ]=  
3

In[ ]:= nddc[[1]]


Out[ ]= 3
2 manyap8.nb

In[ ]:= nddc[[2]]


Out[ ]= -4

In[ ]:= nddc[[3]]


Out[ ]= 1

In[ ]:= nddc[[4]]


2
Out[ ]=
3

In[172]:= ClearAll;
x = {0, 1, 2, 3, 4};
y = {5, 15, 61, 197, 501};
n = Length[x];
ndd1 = Table[0, {n - 1}];
ndd2 = Table[0, {n - 2}];
ndd3 = Table[0, {n - 3}];
ndd4 = Table[0, {n - 4}];
Fori = 1, i ≤ n - 1, i ++,
ndd1[[i]] = y[[i + 1]] - y[[i]]  x[[i + 1]] - x[[i]];
Fori = 1, i ≤ n - 2, i ++,
ndd2[[i]] = ndd1[[i + 1]] - ndd1[[i]]  x[[i + 2]] - x[[i]];
Fori = 1, i ≤ n - 3, i ++,
ndd3[[i]] = ndd2[[i + 1]] - ndd2[[i]]  x[[i + 3]] - x[[i]];
Fori = 1, i ≤ n - 4, i ++,
ndd4[[i]] = ndd3[[i + 1]] - ndd3[[i]]  x[[i + 4]] - x[[i]];
nddc = Table[0, {n}];
nddc[[1]] = y[[1]];
nddc[[2]] = ndd1[[1]];
nddc[[3]] = ndd2[[1]];
nddc[[4]] = ndd3[[1]];
nddc[[5]] = ndd4[[1]];
(* creating polynomial*)
sum = 0;
pr = 1;
Fori = 1, i ≤ n, i ++,
sum = sum + nddc[[i]] * pr;
pr = pr * t - x[[i]];
Print["NEWTON DIVIDED DIFFERENCE POLYNOMIAL= ", Expand[sum]]
NEWTON DIVIDED DIFFERENCE POLYNOMIAL= 5 + 4 t + 2 t 2 + 3 t3 + t4

You might also like