条件式等を使って,途​中で式を変更する方法​が知りたいです.

5 views (last 30 days)
祐亮
祐亮 on 22 Jan 2023
Edited: Hernia Baby on 22 Jan 2023
例えばですが,x-y座標において y=2x のグラフで,代入するxが∼以上(例,x=3)の時にy=3xに式を変更するような条件式を知りたいです.以下のようなコードの場合,何を加えれば良いでしょうか.
t = linspace ( 1 , 10 )
y=2*x
以下の画像のように途中で式が変わる方法が知りたいです.

Answers (1)

Hernia Baby
Hernia Baby on 22 Jan 2023
Edited: Hernia Baby on 22 Jan 2023
今回はインデックスで条件判定します
追記:問題のグラフみて書き換えました
t = linspace(0,10);
y = zeros(1,length(t));
threshold = 3;
idx = t >= threshold; % 3以上か?
y(~idx) = 2.*t(~idx); % 3未満は2t
y(idx) = 3.*t(idx) - (3-2)*threshold; % 3以上は3t(連続にしました)
描写します
plot(t,y)
  1 Comment
Atsushi Ueno
Atsushi Ueno on 22 Jan 2023
Edited: Atsushi Ueno on 22 Jan 2023
ほぼ同じ意見です。
こうすれば目的の閾値を求める様子が良く分かるのではないでしょうか?
代数的に求めれば良い話ですが、数値計算で求める様子が判るかと思います。
t = linspace(0,10);
for thresh = 0:0.1:10
idx = t >= thresh; % thresh以上か?
y(~idx) = 2.*t(~idx); % thresh未満は2t
y(idx) = 3.*t(idx) - (3 - 2) * thresh;
% thresh以上は3t (継ぎ目を合わせる)
plot(t,y);
if y(end) <= 25.0 % 時間通りにcafeに着いたthreshを表示して終わり
thresh
break;
end
pause(0.1); % 変化を見る為のウェイト
end

Sign in to comment.

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!