Create a function based on some criteria

2 ビュー (過去 30 日間)
Deepro Bardhan
Deepro Bardhan 2021 年 9 月 17 日
コメント済み: Deepro Bardhan 2021 年 9 月 17 日
Suppose I want to create a function :
y=x^2 for x>5
x^3 for 0<x<=5
-1 for x<0
How to implement this in matlab without using fucntion?
Can it be done in single statement so that I can plot this or use in other functions?

採用された回答

KSSV
KSSV 2021 年 9 月 17 日
編集済み: KSSV 2021 年 9 月 17 日
if x <= 0
y = -1 ;
elseif x > 0 && x <= 5
y = x^3 ;
elseif x > 5
y = x^2 ;
end
  3 件のコメント
KSSV
KSSV 2021 年 9 月 17 日
x = 0:0.01:10;
y = zeros(size(x)) ;
y(x <= 0) = -1 ;
y(x > 0 & x <= 5) = x(x > 0 & x <= 5).^3 ;
y(x > 5) = x(x > 5).^2 ;
plot(x,y)
Deepro Bardhan
Deepro Bardhan 2021 年 9 月 17 日
Thanks . That works!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by