Iteration: Newton-Raphson Method

  • Thread starter Thread starter aznkid310
  • Start date Start date
  • Tags Tags
    Method
Click For Summary

Discussion Overview

The discussion revolves around the application of the Newton-Raphson method and iterative techniques for solving the equation x = ln(2 + x). Participants explore various aspects of the problem, including convergence, the impact of initial guesses, and the implementation of algorithms in MATLAB.

Discussion Character

  • Homework-related
  • Mathematical reasoning
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using 16 decimal places for accuracy in the iterative method, while another questions if their result of approximately 4.233406958320529e-010 is sufficient.
  • There is a clarification needed regarding whether Q3 refers to Q1 or Q2, indicating potential confusion about the homework tasks.
  • Another participant emphasizes the importance of plotting the residue at every iteration as part of the iterative method.
  • Participants discuss the need for a loop in MATLAB to achieve convergence and the potential number of iterations required for different levels of accuracy.
  • Concerns are raised about the comparison of floating-point values due to rounding inaccuracies, suggesting the use of an epsilon value for comparisons.
  • There are corrections made regarding the formulation of the Newton-Raphson update function, with discussions on the correct derivative and function definitions.
  • One participant expresses difficulty in implementing the Newton-Raphson method in MATLAB and seeks guidance on coding.
  • Another participant points out that the lack of output from the code indicates a logical error in the implementation.

Areas of Agreement / Disagreement

Participants generally agree on the need for precision in calculations and the importance of correctly implementing the iterative methods. However, there are disagreements and confusion regarding the specifics of the homework questions and the implementation details in MATLAB, indicating that the discussion remains unresolved in certain areas.

Contextual Notes

Limitations include potential misunderstandings of the homework questions, the need for clarity in the iterative methods, and unresolved issues related to the implementation of the algorithms in MATLAB.

Who May Find This Useful

This discussion may be useful for students learning numerical methods, particularly those interested in iterative techniques for solving equations and their implementation in programming environments like MATLAB.

aznkid310
Messages
106
Reaction score
1

Homework Statement



For the exercise to work correctly you’ll need to carry 16 decimal places – double
precision.

Q1) Find the value of x that satisfies
x = ln(2 + x)
Begin with x(0) = 2, and try the iteration scheme
x(k+1) = ln(2 + x(k)):
If this method converges (lets say lim x(k) as =>inf = e), determine it’s order. Repeat the
calculation, and plot log (x(k) -e) on the y-axis against log (x(k-1) -e) on the x axis.

Q2) Solve (1) using the Newton-Raphson method, again beginning with x(0) = 2.
Graph log (x(k) -e) on the y-axis against log (x(k-1) -e) to determine the order of convergence.

Q3) Repeat Q3 with x(0) = -1.999

Point: for a problem with multiple roots, the solution will depend on the starting
point.

Q4) What happens with Newton-Raphson for -1:62 < x(0) < -1:01?

Point: Newton-Raphson converges rapidly when it converges, but convergence is
not guaranteed.

Q5) Use the naive method of Q1 to solve (1) beginning with x(0) = -1:5.

Point: Regions of convergence depend on the function and the method.

Homework Equations



For#1, am I suppose to just use brute force and iterate?

Also, can someone walk me through a few steps of this method? I am not really understanding how to do this?

The Attempt at a Solution



#1) Using MATLAB, i get x= 1.14619321999999 with a difference of about

4.233406958320529e-010. Is this close enough?
 
Physics news on Phys.org
You are supposed to calculate each of the iterates for two different estimation techniques, one being the approach in question 1, x_{k+1} = \ln(2+x_k), and other being Newton-Raphson. The end result is only a small part of the problem. 4.233406958320529e-010 is not "close enough". You were explicitly asked to carry 16 decimal places.
 
Q3) Repeat Q3 with x(0) = -1.999
This sounds like a recursive question. ;)
Would you please clarify if the repeat is on Q1 or Q2?
 
For Q1, you will use the brute force iterative method. If you are using MATLAB, you have the option of using a loop and get out when the error is small enough. So it is not as brute force as it may seem.
Also, don't forget to either print or plot the residue (error) at every iteration as requested at the end of Q1.
 
mathmate said:
This sounds like a recursive question. ;)
Would you please clarify if the repeat is on Q1 or Q2?

Sorry, Q3 should read: Repeat Q2 with x(0) = -1.999
 
Here is my code for part 1. Can someone help me check this? I am not sure if it's correct

clc, clear;
format compact;
format long
x = 2;
for count = 1:100 %Number of Iterations
a = log(2+x); %left hand side of equation
b = x; %right hand side of equation
if (a==b)
disp(x)
disp(count) %Number of iterations needed to get to answer
answer = x;
break;
end
x = a; %Answer
end

f = 2; %x(k=0) = 2
for k = 1:count
d = log(2+f);
e = f;
c = d-answer; %x(k)-a, where a is my answer (for convergence)
axisy(k) = c; %put answers in an array
f = d;
end

g = log(-answer);
for k = 1:count
h = log(2+g);
i = g;
j = h-answer; x(k-1)-a, where a is the convergent answer
axisx(k) = j;
g = h;
end

loglog(axisx,axisy)

2)For Newton-Raphson, is my f(x) = ln(2+x)-x?

