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

1 vue (au cours des 30 derniers jours)
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);

Réponse acceptée

Torsten
Torsten le 17 Avr 2022
Modifié(e) : Torsten le 17 Avr 2022
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 commentaire
Faruk Yagiz
Faruk Yagiz le 17 Avr 2022
Thank you so much, so only needed thing was this code then :)
F = matlabFunction(F);
jac = matlabFunction(jac);

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 17 Avr 2022
Modifié(e) : Image Analyst le 17 Avr 2022
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.

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by