Resolve a Complex equation in a loop WHILE
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Alberto
2014-4-14
I think you defined your functions badly. Try something like
f=@(x)(1-((c*x/2)*(log(x+1)-log(x-1))))
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!