Error, array indices must be positive integers or logical values

2 views (last 30 days)
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)

Accepted Answer

Jon
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
Axel Schörling
Axel Schörling on 14 Feb 2020
No, i'm just not very good at this whole matlab thing.
But whith the help of a friend and your answer i did rewrite the code so i got the answer i was looking for.
thank you!
Jon
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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!