Array indices must be positive integers or logical values.

3 ビュー (過去 30 日間)
JM
JM 2019 年 10 月 14 日
コメント済み: JM 2019 年 10 月 14 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 14 日
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;
  1 件のコメント
JM
JM 2019 年 10 月 14 日
I see now, thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNewton-Raphson Method についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by