Array indices must be positive integers or logical values.

Hello, I am having trouble coding a solution using the Newton Raphson method. I need to input a negative x0 number, but I am getting the error, 'Array indices must be positive integers or logical values.'. Please help!
clc; clear
x0=input('Enter intial guess: ');
i=1;
I=100;
err=0.1;
while((err>1e-3) && i<I)
x=x0;
fx(x)=(3/500)*(x^3 - 18.535*x^2 + 25.697*x + 28.099);
pfx(x)= ((9000*x^2)-(111210*x)+77091)/500000;
x1=x-((fx(x))/(pfx(x)));
err=abs(x-x1);
x=x1;
i=i+1;
end
disp('Root of the function')
x(end)

댓글 수: 1

Unrecognized function or variable 'fx'.
If your fx were an array then fx(x) would try to index fx at x which could be a problem.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2019년 10월 14일
pfx(x)= ((9000*x^2)-(111210*x)+77091)/500000;
does not define a formula relating a function name pfx and an input value x. Instead, that line of code calculates a numeric value on the right side using the current value of x, and tries to store it in a variable named pfx at index value x . That cannot work if x happens to go fractional or 0 or negative.
To define a formula, you would need
pfx = @(x) ((9000*x^2)-(111210*x)+77091)/500000;

추가 답변 (0개)

질문:

JM
2019년 10월 14일

댓글:

JM
2019년 10월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by