How to make a function proceed in time when t is unknown

2 ビュー (過去 30 日間)
Maria Andersson
Maria Andersson 2014 年 2 月 2 日
コメント済み: Maria Andersson 2014 年 2 月 2 日
Hi!
I have a function, for example speed as following:
v= v0 + a.*(t-t0);
If I have the input variables v0, a and t0 but t is unknown and I know that for this specific step the process will continue until v=-20 m/s. How can I make matlab plot the whole process with small time steps (dt) until the speed is -20 m/s?
I'm thinking about using a while function such as:
while v>=-20
v0=46;
t0=2;
a=-9.81;
v= v0+a*(t-t0);
end
It would be great if anyone could help me with that cause I am having real hard trouble to find how this should be done and trial and error does not seem to be the best solution!
Thanks

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 2 日
編集済み: Mischa Kim 2014 年 2 月 2 日
I suggest computing the time tend at which v=-20 m/s by hand, since it is so simple and then use
v0 = 46;
t0 = 2;
a = -9.81;
t = linspace(t0, tend, 50);
for ii = 1:length(t)
v(ii) = v0 + a*(t(ii) - t0);
end
Alternatively, you can use your while loop instead
v(1) = v0;
t(1) = t0;
dt = 0.1; % or any other value
ii = 1;
while v(ii) >= -20
ii = ii + 1;
t(ii) = t(ii - 1) + dt;
v(ii) = v(ii - 1) + a*(t(ii) - t0);
end
  2 件のコメント
Mischa Kim
Mischa Kim 2014 年 2 月 2 日
編集済み: Mischa Kim 2014 年 2 月 2 日
Yep, as I was suggesting, compute tend by hand. Or add another line
tend = (-20 - v0)/a + t0;
The problem with the while-loop is that you might not exactly hit the -20 m/s. You can, but this will make your algorithm much more complex.
Maria Andersson
Maria Andersson 2014 年 2 月 2 日
When I run the code you suggested, I get the following error message:
Undefined function 't' for input arguments of type 'double'.
Error in Inl_Raket (line 21)
v(i)= 46 + a*(t(i)-2);
Any idea why this happens?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by