Error " Array indices must be positive integers or logical values."

1 回表示 (過去 30 日間)
Jessica Larry
Jessica Larry 2020 年 4 月 29 日
コメント済み: Jessica Larry 2020 年 4 月 29 日
This is the Question I am being asked to solve and this is the code I have so far...
T1 = 70; %deg F
T2 = 200; %deg F
W = 2; %ft
L = 2; %ft
x = 1; %ft
y = 1; %ft
for n=0:1.83:11
T(n)=(T2-T1)*(2/pi)*(2/n)*(sin(n*pi*x/L))*(sinh(n*pi*y/L))/sinh(n*pi*W/L)+T1;
end
I'm nor sure if my n for the for loop is correct since nmax is 11 and there needs to be six steps, but I keep getting an Error " Array indices must be positive integers or logical values."
Not sure how I can fix the Error.
  1 件のコメント
Rik
Rik 2020 年 4 月 29 日
As a side note: if your code is correct, then the only "correct and appropriate" use of a loop will be be this:
for dummy=1
T=%array operation that creates T
end

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

採用された回答

Rik
Rik 2020 年 4 月 29 日
You need to distinguish between the index of your variable in Matlab and the subscript in the mathematical sense.
sub=___;%hint: look at linspace
for n=1:numel(sub)
T(n)=___
  3 件のコメント
Rik
Rik 2020 年 4 月 29 日
If you use T(n) in Matlab that can mean two things: you are calling a function with an input argument, or you have a variable and are extracting a value from it. These two interpretations are strictly separated.
You are trying to fill an array with values. You may want to say that T has a specific value for n=0.1, but that doesn't fit either description. So you what you have to do is to create two arrays: one will hold the values of n, the other will hold the respective values of T.
T(0.1) result in an error because T is not a function. Store that 0.1 in another array and use integers to index:
sub=[0.1 0.2];
T(1)=SomeFunction(sub(1));
T(2)=SomeFunction(sub(2));
%don't do T(0.1), but do this:
T(sub==0.1)
Jessica Larry
Jessica Larry 2020 年 4 月 29 日
Oh ok that makes a lot of sense, thanks so much for the help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by