Array indices must be positive integers or logical values.

1 visualización (últimos 30 días)
JM
JM el 14 de Oct. de 2019
Comentada: JM el 14 de Oct. de 2019
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 comentario
Walter Roberson
Walter Roberson el 14 de Oct. de 2019
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.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Oct. de 2019
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;

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by