I have a function $f(x) = \dfrac{1}{3} (2-e^x + x^2), [a,b]=[0,1]$ I start with an initial value of $.5$ What I am trying to do is write a short program that takes my initial $P_0 = 0.5$, plug it into the function to output $P_1$, put $P_1$ back into the function to output $P_2$, and take that value, put it back into the function to get $P_3$, etc. I want it to stop when $|P_n - P_{n-1}| \leq 0.0001$
I have written this, but I don't get any output from it:
f[x_] := (1/3)*(2 - Exp[x] + x^2);
a = 0.5;
Do[{c = f[a]
If[Abs[c - a] > 0.001, a = c
Print[c]]
If[Abs[c - a] <= 0.001, Print["The solution is " c]]
}, {i, 1, 10}];
If you are so kind to give me advice, take into account my only programming experience was FORTRAN from 22 years ago, so I am quite lost!
Thank you!
With[{eps = 0.0001}, FixedPoint[Function[{x}, (1/3)*(2 - Exp[x] + x^2)], .5, SameTest -> (Abs[#1 - #2] < eps &)] ]
!Mathematica graphics $\endgroup$FixedPoint
withFixedPointList
, like thisWith[{eps = 0.0001}, FixedPointList[Function[{x}, (1/3)*(2 - Exp[x] + x^2)], .5, SameTest -> (Abs[#1 - #2] < eps &)]]
and this gives{0.5, 0.200426, 0.272749, 0.253607, 0.25855, 0.257266, 0.257599, 0.257512}
$\endgroup$