0% found this document useful (0 votes)
41 views7 pages

Numerical Methods Notebook 074105

This document demonstrates using Mathematica to find the root of a polynomial function g(x) = x^3 + 4x^2 - 2x + 10 through iterative bisection and Newton's methods. It starts with an interval of [-5,-4] and performs 12 iterations of bisection to converge on a root of approximately -4.84009. It also uses Newton's method to find the root of x^2 - 12 = 0 starting from x=3, converging after 7 iterations. ResourceFunction is used to perform root finding with specified methods, intervals, and number of steps.

Uploaded by

Eddie Brock
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)
41 views7 pages

Numerical Methods Notebook 074105

This document demonstrates using Mathematica to find the root of a polynomial function g(x) = x^3 + 4x^2 - 2x + 10 through iterative bisection and Newton's methods. It starts with an interval of [-5,-4] and performs 12 iterations of bisection to converge on a root of approximately -4.84009. It also uses Newton's method to find the root of x^2 - 12 = 0 starting from x=3, converging after 7 iterations. ResourceFunction is used to perform root finding with specified methods, intervals, and number of steps.

Uploaded by

Eddie Brock
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/ 7

In[4]:= (*Sum of 1/1+1/2+...

+1/n*)
m = 10;
Sum[1 / n, {n, 1, m}]
Out[5]=
7381
2520

In[6]:= (*Absolute value of an integer is given by the command Abs*)


Abs[- 100]
Out[6]=

100

In[7]:= (*RandomInteger creates a list of random integers the numbers


in the first {,} gives the range from which the values are taken*)
(*the second argument shows hw many to consider*)
(*the first argument is optional*)
RandomInteger[{1, 101}, 100]
Out[7]=

{39, 63, 70, 18, 6, 63, 73, 68, 9, 64, 38, 34, 77, 11, 19, 86, 13, 96, 54,
36, 85, 17, 3, 94, 29, 69, 41, 9, 44, 93, 67, 66, 80, 30, 1, 79, 7, 59, 5, 12,
17, 10, 47, 82, 52, 72, 48, 11, 31, 92, 4, 33, 76, 21, 51, 90, 8, 43, 16, 64,
77, 23, 24, 37, 10, 63, 38, 35, 68, 47, 6, 100, 92, 94, 7, 43, 31, 25, 20, 11,
36, 70, 24, 97, 53, 72, 46, 39, 14, 95, 98, 65, 22, 40, 25, 57, 4, 65, 12, 29}

In[8]:= RandomInteger[{}, 100]


Out[8]=

{1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0,
0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0}

In[9]:= (*Sort in ascending *)


Sort[RandomInteger[{0, 200}, 100]]
Out[9]=

{0, 2, 4, 5, 5, 6, 8, 11, 13, 21, 23, 31, 34, 35, 36, 39, 39, 41, 44, 52, 53, 55, 56, 56, 58,
58, 59, 60, 60, 61, 63, 65, 67, 68, 70, 73, 75, 76, 77, 77, 84, 85, 86, 90, 90, 91, 95,
96, 97, 99, 102, 103, 103, 111, 114, 114, 115, 116, 117, 122, 125, 127, 127, 132, 135,
137, 138, 138, 139, 142, 148, 149, 149, 152, 153, 159, 160, 163, 164, 171, 172, 173,
174, 175, 177, 179, 181, 181, 183, 192, 193, 194, 195, 195, 196, 197, 198, 198, 198, 200}

In[11]:=

g[x_] := x ^ 3 + 4 * x ^ 2 - 2 * x + 10;
2

In[12]:=

Plot[g[x], {x, - 5, 4}]


Out[12]=

60

40

20

-4 -2 2 4

In[13]:=

Solve[g[x] ⩵ 0, x]
Out[13]=

x → -4.84… , x → 0.420… - 1.37… ⅈ , x → 0.420… + 1.37… ⅈ 

In[14]:=

