Maple Loop Troubles: Fixing Newton's Method Procedure

  • Thread starter Thread starter cdotter
  • Start date Start date
  • Tags Tags
    Loop Maple
Click For Summary
SUMMARY

The discussion centers on resolving issues with a Newton's method procedure in Maple. The original code failed to properly assign the "guess" variable, leading to errors during execution. The solution involved renaming the input variable to "guesso" and explicitly assigning it to "guess" within the procedure. This adjustment allowed the function to operate correctly, demonstrating the importance of variable scope in Maple programming.

PREREQUISITES
  • Understanding of Maple programming language
  • Familiarity with Newton's method for root-finding
  • Knowledge of function definition and variable scope in programming
  • Basic calculus concepts, specifically differentiation
NEXT STEPS
  • Explore advanced Maple programming techniques for function manipulation
  • Learn about variable scope and lifetime in Maple
  • Investigate alternative root-finding methods in numerical analysis
  • Study the use of the 'unapply' function in Maple for function handling
USEFUL FOR

Students and educators in mathematics, particularly those learning numerical methods, as well as programmers seeking to improve their skills in Maple for mathematical computations.

cdotter
Messages
305
Reaction score
0

Homework Statement


I'm trying to make a Newton's method procedure in Maple, but I can't figure out what's wrong.

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

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.
 
Physics news on Phys.org
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;
 
Päällikkö said:
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
9K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
10
Views
2K
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
5K