Error, array indices must be positive integers or logical values
1 view (last 30 days)
Show older comments
I've read through some other posts but i can't seem to understand why this code gives me the error "array indices must be positive integers or logical values".
clc, clear, clf
f1=@(x,y)x.*exp(x.*y+0.8)+exp(y.^2)-5;
f2=@(x,y)x.^2-y.^2-0.3*exp(x.*y);
x=linspace(-3,3,100); y=linspace(-3,3,100);
[X,Y]=meshgrid(x,y);
Z1=f1(X,Y); Z2=f2(X,Y);
contour(x,y,Z1,[0 0],'green')
hold on
contour(x,y,Z2,[0 0],'r')
grid on
f=@(x)[f1(x,y) f2(x,y)];
D=@(x)-(3*x.*exp(x.*y)+20*y./10);
C=@(x)2*x-(3*y.*exp(x.*y))/10;
B=@(x)2*y.*exp(y.^2)+(x.^2).*exp(x.*y+4/5);
A=@(x)(y.*x+1).*exp(y.*x+4/5);
Df=[A(x) B(x); C(x) D(x)];
x=newton(f(x),Df(x),[1;0.5],1e-5)
0 Comments
Accepted Answer
Jon
on 14 Feb 2020
Edited: Jon
on 14 Feb 2020
In your code Df is a 2 by 200 matrix not a function. It therefore does not make sense in the last line of your code to have the expression.
Df(x)
where x is are not integer indices into a matrix.
I think you probably wanted to define an anonymous function
Df =@(x) [A(x) B(x); C(x) D(x)];
Also it is a little strange that you reassign x in the left hand side of your last line of code. You already had a value for x, did you want to overwrite it?
2 Comments
Jon
on 14 Feb 2020
Glad to hear. Looks like your off to a good start with your MATLAB, just keep at it and it will keep getting more natural
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!