how to solve Index exceeds the number of array elements (1)?

1 回表示 (過去 30 日間)
Aaron Fredrick
Aaron Fredrick 2020 年 12 月 21 日
コメント済み: Aaron Fredrick 2020 年 12 月 21 日
Hy guys im writing the following code
% creating an array for D %
D_array = 1:1:40;
% preallocation for optimal distance & cost %
Optimal_distance = zeros(1,length(D));
Cost = zeros(1,length(D));
% Newton's Raphson Method %
for j = 1:length(D_array)
D = D_array(j);
% Newton-Raphson's Method %
% initial guess %
x0 = 60;
% 1st derivative value %
fx = @(x) (C_S1)*sqrt(D^(2)+x.^(2))+(C_O1)*(L-x);
% 2nd derivative value %
gx = @(x) ((C_S1)*x./sqrt(x.^(2)+D^2))-(C_O1);
g = g(x0);
Precision = 0.01;
while abs(g) > Precision
% new 1st derivative value %
f= fx(x0);
% Newton-Raphson's formula %
x0 = x0 - f/g;
% new 2nd derivative value %
g= gx(x0);
end
Cost(j) = g;
Optimal_distance(j) = x0;
end
Im trying to get values of x0 where my value of D changes but when i run the code i get the following error message;
Index exceeds the number of array elements (1).
Error in Q1c (line 32)
g = g(x0);
How can I solve this problem?

採用された回答

Jan
Jan 2020 年 12 月 21 日
Maybe you mean:
g = gx(x0);
% ^
  5 件のコメント
Jan
Jan 2020 年 12 月 21 日
Use the debugger to check the values: g does not reach a fix point near to 0, but it is iterating between -4499994.58631 and 499560.68281.
Aaron Fredrick
Aaron Fredrick 2020 年 12 月 21 日
How can I modify my code so i can get a proper output?

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by