Error, array indices must be positive integers or logical values
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Jon
2020-2-14
编辑:Jon
2020-2-14
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 个评论
Jon
2020-2-14
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!