then i use g(x)=x(k) - f(x)/f('x)

=x(k)-[ ln(2+x)-x] / (1/(2+x)+1) ?

Im not sure how to write a code for this. I've searched online and have found a few codes, but nothing that I need
Should I do this by hand instead?
 
Last edited:
If MATLAB is part of your coursework, you would probably be obliged to use it. If not, it would be an option, but probably a costly one in time.
If you post your results from MATLAB or hand calculations, I would be pleased to check the answer for you.
 
Hint: to get an accuracy of 15 decimal places, you would need about 31 iterations for Q1, and the first few digits are: 1.1461932206...
Therefore I think hand calculations are too prohibitive, as I have not yet found calculators of 15+ digits of accuracy.
Edit:
For your information, the primitive iteration method requires 202 iterations to get 100 decimal digits of accuracy, while Newton's method in Q2 requires only 8!
 
Last edited:
In floating point calculations, a==b will almost never be true because of rounding inaccuracies. You will need to compare the absolute value of the difference with the accuracy (eps) required. In your case,
eps=1.0E-15
So the comparison should read something like:
Code:
if abs(a-b)<eps

2)For Newton-Raphson, is my f(x) = ln(2+x)-x?
This is correct.
then i use g(x)=x(k) - f(x)/f('x)
9(x)=x(k)-[ ln(2+x)-x] / (1/(2+x)+1)
You have made a mistake in the sign of the derivative of -x.
g(x) should be equal to x(k)-[ ln(2+x)-x] / (1/(2+x)-1)

For the Newton's method, you proceed as in Q1, except that the values of a and b would be:
Code:
a=2.0
for count...
b=g(a)
if abs(a-b)<eps
...
a=b
 
  • #10
for the code I wrote, i got x = 1.14619322062058 and it took 34 iterations

I tried following what you said, but I was unable to get the correct answer. Could you guide me along a bit more? Sorry for being a little slow; it has been a while since I had to do programming of any sort. Here was my attempt

for count = 1:50
a = 2;
g(a)=a-[log(2+a)/((1/2+a)-1)]
b = g(a);
if (a-b)<eps
disp(a)
disp(count)
end
a=b;
end
 
  • #11
You have an error in your update function g(a).

The function at hand is

f(x)=\ln(2+x)-x

Differentiating,

f&#039;(x)=\frac{1}{2+x}-1

The k+1th Newton-Raphson iterate is

\aligned<br /> x_{k+1} &amp;= x_k - \frac{f(x_k)}{f&#039;(x_k)} \\<br /> &amp;= x_k - \frac{\ln(2+x_k)-x_k}{1/(2+x_k)-1}<br /> \aligned

Compare this to your code, where you have

g(a)=a-[log(2+a)/((1/2+a)-1)]
 
  • #12
sorry it should have been g(a)=a-([log(2+a)-a)/((1/2+a)-1)]

But even using this, i get:

g =
0 2.40913709258674
2
1
 
  • #13
Look at the denominator.
 
  • #14
Code:
 for the code I wrote, i got x = 1.14619322062058 and it took 34 iterations
The value of x is correct. Excellent work!
There could be a minor variation of the number of iterations depending on whether the termination condition is based on the latest value or not.

Some minor corections to the Q2 code are required, including the starting value of a & b should be initialized outside of the loop, and the exit condition tested with a while statement.
Note that the line that defines the value of b inside the while-loop has to be edited as per DH's post.
Check the code and the results, as I do not have Matlab.
Code:
a=0
b=2
% initialize count here
eps=1E-15  % accuracy required
while abs(a-b)<eps  % test exit condition
a=b
b=a-[log(2+a)/((1/2+a)-1)]   % this line to be corrected as per DH's post
% increment count here
disp(b)
disp(count)
end
 
Last edited:
  • #15
clc, clear;
format compact;
format long
a=0;
b=2;
for count = 1:50 % initialize count here
if abs(a-b)<eps % test exit condition
a=b;
b=a-[log(2+a)/((1/(2+a)-1))];
% increment count here
disp(b)
disp(count)
end
end

I have corrected the denominator (thx DH), am when I enter that code in, i get no output
 
  • #16
That you get no output is a *huge* hint that you have done something wrong. That means that that section of the code is never reached.

You don't need Matlab to run your code. You can run it yourself, on paper. Walk through your code and see what it does.
 
  • #17
Code:
Walk through your code and see what it does.
Good idea! Do not forget to study the difference between a for-loop and a while-loop!
 
  • #18
clc, clear;
format compact;
format long
a=0;
b=2;
for count = 1:50 % initialize count here % test exit condition
a=b;
b=a-[(log(2+a)-a)/((1/(2+a)-1))];
if abs(a-b)<eps % increment count here
disp(b)
disp(count)
end
end

I basically switched the order, and it gives me the correct answer. Thanks for the help!
 
  • #19
How many iterations did it take?
 
  • #20
it took 5 iterations, which should be correct since it is less than the 'power' method
 
  • #21
Good work. So now you only have to print the errors at each iteration to plot the graph indicating the order of convergence to log-log scale.

Also now you should have the tools required to do the rest of the questions.

I don't know how many decimal digits Matlab can handle, but if you are interested to experiment further, the result to 38 decimal digits is:
1.14619322062058258523706102852136825279

Have a good week!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
2
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K