Question 01

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

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

Plot[f[x], {x, 0, 4}, PlotStyle → {Thick}, AxesLabel → {x, y}]


y

2
Out[2]=

x
1 2 3 4

-1

In[11]:= a = N[0];
b = N[1];
c = (a + b) / 2;
i = 1;
output = {{a, b, c, f[a], f[b], f[c], f[a] * f[c]}};
While[i ≤ 6, If[f[a] * f[c] < 0, b = c, a = c]; c = (a + b) / 2;
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]"}}], 8]];
Print["Approximate value of the root=", c];

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


1 0. 1. 0.5 -1. 0.63212056 -0.10653066 0.10653066
2 0.5 1. 0.75 -0.10653066 0.63212056 0.27763345 -0.029576474
3 0.5 0.75 0.625 -0.10653066 0.27763345 0.089738571 -0.0095599092
4 0.5 0.625 0.5625 -0.10653066 0.089738571 -0.0072828247 0.00077584412
5 0.5625 0.625 0.59375 -0.0072828247 0.089738571 0.04149755 -0.00030221938
6 0.5625 0.59375 0.578125 -0.0072828247 0.04149755 0.017175839 -0.00012508863
7 0.5625 0.578125 0.5703125 -0.0072828247 0.017175839 0.0049637604 -0.000036150197

Approximate value of the root=0.570313


2

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


Plot[f[x], {x, 0, 4}, PlotStyle → {Thick}, AxesLabel → {x, y}]
y

2
Out[22]=

x
1 2 3 4

-1

In[23]:= 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]", "f[a]*f[c]"}}], 8]];
Print["Approximate value of the root=", c];

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


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 ×

Approximate value of the root=0.567143

You might also like