0% found this document useful (0 votes)
64 views13 pages

Regula-Falsi Method Practical Codes Wolfram

Uploaded by

NANCY
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)
64 views13 pages

Regula-Falsi Method Practical Codes Wolfram

Uploaded by

NANCY
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/ 13

Regula Falsi

Regula-Falsi Method
22/CS/43

In[1]:= f[x_] = x ^ 3 - 5 x + 1;
Plotf[x], {x, 0, 1}, PlotStyle → Thick, Green, AxesLabel → {x, y}
y

x
0.2 0.4 0.6 0.8 1.0

Out[2]=
-1

-2

-3

In[19]:= a = N[0];
b = N[1];
c = a * f[b] - b * f[a]  f[b] - f[a]
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
If[f[c] ≠ 0, If[f[a] * f[c] < 0, b = c, a = c], Break[]];
c = a * f[b] - b * f[a]  f[b] - f[a];
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, "a", "b", "c", "f[a]", "f[b]", "f[c]", "Sign[f[a]*f[c]]", 8;
Print"Aprroximate value of the root is : ", c;
Out[21]=
0.25
2

a b c f[a] f[b] f[c] Sign[f[a]


1 0. 1. 0.25 1. -3. -0.234375 -0.234375
2 0. 0.25 0.20253165 1. -0.234375 -0.0043505686 -0.0043505686
3 0. 0.20253165 0.20165433 1. -0.0043505686 -0.000071505985 -0.000071505985
4 0. 0.20165433 0.20163992 1. -0.000071505985 -1.1725124 × 10-6 -1.1725124
5 0. 0.20163992 0.20163968 1. -1.1725124 × 10-6 -1.9225413 × 10-8 -1.9225413
6 0. 0.20163968 0.20163968 1. -1.9225413 × 10-8 -3.1523451 × 10-10 -3.1523451
7 0. 0.20163968 0.20163968 1. -3.1523451 × 10-10 -5.1688671 × 10-12 -5.1688671

Aprroximate value of the root is : 0.20164

In[27]:= f[x_] := 3 * x ^ 3 + 10 * x ^ 2 + 10 * x + 7;
Plotf[x], {x, - 3, - 2}, PlotStyle → Thick, Blue, AxesLabel → {x, y}
Out[28]=
y

x
-3.0 -2.8 -2.6 -2.4 -2.2

-5

-10

In[29]:= a = N[- 3];


b = N[- 2];
c = a * f[b] - b * f[a]  f[b] - f[a]
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
If[f[c] ≠ 0, If[f[a] * f[c] < 0, b = c, a = c], Break[]];
c = a * f[b] - b * f[a]  f[b] - f[a];
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, "a", "b", "c", "f[a]", "f[b]", "f[c]", "Sign[f[a]*f[c]]", 8;
Print"Aprroximate value of the root is : ", c;
Out[31]=
- 2.17647
3

a b c f[a] f[b] f[c] Sign[f[a]*f[c]]


1 -3. -2. -2.1764706 -14. 3. 1.6755547 -23.457765
2 -3. -2.1764706 -2.2644974 -14. 1.6755547 0.79783317 -11.169664
3 -3. -2.2644974 -2.3041524 -14. 0.79783317 0.35060609 -4.9084852
4 -3. -2.3041524 -2.321153 -14. 0.35060609 0.14859816 -2.0803742
5 -3. -2.321153 -2.3282827 -14. 0.14859816 0.062011393 -0.8681595
6 -3. -2.3282827 -2.3312448 -14. 0.062011393 0.025710174 -0.35994243
7 -3. -2.3312448 -2.3324707 -14. 0.025710174 0.010630779 -0.14883091

Aprroximate value of the root is : - 2.33247

In[37]:= f[x_] = x ^ 4 - 3 x ^ 2 + x - 10;


Plotf[x], {x, 2, 3}, PlotStyle → Thick, Orange, AxesLabel → {x, y}
Out[38]=
y

40

30

20

10

x
2.2 2.4 2.6 2.8 3.0

In[39]:= a = N[2];
b = N[3];
c = a * f[b] - b * f[a]  f[b] - f[a]
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
If[f[c] ≠ 0, If[f[a] * f[c] < 0, b = c, a = c], Break[]];
c = a * f[b] - b * f[a]  f[b] - f[a];
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, "a", "b", "c", "f[a]", "f[b]", "f[c]", "Sign[f[a]*f[c]]", 8;
Print"Aprroximate value of the root is : ", c;
Out[41]=
2.07843
4

a b c f[a] f[b] f[c] Sign[f[a]*f[c]]


1 2. 3. 2.0784314 -4. 47. -2.2198625 8.87945
2 2.0784314 3. 2.119995 -2.2198625 47. -1.1637008 2.5832558
3 2.119995 3. 2.1412571 -1.1637008 47. -0.59162874 0.68847884
4 2.1412571 3. 2.1519325 -0.59162874 47. -0.29607559 0.17516683
5 2.1519325 3. 2.1572414 -0.29607559 47. -0.1469951 0.04352166
6 2.1572414 3. 2.159869 -0.1469951 47. -0.072691406 0.010685281
7 2.159869 3. 2.1611663 -0.072691406 47. -0.035876602 0.0026079206
Aprroximate value of the root is : 2.16117

In[47]:= f[x_] := Log[1 + x] - Cos[x];


Plotf[x], {x, 0, 1}, PlotStyle → Thick, Yellow, AxesLabel → {x, y}
Out[48]=
y
0.2

x
0.2 0.4 0.6 0.8 1.0

-0.2

-0.4

-0.6

-0.8

-1.0

In[49]:= a = N[0];
b = N[1];
c = a * f[b] - b * f[a]  f[b] - f[a]
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
If[f[c] ≠ 0, If[f[a] * f[c] < 0, b = c, a = c], Break[]];
c = a * f[b] - b * f[a]  f[b] - f[a];
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, "a", "b", "c", "f[a]", "f[b]", "f[c]", "Sign[f[a]*f[c]]", 8;
Print"Aprroximate value of the root is : ", c;
Out[51]=
0.867419
5

a b c f[a] f[b] f[c]


1 0. 1. 0.86741939 -1. 0.15284487 -0.022239358
2 0.86741939 1. 0.8842599 -0.022239358 0.15284487 -0.00032698348
3 0.8842599 1. 0.88450698 -0.00032698348 0.15284487 -4.7463948 × 10-6
4 0.88450698 1. 0.88451056 -4.7463948 × 10-6 0.15284487 -6.8884227 × 10-8
5 0.88451056 1. 0.88451062 -6.8884227 × 10-8 0.15284487 -9.9971109 × 10-10
6 0.88451062 1. 0.88451062 -9.9971109 × 10-10 0.15284487 -1.4508839 × 10-11
7 0.88451062 1. 0.88451062 -1.4508839 × 10-11 0.15284487 -2.1049829 × 10-13

Aprroximate value of the root is : 0.884511

In[57]:= f[x_] := Cos[x] - x;


Plotf[x], {x, 0, 1}, PlotStyle → Thick, Pink, AxesLabel → {x, y}
Out[58]=
y

1.0

0.5

x
0.2 0.4 0.6 0.8 1.0

-0.5

In[59]:= a = N[0];
b = N[1];
c = a * f[b] - b * f[a]  f[b] - f[a]
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
If[f[c] ≠ 0, If[f[a] * f[c] < 0, b = c, a = c], Break[]];
c = a * f[b] - b * f[a]  f[b] - f[a];
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, "a", "b", "c", "f[a]", "f[b]", "f[c]", "Sign[f[a]*f[c]]", 8;
Print"Aprroximate value of the root is : ", c;
Out[61]=
0.685073
6

a b c f[a] f[b] f[c] Sign


1 0. 1. 0.68507336 1. -0.45969769 0.089299276 0.089299276
2 0.68507336 1. 0.736299 0.089299276 -0.45969769 0.004660039 0.0004161381
3 0.736299 1. 0.73894536 0.004660039 -0.45969769 0.00023392567 1.0901027
4 0.73894536 1. 0.73907813 0.00023392567 -0.45969769 0.000011719174 2.7414156
5 0.73907813 1. 0.73908478 0.000011719174 -0.45969769 5.8704655 × 10-7 6.8797008
6 0.73908478 1. 0.73908512 5.8704655 × 10-7 -0.45969769 2.9406673 × 10-8 1.7263086
7 0.73908512 1. 0.73908513 2.9406673 × 10-8 -0.45969769 1.4730553 × 10-9 4.3317656
Aprroximate value of the root is : 0.739085

In[67]:= f[x_] := x - Exp[- x];


Plotf[x], {x, 0, 1}, PlotStyle → Thick, Brown, AxesLabel → {x, y}

Out[68]=
y

0.5

x
0.2 0.4 0.6 0.8 1.0

-0.5

-1.0

In[69]:= a = N[0];
b = N[1];
c = a * f[b] - b * f[a]  f[b] - f[a]
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
If[f[c] ≠ 0, If[f[a] * f[c] < 0, b = c, a = c], Break[]];
c = a * f[b] - b * f[a]  f[b] - f[a];
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, "a", "b", "c", "f[a]", "f[b]", "f[c]", "Sign[f[a]*f[c]]", 8;
Print"Aprroximate value of the root is : ", c;
Out[71]=
0.6127
7

a b c f[a] f[b] f[c] Sign[f[a]*f[


1 0. 1. 0.61269984 -1. 0.63212056 0.070813948 -0.070813948
2 0. 0.61269984 0.57218141 -1. 0.070813948 0.0078882729 -0.0078882729
3 0. 0.57218141 0.56770321 -1. 0.0078882729 0.00087739198 -0.00087739198
4 0. 0.56770321 0.56720555 -1. 0.00087739198 0.000097572726 -0.000097572726
5 0. 0.56720555 0.56715021 -1. 0.000097572726 0.000010850621 -0.000010850621
6 0. 0.56715021 0.56714406 -1. 0.000010850621 1.2066458 × 10-6 -1.2066458
7 0. 0.56714406 0.56714338 -1. 1.2066458 × 10-6 1.3418529 × 10-7 -1.3418529

Aprroximate value of the root is : 0.567143

Secant Method
In[1]:= f[x_] = x ^ 3 - 5 * x + 1;
Plotf[x], {x, 0, 1}, PlotStyle → Thick, Green, AxesLabel → {x, y}
a = N[0];
b = N[1];
c = Nb - (b - a)  f[b] - f[a] * f[b];
i = 1;
output = {{a, b, c, f[a], f[b]}};
Whilei ≤ 6, If[f[b] ≠ f[a], b = c, a = c];
c = Nb - (b - a)  f[b] - f[a] * f[b];
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b]}];
PrintNumberForm
TableFormoutput, TableHeadings → Automatic, {"a", "b", "c", "f[a]", "f[b]"}, 0;
Print"Approx val of the root = " , c;
y

