フィルターのクリア

Array indices must be positive integers or logical values

1 回表示 (過去 30 日間)
Syed Abdul Rafay
Syed Abdul Rafay 2022 年 10 月 29 日
コメント済み: Syed Abdul Rafay 2022 年 10 月 29 日
clc
f1= (-(3/5)*X(1))+((1/4)*X(2))+((1/4)*cos(X(3)))+1.43;
f2= ((1/4)*X(1))-((3/5)*X(2))-((1/4)*sin(X(3)))-1.24;
f3= ((1/4)*sin(X(1)))-((1/4)*exp(-X(2)))-((3/5)*X(3))-1.17;
F= [f1;f2;f3];
%J= jacobian([f1,f2,f3],[X(1),X(2),X(3)]);
J = [(-3/5) (1/4) (-1/4)*sin(X(3));
1/4 -3/5 (-1/4)*cos(X(3));
(1/4)*cos(X(1)) (1/4)*exp(-X(2)) -3/5];
x0=0; %initial guess
y0=0;
z0=0;
X = [x0;y0;z0];
TC=10^-8;
i = 0;
i_max = 100;
while(error>TC)
i=i+1;
X0=X;
X = X0 - J(X0)\F(X0);
error = max(abs(X-X0));
end
if i>i_max
disp('No solutions are found');
else
disp(['x = ',num2str(X(1))])
disp(['y = ',num2str(X(2))])
disp(['z = ',num2str(X(3))])
disp(error)
end
I get error
" Array indices must be positive integers or logical values. "
Can someone help me where is the problem?
also can you help me with J= jacobian([f1,f2,f3],[X(1),X(2),X(3)])? when I run this with upper code it give me error of incorrect arrangement
  1 件のコメント
Jan
Jan 2022 年 10 月 29 日
Please post the complete error message. Then the readers do not have to guess, in which line the error occurs.
We cannot run your code due to the missing inputs, so it would be a good idea to post all details you have in the command line available already.
"it give me error of incorrect arrangement" - see above: Post a copy of the message instead of a rough paraphrasation.

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

採用された回答

Jan
Jan 2022 年 10 月 29 日
I guess boldly, that this line is failing:
X = X0 - J(X0)\F(X0);
J and F are defined as vectors, not as functions.
I've removed the pile of parentheses from the equation and inserted a check of i<i_max in the loop condition:
F = @(X) [-(3/5)*X(1) + X(2) / 4 + cos(X(3)) / 4 + 1.43; ...
X(1) / 4 - (3/5)*X(2) - sin(X(3)) / 4 - 1.24; ...
sin(X(1)) / 4 - exp(-X(2)) / 4 - (3/5)*X(3) - 1.17];
J = @(X) [-3/5, 1/4, -sin(X(3) / 4);
1/4, -3/5, -cos(X(3) / 4);
cos(X(1)) / 4, exp(-X(2)) / 4, -3/5];
Err = Inf;
X = [0;0;0];
TC = 1e-8;
i = 0;
i_max = 100;
while Err > TC && i < i_max
i = i+1;
X0 = X;
X = X0 - J(X0) \ F(X0);
Err = max(abs(X - X0));
end
if i == i_max
disp('No solutions are found');
else
disp(X)
disp(Err)
end
1.3984 -1.8385 -4.1593
5.2661e-09
  2 件のコメント
Syed Abdul Rafay
Syed Abdul Rafay 2022 年 10 月 29 日
Is there are way to use jacobian command in this code without writing the jacobian matrix by myself and taking its inverse and adding that in the loop?
Syed Abdul Rafay
Syed Abdul Rafay 2022 年 10 月 29 日
Also thanks a lot. It made my day.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by