Clear Filters
Clear Filters

Array indices must be positive integers or logical values

1 view (last 30 days)
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 Comment
Jan
Jan on 29 Oct 2022
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.

Sign in to comment.

Accepted Answer

Jan
Jan on 29 Oct 2022
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 Comments
Syed Abdul Rafay
Syed Abdul Rafay on 29 Oct 2022
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?

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!