Maple Loop Troubles: Fixing Newton's Method Procedure

In summary, the conversation discusses a problem with creating a Newton's method procedure in Maple. The correct solution involves changing the input variable and adding a new line of code.
  • #1
cdotter
305
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
  • #2
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;
 
  • #3
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.
 

FAQ: Maple Loop Troubles: Fixing Newton's Method Procedure

1. What is "Maple Loop Troubles"?

"Maple Loop Troubles" refers to a common issue encountered when using the Maple software to solve equations using Newton's method. It occurs when the algorithm gets stuck in a loop and is unable to converge to a solution.

2. How does Maple's Newton's method procedure work?

The Newton's method procedure in Maple uses an iterative process to approximate the root of a function by repeatedly applying a simple formula. The algorithm starts with an initial guess and then improves upon it with each iteration until the desired level of accuracy is achieved.

3. What causes the "Maple Loop Troubles" in Newton's method procedure?

There are a few possible reasons for the "Maple Loop Troubles" in Newton's method procedure. One common cause is when the initial guess is too far from the actual root, causing the algorithm to overshoot and enter a repeating loop. Another cause could be a function with multiple roots, causing the algorithm to converge to a different root than the one desired.

4. How can I fix the "Maple Loop Troubles" in Newton's method procedure?

There are a few strategies for fixing the "Maple Loop Troubles" in Newton's method procedure. One approach is to try different initial guesses, preferably ones that are closer to the actual root. Another option is to use a different root-finding method, such as the bisection method, which is more robust and less prone to looping.

5. Can I prevent the "Maple Loop Troubles" from happening in the first place?

While it's not always possible to prevent the "Maple Loop Troubles", there are some best practices that can help. These include choosing an appropriate initial guess, avoiding functions with multiple roots, and using the bisection method as a backup if the Newton's method procedure fails. Additionally, it's always helpful to carefully examine the function and its behavior before using any root-finding method.

Similar threads

Replies
1
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
8
Views
8K
Replies
5
Views
3K
Replies
1
Views
2K
Back
Top