Array indices must be positive integers or logical values Error

I'm trying to execute this for loop and I keep coming up with same error. I don't understand why this is happening so if you could help at all it would be greatly appreciated.
for t= linspace(0,99)
C_1=x0;
C_2=x0_dot+(Wn*x0);
x(t)= (C_1 + (C_2*t))*exp(-Wn*t);
end

 採用された回答

madhan ravi
madhan ravi 2018 年 11 月 4 日
編集済み: madhan ravi 2018 年 11 月 4 日

0 投票

t= linspace(0,99);
for i = 1:numel(t)
C_1=x0;
C_2=x0_dot+(Wn*x0);
x(i)= (C_1 + (C_2*t(i)))*exp(-Wn*t(i));
end
plot(t,x)
if x0, x0_dot and Wn are scalars meaning a single number then you can do something like:
t = linspace(0,99);
C_1=x0;
C_2=x0_dot+(Wn*x0);
x = (C_1 + (C_2.*t)).*exp(-Wn.*t);
plot(t,x)

7 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 4 日
編集済み: madhan ravi 2018 年 11 月 4 日
Note : some of your parameters are not defined , I suspect you don't need loop for your case
Emma Powderly
Emma Powderly 2018 年 11 月 4 日
Thank you but I get a new error on both options about the multiplication.
Error using * Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in Lab2 (line 23) x = (C_1 + (C_2.*t))*exp(-Wn.*t);
madhan ravi
madhan ravi 2018 年 11 月 4 日
just put .* instead of *
Emma Powderly
Emma Powderly 2018 年 11 月 4 日
Thanks if you don't mind me asking why doesn't the first option work when x0,x0_dot and Wn are scalars?
madhan ravi
madhan ravi 2018 年 11 月 4 日
actually the first option works but its useless to use a loop if you can do it directly why go around if you can take a shortcut?
Emma Powderly
Emma Powderly 2018 年 11 月 4 日
thanks I was following a flowchart and confused the for for the if loop I get it now thank you
madhan ravi
madhan ravi 2018 年 11 月 4 日
Anytime :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by