How to change a function within a loop at certain point in MATLAB

1 回表示 (過去 30 日間)
Sebastian Kudera
Sebastian Kudera 2020 年 3 月 5 日
編集済み: Stephen23 2020 年 3 月 5 日
I have this piece of code:
x=0:20000;
h0=10;
alfa0=-0.1*pi/180;
h=h0+tan(alfa0).*x+10^(-6)*-0.4.*x.^2/(2*(cos(alfa0)^2));
Now, when executed it gives me this graph which shows that h is decreasing as x is increasing.
I would like Matlab to stop when h gets to 0, and then use a different function for h. Let's say
h=2.*x
so we have an actual example.
I tried some stuff with while loops and if statements, but I am getting nowhere. Thanks for the help.

回答 (1 件)

Stephen23
Stephen23 2020 年 3 月 5 日
編集済み: Stephen23 2020 年 3 月 5 日
" I tried some stuff with while loops and if statements..."
That is entirely the wrong approach in MATLAB. Just use logical indexing:
idx = h<=0;
h(idx) = 2.*x(idx);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by