MATLAB Answers

Resolve a Complex equation in a loop WHILE

7 views (last 30 days)
Eduardo
Eduardo on 13 Apr 2014
Answered: Alberto on 14 Apr 2014
Hello,
I am trying to resolve a Newton-Raphson method by a complex equation.
But I am with the following error:
Attempted to access f(2.07279); index must be a positive integer or logical.
Error in teste04 (line 17) x = (x - (f(x)/df(x)));
I wrote the code:
x=2; %initial value
x_old= 12;
% c = 1.1 % If c is greater than the unity all the roots will be complex
c = 0.99; % If c is lower than the unity all the roots will be real
f(x)=(1-((c*x/2)*(log(x+1)-log(x-1)))); % the function that I would like to calculate the root by the newton-raphson method
df(x)=((c/2)*((log(x+1)-log(x-1))+(2*x/(x^2-1)))); % derivative of the f(x)
tolerance=0.00001; % the method will stop if the difference between the the diff = [x(i+1) - x(i)] were lower than 1E-5
while abs(x_old-x) > tolerance
x_old = x;
x = (x - (f(x)/df(x)));
pause;
end

Answers (1)

Alberto
Alberto on 14 Apr 2014
I think you defined your functions badly. Try something like
f=@(x)(1-((c*x/2)*(log(x+1)-log(x-1))))

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!