How to plot a piece-wise function so I can then shift/compress it afterwards
3 ビュー (過去 30 日間)
古いコメントを表示
Good afternoon everyone. I am trying to find a way to plot the following piecewise function in MATLAB. I tried to use the inline function to create each piece but I am not sure how to plot it as a piecewise. I tried to use a heaviside function but it looked a bit off. Here is my piecewise I have to plot for x(t):
from 0<=t<2 x(t)=(1//2)exp(t).....
from 2<=t<4 x(t)= 2*t-8.....
0 otherwise
afterwards i have to do the code for x(-t-1) which is an axis flip and a left shift I believe.
Any help from the gurus? I know there is more than one way to skin a cat but seeing as how I will be manipulating the piecewise function by shifting it and compressing it I would think it would need to be strictly defined in range
0 件のコメント
採用された回答
Walter Roberson
2016 年 10 月 15 日
syms x(t) shifted_x(t)
x(t) = evalin(symengine, 'piecewise([0 <= t and t < 2, exp(t)/2], [2 <= t and t < 4, 2*t-8], [t < 0 or t >= 4, 0])');
shifted_x(t) = x(-t-1);
I do not recommend this approach. The evalin(symengine) is ugly and relies upon a feature that is probably going to be removed. It is, however, the only way to create a symbolic piecewise . If you need to do something like integrate this or laplace transform then you would need the symbolic version.
If you do not need symbolic manipulation then you should use logical indexing, possibly together with a "trick":
x = @(t) (0 <= t & t < 2) .* exp(t)/2 + (2 <= t & t < 4) .* (2*t-8);
shifted_x = @(t) x(-t-1);
This trick will not work if any of your formula can produce a NaN or inf at a location where the piecewise is not intended to apply. For example,
(x ~= 0) .* 1./x + (x == 0) .* (-1) .* ones(size(x))
will fail if x becomes 0, because even though the (x ~= 0) seems to mask out the 1./x, the 1./x will be evaluated anyhow and will return inf and the false of (x ~= 0) multiplied by inf will give NaN.
But for example,
(x > 0) .* 1./max(x, eps) + (x <= 0) .* (-1) .* ones(size(x))
would be okay because at those negative and 0 values, max(x,eps) would be a non-zero value and 1 divided by that value would not be inf or nan, and then the (1/eps) that resulted would be multipled by the false of (x > 0) giving 0 which is fine.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Assumptions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!