Newton-Raphson Method in MATLAB for f(x)=tan-1 (x-1) - 0.5

  • Context: MATLAB 
  • Thread starter Thread starter roam
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on implementing the Newton-Raphson method in MATLAB to find a zero of the function f(x) = tan-1(x-1) - 0.5. The user sets an initial estimate of x0 = 4 and uses an absolute tolerance of 1e-1, but the output is an extremely large number instead of a converged solution. The issue arises from the initial guess being too far from the actual root, leading to failure in reaching the specified tolerance. A subsequent attempt with an absolute tolerance of 10^-9 yields no change in output, confirming the need for a better initial estimate.

PREREQUISITES
  • Understanding of the Newton-Raphson method
  • Familiarity with MATLAB syntax and functions
  • Knowledge of function derivatives
  • Concept of numerical tolerance in iterative methods
NEXT STEPS
  • Refine initial estimates for root-finding problems in MATLAB
  • Learn about MATLAB's built-in functions for root finding, such as fzero
  • Explore the impact of different tolerances on convergence in numerical methods
  • Study the behavior of the atan function and its derivatives for better initial guesses
USEFUL FOR

Students and professionals in mathematics, engineering, and computer science who are learning numerical methods and MATLAB programming, particularly those interested in root-finding algorithms.

roam
Messages
1,265
Reaction score
12
In MATLAB, I want to use the Newton-Raphson method with absolute tolerance 1e-1, other tolerances zero, and an initial estimate x0 = 4 to find a zero of the function

f(x) = tan-1 (x-1) - 0.5

And then repeat the process for a better estimate with absolute tolerance 10-9. So here is my code so far:

Code:
format long
x=4;
ind=0;
k=1;
f=@(x)(atan(x-1)-0.5);
fp=@(x)(1/((x - 1)^2 + 1));
N=30;
tola=1e-1;
tolr=0;
tolf=0;
 
while ind == 0 & k <= N,
    fp_value=feval(fp,x);
    if fp_value ~= 0,
        f_value=feval(f,x);
        if abs(f_value)>tolf,
            x_new=x - f_value/fp_value;
            if abs(x_new - x)<= tola + tolr * abs(x),
                ind=2;
            end
            x=x_new;
        else
            ind=1;
        end
    else
        ind=3;
    end
    k=k+1;
end
 
if ind==0,
    ind=4;
end
 
x

But we get:

Code:
x =
 
   -2.742845125833007e+211

So why is it that we don't get a single number instead of all 30 iterations? :confused:

Also when I changed the value of the absolute tolerance to 10^-9, there was no change in the output. Why is that? I'm new to Matlab, so any help is greatly appreciated.
 
Physics news on Phys.org
To get intermediate values, remove the ; in the line
Code:
x=x_new;

Since the wrong result is obtained, the tolerance is never reached, which is why its value is irrelevant. The problem is that the initial guess is too far from the real solution.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K