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!