Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

math - Secant Method in MAPLE

I am trying to use the Secant Method in MAPLE to find the negative solution of f(x)=0, where f(x)=e^(sin(x))-x^2-x+1. However, my code is not giving me the right solution. What am I doing wrong?

>restart
>Digits:=20
>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[2]-x[1])>epsilon do
x[2]:=evalf(x[1]-f(x[1])*(x[1]-x[0])/(f(x[1])-f(x[0])));
ind:=ind+1:
od:
printf("Numerical Solution %a
",x=x[2]);
printf("Number of iterations %a",ind);
question from:https://stackoverflow.com/questions/65930837/secant-method-in-maple

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...