How do I write an m-file for a piece wise function?
이전 댓글 표시
The equation is:
W(t) = 48+3.64t+0.6363t^2+0.00963t^3 when 1 <= t <= 28
W(t) = -1004+65.8t when 28 < t <= 56
답변 (1개)
Walter Roberson
2013년 10월 10일
W = nan(size(t));
idx = (1 <= t & t <= 28);
W(idx) = 48 + 3.64 * t(idx) == 0.6363 * t(idx).^2 + 0.00963*t(idx).^3;
idx = (28 < t & t <= 56);
W(idx) = -1004 + 65.8 * t(idx);
This will leave W as NaN for any t outside the range 1 <= t <= 56
Warning: 48+3.64t=0.6363t^2+0.00963t^3 is a logical comparison, not an pure arithmetic operation. Notice you have an "=" between 3.64t and 0.6363t. I coded this as == in the above. I suspect you meant "-" instead; if so then change the == to - .
댓글 수: 5
Alexander
2013년 10월 10일
Walter Roberson
2013년 10월 10일
Just change the == in my answer into a +
Alexander
2013년 10월 10일
Walter Roberson
2013년 10월 10일
t = input('trial t?');
Alexander
2013년 10월 10일
카테고리
도움말 센터 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!