x
0.2 0.4 0.6 0.8 1.0

Out[2]=
-1

-2

-3
8

a b c f[a] f[b]
1 0. 1. 0.25 1. -3.
2 0. 0.25 0.202532 1. -0.234375
3 0. 0.202532 0.201654 1. -0.00435057
4 0. 0.201654 0.20164 1. -0.000071506
5 0. 0.20164 0.20164 1. -1.17251 × 10-6
6 0. 0.20164 0.20164 1. -1.92254 × 10-8
7 0. 0.20164 0.20164 1. -3.15235 × 10-10

Approx val of the root = 0.20164


In[253]:=
f[x_] := 3 * x ^ 3 + 10 * x ^ 2 + 10 * x + 7;
Plotf[x], {x, - 3, - 2}, PlotStyle → Thick, Purple, AxesLabel → {x, y}
a = N[- 3];
b = N[- 2];
c = Nb - (b - a)  f[b] - f[a] * f[b];
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
c = Nb - (b - a)  f[b] - f[a] * f[b];
a = b;
b = c;
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, {"a", "b", "c", "f[a]", "f[b]", "f[c]", "f[a]*f[c]"}, 0;
Print"Approx val of the root = " , c;
Out[254]=
y

x
-3.0 -2.8 -2.6 -2.4 -2.2

