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

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

Réponses (1)

Hernia Baby
Hernia Baby le 22 Jan 2023
Modifié(e) : Hernia Baby le 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 commentaire
Atsushi Ueno
Atsushi Ueno le 22 Jan 2023
Modifié(e) : Atsushi Ueno le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur データ型の識別 dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!