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 CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by