-5

-10
9

a b c f[a] f[b] f[c] f[a]*


1 -3. -2. -2.17647 -14. 3. 1.67555 -23.4578
2 -2. -2.17647 -2.17647 3. 1.67555 1.67555 5.02666
3 -2.17647 -2.39972 -2.39972 1.67555 -0.868172 -0.868172 -1.45467
4 -2.39972 -2.32353 -2.32353 -0.868172 0.119886 0.119886 -0.104081
5 -2.32353 -2.33277 -2.33277 0.119886 0.00691275 0.00691275 0.00082874
6 -2.33277 -2.33334 -2.33334 0.00691275 -0.000060879 -0.000060879 -4.20842
7 -2.33334 -2.33333 -2.33333 -0.000060879 3.0459 × 10-8 3.0459 × 10-8 -1.85431

Approx val of the root = - 2.33333

In[263]:=
f[x_] = x ^ 4 - 3 x ^ 2 + x - 10;
Plotf[x], {x, 2, 3}, PlotStyle → Thick, Blue, AxesLabel → {x, y}
a = N[2];
b = N[3];
c = Nb - (b - a)  f[b] - f[a] * f[b];
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
c = Nb - (b - a)  f[b] - f[a] * f[b];
a = b;
b = c;
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, {"a", "b", "c", "f[a]", "f[b]", "f[c]", "f[a]*f[c]"}, 0;
Print"Approx val of the root = " , c;
Out[264]=
y

40

30

20

10

x
2.2 2.4 2.6 2.8 3.0
10

a b c f[a] f[b] f[c] f[a]


1 2. 3. 2.07843 -4. 47. -2.21986 8.87945
2 3. 2.07843 2.07843 47. -2.21986 -2.21986 -104.334
3 2.07843 2.11999 2.11999 -2.21986 -1.1637 -1.1637 2.58326
4 2.11999 2.16579 2.16579 -1.1637 0.0960325 0.0960325 -0.111753
5 2.16579 2.1623 2.1623 0.0960325 -0.00365078 -0.00365078 -0.000350593
6 2.1623 2.16243 2.16243 -0.00365078 -0.0000107868 -0.0000107868 3.93802
7 2.16243 2.16243 2.16243 -0.0000107868 1.2174 × 10-9 1.2174 × 10-9 -1.31319

