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!