Why do I get "Array indices must be positive integers or logical values." error?
Afficher commentaires plus anciens
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
Plus de réponses (1)
Image Analyst
le 17 Avr 2022
Modifié(e) : Image Analyst
le 17 Avr 2022
0 votes
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 Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!