フィルターのクリア

How to use two different equations for different ranges of values within an array?

5 ビュー (過去 30 日間)
Bixius
Bixius 2018 年 8 月 1 日
回答済み: Will Fritz 2018 年 8 月 1 日
I have a range of values, x = 0:0.1:5 as my inputs.
I want to use
Eq.1 when 0 < x <= 1,
and
Eq.2 for 1 < x < 5.
and then plot(x,y)
if (x >= 0) && (x <= 1)
y = x^(0.5)
elseif (x > 1)
y = x^(-0.5)
end
  2 件のコメント
Will Fritz
Will Fritz 2018 年 8 月 1 日
Is your Eq1: y = x^(0.5) and Eq2: y = x^(-0.5)? Also, what are the specific bounds? The code snippet at the end has different bounds than the two initial bounds you stated
Bixius
Bixius 2018 年 8 月 1 日
yes, Eq1 is y = x^(0.5) and Eq.2 is y = x^(-0.5). and I apologise
I meant to write
if (x >= 0) && (x <= 1)
y = x^(0.5)
elseif (x > 1)
y = x^(-0.5)
end

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

採用された回答

Will Fritz
Will Fritz 2018 年 8 月 1 日
The following should work for your intended restrictions.
% initialize x
x = 0:0.1:5;
% initialize y with all zeros
y = zeros(1,numel(x));
for i = 1:numel(x)
if (x(i) >= 0) && (x(i) <= 1)
y(i) = x(i)^0.5;
else
y(i) = x(i)^(-0.5);
end
end
plot(x,y)
Check out this regarding for loops: MATLAB Doc 'for loop'
Also this, regarding if statements: MTALAB Doc 'if, elsif, else'

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by