Why do I get "Array indices must be positive integers or logical values." error?

2 ビュー (過去 30 日間)
Faruk Yagiz
Faruk Yagiz 2022 年 4 月 16 日
コメント済み: Faruk Yagiz 2022 年 4 月 17 日
Hi, I keep getting this error while computing Newton's method for systems for 3 nonlinear system of equations. Why do I keep this error? Checking for a hour but still couldn't find.
The error is;
Array indices must be positive integers or logical values.
Error in indexing (line 1075)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in q6 (line 36)
F = [f(a,b,c); g(a,b,c); h(a,b,c)];
And my code is;
clc;
clear;
close all;
syms x y z
f(x,y,z)=2*x+y+2*z^2-5;
g(x,y,z)=y^3+4*z-4;
h(x,y,z)=x*y+z-exp(z);
fdx(x,y,z) = diff(f,x); %derivative of f wrt x
fdy(x,y,z) = diff(f,y); %derivative of f wrt y
fdz(x,y,z) = diff(f,z); %derivative of f wrt z
gdx(x,y,z) = diff(g,x); %derivative of g wrt x
gdy(x,y,z) = diff(g,y); %derivative of g wrt y
gdz(x,y,z) = diff(g,z); %derivative of g wrt z
hdx(x,y,z) = diff(h,x); %derivative of h wrt x
hdy(x,y,z) = diff(h,y); %derivative of h wrt y
hdz(x,y,z) = diff(h,z); %derivative of h wrt z
%Initial guesses
a=2;
b=2;
c=2;
ap=0;
bp=0;
cp=0;
iteration=1;
double(a);
double(b);
double(c);
eps=0.01;
for i=1:100
F = [f(a,b,c); g(a,b,c); h(a,b,c)];
jac=[fdx(a,b,c) fdy(a,b,c) fdz(a,b,c);gdx(a,b,c) gdy(a,b,c) gdz(a,b,c);hdx(a,b,c) hdy(a,b,c) hdz(a,b,c)]; %jacobian
h=-1*inv(jac)*F;
ap=a;
bp=b;
cp=c;
a=a+h(1);
b=b+h(2);
c=c+h(3);
iteration=iteration+1;
if (abs(a-ap)<eps && abs(b-bp)<eps && abs(c-cp)<eps)%check for error
break
end
end
fprintf('iteration=%d\ti=%f\tj=%f\tk=%f',iteration,a,b,c);

採用された回答

Torsten
Torsten 2022 年 4 月 17 日
編集済み: Torsten 2022 年 4 月 17 日
syms x y z
f=2*x+y+2*z^2-5;
g=y^3+4*z-4;
h=x*y+z-exp(z);
fdx = diff(f,x); %derivative of f wrt x
fdy = diff(f,y); %derivative of f wrt y
fdz = diff(f,z); %derivative of f wrt z
gdx = diff(g,x); %derivative of g wrt x
gdy = diff(g,y); %derivative of g wrt y
gdz = diff(g,z); %derivative of g wrt z
hdx = diff(h,x); %derivative of h wrt x
hdy = diff(h,y); %derivative of h wrt y
hdz = diff(h,z); %derivative of h wrt z
F=[f;g;h];
jac = [fdx fdy fdz;gdx gdy gdz;hdx hdy hdz]
F = matlabFunction(F);
jac = matlabFunction(jac);
%Initial guesses
a=2;
b=2;
c=2;
ap=0;
bp=0;
cp=0;
iteration=1;
eps=0.01;
for i=1:100
h=-jac(a,b,c)\F(a,b,c);
ap=a;
bp=b;
cp=c;
a=a+h(1);
b=b+h(2);
c=c+h(3);
iteration=iteration+1;
if (abs(a-ap)<eps && abs(b-bp)<eps && abs(c-cp)<eps)%check for error
break
end
end
fprintf('iteration=%d\ti=%f\tj=%f\tk=%f',iteration,a,b,c);
  1 件のコメント
Faruk Yagiz
Faruk Yagiz 2022 年 4 月 17 日
Thank you so much, so only needed thing was this code then :)
F = matlabFunction(F);
jac = matlabFunction(jac);

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 4 月 17 日
編集済み: Image Analyst 2022 年 4 月 17 日
This is asked every day. So, see the FAQ for a thorough discussion of the error.
I don't have the Symbolic Toolbox but you probably can't have a symbolic variable as an index. You need an actual, specific number.

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by