Maple loop troubles

305
0
1. The problem statement, all variables and given/known data
I'm trying to make a Newton's method procedure in Maple, but I can't figure out what's wrong.

2. Relevant equations

Code:
newton := proc (func, iterations, guess)
f := unapply(func, x);
fprimej := diff(f(x), x);
fprime := unapply(fprimej, x);
for i from 1 to iterations do:
     value := evalf(guess-f(guess)/fprime(guess));
     printf("Iteration %d: %a\n", i, value);
     guess := value;
end do;
end proc;

3. The attempt at a solution

Something is going wrong when I assign "guess" to "value" because it works fine if I hard code a "guess" value in. What's the correct (and error free) way of assigning "guess" to "value?" Thank you.
 

Päällikkö

Homework Helper
517
10
I've got very little experience in coding Maple, so this is probably a bit of a hack. Apparently you can't properly define new variables in the function input, i.e. the "guess" you give the function seems not to be a proper variable. I changed it to guesso and added guess := guesso; as the first line. It worked.

Code:
newton := proc (func, iterations, guesso)
guess := guesso;
f := unapply(func, x);
fprimej := diff(f(x), x);
fprime := unapply(fprimej, x);
for i from 1 to iterations do:
     value := evalf(guess-f(guess)/fprime(guess));
     printf("Iteration %d: %a\n", i, value);
     guess := value;
end do;
end proc;
 
305
0
I've got very little experience in coding Maple, so this is probably a bit of a hack. Apparently you can't properly define new variables in the function input, i.e. the "guess" you give the function seems not to be a proper variable. I changed it to guesso and added guess := guesso; as the first line. It worked.

Code:
newton := proc (func, iterations, guesso)
guess := guesso;
f := unapply(func, x);
fprimej := diff(f(x), x);
fprime := unapply(fprimej, x);
for i from 1 to iterations do:
     value := evalf(guess-f(guess)/fprime(guess));
     printf("Iteration %d: %a\n", i, value);
     guess := value;
end do;
end proc;
Thank you, that works perfectly.
 

The Physics Forums Way

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top