Error, array indices must be positive integers or logical values

1 回表示 (過去 30 日間)
Axel Schörling
Axel Schörling 2020 年 2 月 14 日
コメント済み: Jon 2020 年 2 月 14 日
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)

採用された回答

Jon
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 件のコメント
Axel Schörling
Axel Schörling 2020 年 2 月 14 日
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 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 ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by