Array indices must be positive integers or logical values.

2 ビュー (過去 30 日間)
As111
As111 2019 年 10 月 14 日
編集済み: Adam Danz 2019 年 10 月 14 日
x = (a + (i-1).*h)';
alphax = alpha(x);
f = f(x);
I get error: Array indices must be positive integers or logical value
ive tried storing alpha as another variable, but I don't understand.
Here is the full code:
----------------
function [u,x] = solveHeat(alpha, f, a, b, n, tolerance, maxIterations)
h= (b-a)/(n-1);
i=1:n;
x=(a+ (i-1).*h)';
alphax=alpha(x);
f=f(x);
%define starting value of temperature, u
u= [a; zeros(n-2,1);b]
r(1)=0;
r(n)=0;
Dinverse(2:n-1)=1./(-2alphax(2:n-1).*(h^2));
Dinverse(1)=1;
Dinverse(n)=1;
for s =1:maxItreations
r(2:n-1)= (u(1:n-2)-(2+alphax(2:n-1).*h^2)).*u(2:n-1)...
+u(3:n)+(h^2)*f (2:n-1));
u(2:n-1)=u(2:n-1)-Dinverse(2:n-1').*r(2:n-1)';
if norm(r)<tol,break,end
end
end
code to call function
[u, x] = solveHeat(1, @(x) zeros(size(x)), 1, cosh(1), 20, 1e-6, 2000);
error = norm(u - cosh(x))
  2 件のコメント
Adam Danz
Adam Danz 2019 年 10 月 14 日
What are your inputs?
What is the full copy-pasted error message?
And where does ua and ub come from? They aren't defined in your function.
As111
As111 2019 年 10 月 14 日
Sorry fixed ua, ub
Inputs: [u, x] = solveHeat(1, @(x) zeros(size(x)), 1, cosh(1), 20, 1e-6, 2000)
Array indices must be positive integers or logical values.
Error in solveHeat (line 9)
alphax = alpha(x);

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 10 月 14 日
編集済み: Adam Danz 2019 年 10 月 14 日
The error is here
% For position in the column vector
x = (a + (i-1).*h)';
alphax = alpha(x);
You're indexing alpha using vector x. As the error indicates, indices need to be positive integers. Instead, x equals
x =
1
1.0286
1.0572
. . .
Also, the alpha input is just 1 so why why would that be indexed in the first place?
Lastly, your last two inputs don't appear in your function so they aren't doing anything.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by