{a, b} = {- 5, - 4};
{y, z} = {g[a], g[b]}
Out[15]=

{- 5, 18}

In[16]:=

Ifg[y] * g[z] < 0, m =  (a + b) / 2, "root is not in this interval"


Out[16]=
9
-
2
In[17]:=

If[g[m] < 0, a = m, b = m];


{a, b}
Out[18]=
9
- 5, - 
2
3

In[19]:=

{p, q} = {- 5, - 4};
{y, z} = {g[p], g[q]};
Ify * z < 0, m = ((p + q) / 2), "root is not in this interval";
If[g[m] < 0, p = m, q = m];
{{p, q}, {g[p], g[q]}}
Out[23]=
9 71
- 5, - , - 5, 
2 8
In[159]:=

{p, q} = {- 5, - 4};
expression := {y, z} = {g[p], g[q]};

Ify * z < 0, m = ((p + q) / 2), "root is not in this interval";


If[g[m] < 0, p = m, q = m];
{p, q}

In[115]:=

Do[expression, 1];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

9
- =- 4.5 is the Approximate Root with the value 8.875
2
In[119]:=

Do[expression, 2];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

19
- =- 4.75 is the Approximate Root with the value 2.57813
4
In[123]:=

Do[expression, 3];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

39
- =- 4.875 is the Approximate Root with the value - 1.04492
8
In[127]:=

Do[expression, 4];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

77
- =- 4.8125 is the Approximate Root with the value 0.807373
16
In[131]:=

Do[expression, 5];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

155
- =- 4.84375 is the Approximate Root with the value - 0.10849
32
4

In[135]:=

Do[expression, 6];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

309
- =- 4.82813 is the Approximate Root with the value 0.352001
64
In[139]:=

Do[expression, 7];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

619
- =- 4.83594 is the Approximate Root with the value 0.122397
128
In[143]:=

Do[expression, 8];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

1239
- =- 4.83984 is the Approximate Root with the value 0.00711399
256
In[149]:=

Do[expression, 9];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

2479
- =- 4.8418 is the Approximate Root with the value - 0.0506478
512
In[153]:=

Do[expression, 10];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

4957
- =- 4.84082 is the Approximate Root with the value - 0.0217569
1024
In[157]:=

Do[expression, 11];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

9913
- =- 4.84033 is the Approximate Root with the value - 0.00731894
2048
In[161]:=

Do[expression, 12];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

19 825
- =- 4.84009 is the Approximate Root with the value - 0.000101847
4096
5

In[185]:=

g[x_] := x ^ 3 + 4 * x ^ 2 - 2 * x + 10;
{p, q} = {- 5, - 4};
expression := {y, z} = {g[p], g[q]};

Ify * z < 0, m = ((p + q) / 2), "root is not in this interval";


If[g[m] < 0, p = m, q = m];
{p, q}

Do[expression, 12];
Printm, "=", N[m], " is the Approximate Root with the value ", N[g[m]]

19 825
- =- 4.84009 is the Approximate Root with the value - 0.000101847
4096
In[183]:=

g[x_] := x ^ 3 + 4 * x ^ 2 - 2 * x + 10;
ResourceFunction["BisectionMethodFindRoot"][g[x] ⩵ 0, {x, - 5, - 4}, 6, 13, "Steps"]
Out[184]=

steps a f[a] b f[b]


1 - 5.00000 - 5. - 4.00000 18.
2 - 5.00000 - 5. - 4.50000 8.875
3 - 5.00000 - 5. - 4.75000 2.57813
4 - 4.87500 - 1.04492 - 4.75000 2.57813
5 - 4.87500 - 1.04492 - 4.81250 0.807373
6 - 4.84375 - 0.10849 - 4.81250 0.807373
7 - 4.84375 - 0.10849 - 4.82813 0.352001
8 - 4.84375 - 0.10849 - 4.83594 0.122397
9 - 4.84375 - 0.10849 - 4.83984 0.00711399
10 - 4.84180 - 0.0506478 - 4.83984 0.00711399
11 - 4.84082 - 0.0217569 - 4.83984 0.00711399
12 - 4.84033 - 0.00731894 - 4.83984 0.00711399
13 - 4.84009 0. - 4.84009 0.

