Why do I get "Array indices must be positive integers or logical values." error?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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);
0 Commenti
Risposta accettata
Torsten
il 17 Apr 2022
Modificato: Torsten
il 17 Apr 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);
Più risposte (1)
Image Analyst
il 17 Apr 2022
Modificato: Image Analyst
il 17 Apr 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!