How to solve a function with different ranges

조회 수: 5 (최근 30일)
Delia Bosshart
Delia Bosshart 2021년 6월 15일
편집: Scott MacKenzie 2021년 6월 15일
I have a variable that is composed of two functions for different ranges:
alfa_ox = 1/15*C_ox for 0 <= C_ox <= 15
alfa_ox = 1 for C_ox > 15
When I imply this in Matlab, it doesnt consider the second condition (that alfa can't be bigger than 1). How can I solve that?
C_ox = [16;15;7.5;2;7.5;15;15];
if C_ox > 15
alfa = 1
else alfa = 1/15.*C_02
end
Output:
alfa =
1.0667
1.0000
0.5000
0.1333
0.5000
1.0000
1.0000
  댓글 수: 6
Delia Bosshart
Delia Bosshart 2021년 6월 15일
Yes it is a vector. I could already solve the problem with the answers provided, thank you.
Scott MacKenzie
Scott MacKenzie 2021년 6월 15일
편집: Scott MacKenzie 2021년 6월 15일
Sorry for the string of comments here, but your question uses the expression
1/15*C_ox
In your code we see
1/15.*C_02
Please clarify.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2021년 6월 15일
alfa_fcn = @(C_ox) min(C_ox/15,1);
C_ox = [16;15;7.5;2;7.5;15;15];
alfa = alfa_fcn(C_ox)
alfa = 7×1
1.0000 1.0000 0.5000 0.1333 0.5000 1.0000 1.0000
.

추가 답변 (1개)

Alan Stevens
Alan Stevens 2021년 6월 15일
Try
C_ox = [16;15;7.5;2;7.5;15;15];
C_ox(C_ox<=15)=C_ox(C_ox<=15)/15;
C_ox(C_ox>15)=1
C_ox = 7×1
1.0000 1.0000 0.5000 0.1333 0.5000 1.0000 1.0000

카테고리

Help CenterFile Exchange에서 System Composer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by