フィルターのクリア

Problem with my if else command

1 回表示 (過去 30 日間)
Wei Kang Chang
Wei Kang Chang 2019 年 5 月 24 日
コメント済み: Luna 2019 年 5 月 29 日
Hi all,
I have my code written as below, however it does not give me the plot that I want. Instead of using two equations according to the conditions. It gives me a graph using the else equation regardless of the value of x_15. Can anyone help me with this?
x_15=0:40:600;
if x_15<200
y_15=75*(b+500)/(a+50)*(cos(pi*x_15/200))+75*(b+500)/(a+50);
else
y_15=(exp(-x_15/150)).*(((7*(b+110)/(a+10))*(cos(pi*x_15/200)))+(7*(b+110)/(a+10)));
end
scatter(x_15,y_15);
title('S-Shaped Pipeline FEA Model');
xlabel('X position');
ylabel('Y Position');
Thank you.
Regards \
Wei Kang

採用された回答

Stephen23
Stephen23 2019 年 5 月 24 日
編集済み: Stephen23 2019 年 5 月 24 日
An IF statement is not appropriate (without a loop). The simple MATLAB way is to use indexing:
>> a = 1;
>> b = 1;
>> x_15 = 0:40:600;
>> idx = x_15<200; % INDEX!
>> y_15 = (exp(-x_15/150)).*(((7*(b+110)/(a+10))*(cos(pi*x_15/200)))+(7*(b+110)/(a+10)));
>> y_15(idx) = 75*(b+500)/(a+50)*(cos(pi*x_15(idx)/200))+75*(b+500)/(a+50);
% ^^^^^ ^^^^^ INDEXING!
And checking:
>> plot(x_15,y_15)
untitled.png
  1 件のコメント
Luna
Luna 2019 年 5 月 29 日
+1 :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by