Approx val of the root = 2.16243


In[273]:=
f[x_] := Log[1 + x] - Cos[x];
Plotf[x], {x, 0, 1}, PlotStyle → Thick, Orange, AxesLabel → {x, y}
a = N[0];
b = N[1];
c = Nb - (b - a)  f[b] - f[a] * f[b];
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
c = Nb - (b - a)  f[b] - f[a] * f[b];
a = b;
b = c;
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, {"a", "b", "c", "f[a]", "f[b]", "f[c]", "f[a]*f[c]"}, 0;
Print"Approx val of the root = " , c;
Out[274]=
y
0.2

x
0.2 0.4 0.6 0.8 1.0

-0.2

-0.4

-0.6

-0.8

-1.0
11

a b c f[a] f[b] f[c]


1 0. 1. 0.867419 -1. 0.152845 -0.0222394
2 1. 0.867419 0.867419 0.152845 -0.0222394 -0.0222394
3 0.867419 0.88426 0.88426 -0.0222394 -0.000326983 -0.000326983
4 0.88426 0.884511 0.884511 -0.000326983 7.61971 × 10-7 7.61971 × 10-7
5 0.884511 0.884511 0.884511 7.61971 × 10-7 -2.57895 × 10-11 -2.57895 × 10-11
6 0.884511 0.884511 0.884511 -2.57895 × 10-11 1.11022 × 10-16 1.11022 × 10-16
7 0.884511 0.884511 0.884511 1.11022 × 10-16 0. 0.
Approx val of the root = 0.884511
In[283]:=
f[x_] := Cos[x] - x;
Plotf[x], {x, 0, 1}, PlotStyle → Thick, Yellow, AxesLabel → {x, y}
a = N[0];
b = N[1];
c = Nb - (b - a)  f[b] - f[a] * f[b];
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
c = Nb - (b - a)  f[b] - f[a] * f[b];
a = b;
b = c;
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, {"a", "b", "c", "f[a]", "f[b]", "f[c]", "f[a]*f[c]"}, 0;
Print"Approx val of the root = " , c;
Out[284]=
y

1.0

0.5

x
0.2 0.4 0.6 0.8 1.0

-0.5
12

a b c f[a] f[b] f[c] f


1 0. 1. 0.685073 1. -0.459698 0.0892993 0.0892993
2 1. 0.685073 0.685073 -0.459698 0.0892993 0.0892993 -0.0410507
3 0.685073 0.736299 0.736299 0.0892993 0.00466004 0.00466004 0.000416138
4 0.736299 0.739119 0.739119 0.00466004 -0.000057286 -0.000057286 -2.66955
5 0.739119 0.739085 0.739085 -0.000057286 3.52926 × 10-8 3.52926 × 10-8 -2.02177
6 0.739085 0.739085 0.739085 3.52926 × 10-8 2.66787 × 10-13 2.66787 × 10-13 9.4156
7 0.739085 0.739085 0.739085 2.66787 × 10-13 0. 0. 0.
Approx val of the root = 0.739085
In[293]:=
f[x_] := x - Exp[- x];
Plotf[x], {x, 0, 1}, PlotStyle → Thick, Red, AxesLabel → {x, y}
a = N[0];
b = N[1];
c = Nb - (b - a)  f[b] - f[a] * f[b];
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
Whilei ≤ 6,
c = Nb - (b - a)  f[b] - f[a] * f[b];
a = b;
b = c;
i = i + 1;
output = Append[output, {a, b, c, f[a], f[b], f[c], f[a] * f[c]}];
PrintNumberFormTableFormoutput,
TableHeadings → Automatic, {"a", "b", "c", "f[a]", "f[b]", "f[c]", "f[a]*f[c]"}, 0;
Print"Approx val of the root = " , c;
Out[294]=
y

0.5

x
0.2 0.4 0.6 0.8 1.0

-0.5

-1.0
13

a b c f[a] f[b] f[c]


1 0. 1. 0.6127 -1. 0.632121 0.0708139
2 1. 0.6127 0.6127 0.632121 0.0708139 0.0708139
3 0.6127 0.563838 0.563838 0.0708139 -0.00518235 -0.00518235
4 0.563838 0.56717 0.56717 -0.00518235 0.0000424192 0.0000424192
5 0.56717 0.567143 0.567143 0.0000424192 2.53802 × 10-8 2.53802 × 10-8
6 0.567143 0.567143 0.567143 2.53802 × 10-8 -1.24234 × 10-13 -1.24234 × 10-13
7 0.567143 0.567143 0.567143 -1.24234 × 10-13 0. 0.
Approx val of the root = 0.567143

You might also like