Maple piecewise to MATLAB

조회 수: 6 (최근 30일)
Bibha
Bibha 2011년 4월 28일
Hi,
I am learning Matlab and stumble on this. I have an expression in Maple and trying to write it in Matlab but my basic knowledge plus some search went down the drain. So, I am here seeking some help.
In Maple: result:= (tb,t,tr,i)->piecewise(t<=tb,0 ,tb<t and t<=tb+tr, i*t, t>tb+tr and t<=500, 100, t>500,t-100);
In simple terms:
tb >= t then 0
tb < t <= tb+tr then i*t
tb+tr < t <= 500 then 100
500 < t then t-100
Now, t varies from 1 to 100 with 100 points. [ linspace(1,100,100]
What would be the equivalent of this expression in Matlab?
Many thanks in advance.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 4월 28일
variant
function y = resultmatlab(t,tb,tr,ii)
p = t(:);
y = zeros(size(p));
t1 = p<=tb; y(t1) = 0;
t2 = p > tb & p <= tb + tr; y(t2) = ii*p(t2);
t3 = p>tb+tr & p<=500; y(t3) = 500;
t4 = ~(t1|t2|t3); y(t4) = p(t4)-500;
y = reshape(y,size(t));
use (Command window)
>> y = resultmatlab(1:100,20,40,100)
  댓글 수: 1
Bibha
Bibha 2011년 4월 28일
fantabulous!
It worked perfectly. Thank you again.

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

추가 답변 (1개)

John D'Errico
John D'Errico 2011년 4월 28일
The simple answer is to download my piecewise_eval from the file exchange.
John
  댓글 수: 1
Bibha
Bibha 2011년 4월 28일
Thanks for the link John.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by