matlab piece wise function
4 views (last 30 days)
Show older comments
Hello everyone,
I have this piece-wise function uEx as a function of some integers x and t. But I really can't decypher what exactly is going to turn out. Could you please help me to translate it to mathematical language?
uEx =@(x,t) (x>-0.5).*(x<0.5+0.5.*t).*(1.*(x>-0.5+t) + (x+0.5)./(1.0*t).*(x<-0.5+t));
Thanks :)
0 Comments
Answers (1)
Walter Roberson
on 25 Jan 2021
(x>-0.5)
So x is greater than -1/2
(x<0.5+0.5.*t)
and x < 1/2 + t/2.
If x is not in that range the the overall result will be 0. If it is in that range then the overall result is determined by the remainder:
(1.*(x>-0.5+t) + (x+0.5)./(1.0*t).*(x<-0.5+t))
That breaks into two contributions:
1.*(x>-0.5+t)
which is 1 if x > t - 1/2 and 0 otherwise
(x+0.5)./(1.0*t).*(x<-0.5+t)
which is (x + 1/2)/t if x < t - 1/2 and 0 otherwise.
Putting the parts together:
if x > -1/2 & x < (t+1)/2 & x < t - 1/2 then (x + 1/2)/t
if x > -1/2 & x < (t+1)/2 & x > t - 1/2 then 1
otherwise 0
There is probably a bug in the formula. Consider x = t - 1/2 then x > -0.5+t is false because x == -0.5+t and the > is strict not >= . So the case that contributes 1 is not invoked. And x<-0.5+t is false becuase x == -0.5+t and the < is strict not <= so the case that contributs (x+1/2)/t is not invoked. So at x = t - 1/2 neither branch is invoked and you are left with all 0. But if you examine the limit as x = t - 1/2 then if x >= t - 1/2 instead of x > t - 1/2 were coded then the contribution would be 1; if x <= t-1/2 instead of x<t-1/2 then (x+1/2)/t would be (t-1/2+1/2)/t which would be (t+0)/t which would be 1 unless t = 0 . The left and right side would both lead to 1 at that case, so the limit would be 1... but the code gives 0. Likely a bug.
The whole formula will fail for t == 0 exactly. The division by 1.0*t would give infinity and infinity times 0 gives nan.
See Also
Categories
Find more on Boundary Conditions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!