If you change that od:
to od;
then it won't suppress the display of output from the statements within the loop.
Doing so would reveal details of what your code is doing.
The main problem is that the code within the loop doesn't make use of ind
. It is hard-coded to assign to x[2]
and make use of x[1]
and x[0]
.
But what you want is a scheme in which those various x
values depend on the ind
counter.
First, change the od:
to od;
and run it. Then try something like this below, and see how it differs.
restart;
Digits:=30;
f:=x->exp(sin(x))-x^2-x+1;
a:=-5; b:=0;
x[0]:=a;x[1]:=b;
epsilon:=Float(1,-15);
x[0]:=-4:x[1]:=-1:
ind:=0;
while abs(x[ind+1]-x[ind])>epsilon do
ind := ind+1;
x[ind+1]:=evalf( x[ind]
- f(x[ind]) * ( x[ind] - x[ind-1] )
/( f(x[ind]) - f(x[ind-1]) ) );
od;
printf("Numerical Solution %a
",x=x[ind+1]);
printf("Number of iterations %a",ind);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…