In[190]:=

ResourceFunction["BisectionMethodFindRoot"][g[x] ⩵ 0, {x, - 5, - 4}, 6, 13]


Out[190]=

{x → - 4.84009}

In[195]:=

ResourceFunction["NewtonsMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3}, 7, "Steps"]


Out[195]=

step x residual derivative


0 3. 3.00000 6.000000
1 3.5 0.25000 7.000000
2 3.46429 0.00128 6.928571
3 3.4641 0. × 10-6
6

In[196]:=

ResourceFunction["NewtonsMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3}, 7]


Out[196]=

{x → 3.464102}

In[200]:=

ResourceFunction["BisectionMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3, 3.5}, 7, 4]


Out[200]=

{x → 3.437500}

In[207]:=

ResourceFunction["BisectionMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3, 3.5}, 7, 4, "Steps"]


Out[207]=

steps a f[a] b f[b]


1 3.000000 - 3. 3.500000 0.25
2 3.250000 - 1.4375 3.500000 0.25
3 3.375000 - 0.609375 3.500000 0.25
4 3.437500 - 0.183594 3.500000 0.25

In[206]:=

ResourceFunction["BisectionMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3, 3.5}, 7, 5]


Out[206]=

{x → 3.468750}

In[208]:=

ResourceFunction["BisectionMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3, 3.5}, 7, 10]


Out[208]=

{x → 3.463867}

In[209]:=

ResourceFunction["BisectionMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3, 3.5}, 7, 15]


Out[209]=

{x → 3.464081}

In[214]:=

ResourceFunction["BisectionMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3, 3.5}, 7, 19]


Out[214]=

{x → 3.464102}

In[215]:=

ResourceFunction["SecantMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3, 4}, 7]


Out[215]=

{x → 3.464102}

In[216]:=

ResourceFunction["SecantMethodFindRoot"][x ^ 2 - 12 ⩵ 0, {x, 3, 4}, 7, "Steps"]

In[1]:= A = MatrixForm[{{1, 1, 1}, {3, 2, 3}, {3, 2, 1}}]


Out[1]//MatrixForm=
1 1 1
3 2 3
3 2 1
7

In[2]:= A
Out[2]//MatrixForm=
1 1 1
3 2 3
3 2 1

In[12]:=

B = MatrixForm[{{a, b, c}, {d, e, f}, {g, h, i}}];


B
Out[13]//MatrixForm=
a b c
d e f
g h i

In[9]:= MatrixForm[lu]
Out[9]//MatrixForm=
1 1 1
3 2 3
3 2 1

In[10]:=

{l, u} = {LowerTriangularize[lu, - 1] + IdentityMatrix[3], u = UpperTriangularize[lu]};


{l // MatrixForm, u // MatrixForm}
1 1 1
LowerTriangularize : Argument 3 2 3 at position 1 is not a non-empty rectangular matrix.
3 2 1
1 1 1
UpperTriangularize : Argument 3 2 3 at position 1 is not a non-empty rectangular matrix.
3 2 1
Out[11]=
1 1 1 1 1 1
1 + LowerTriangularize 3 2 3 , - 1 LowerTriangularize 3 2 3 , - 1 LowerTriangularize
3 2 1 3 2 1
1 1 1 1 1 1
 LowerTriangularize 3 2 3 , - 1 1 + LowerTriangularize 3 2 3 , - 1 LowerTriangularize
3 2 1 3 2 1
1 1 1 1 1 1
LowerTriangularize 3 2 3 , - 1 LowerTriangularize 3 2 3 , - 1 1 + LowerTriangularize
3 2 1 3 2 1

You might also like