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

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

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?
Also thanks a lot. It made my day.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2022년 10월 29일

댓글:

2022년 10월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by