Vector in a piecewise function

22 ビュー (過去 30 日間)
Daniel
Daniel 2013 年 9 月 10 日
編集済み: Walter Roberson 2017 年 2 月 21 日
So, I've trying to learn to use vectors in a piecewise function. I've got the function working properly, (code below). When I use any single number, (pwise(5), it works right. When I use a vector, say, pwise(-5:0:50), it gives me numbers I know are not correct. -5 should be -5, I'm just not sure what MatLab is doing. Any pointers?
function[v]=pwise(t)
%Figures Piecewise Function
if t<0
v=0
elseif t<8
v=10*t^2-5*t
elseif t<16
v=624-5*t
elseif t<26
v=36*t+12*(t-16)^2
else
v=2136*exp(-0.1*(t-26))
end
  1 件のコメント
Image Analyst
Image Analyst 2013 年 9 月 10 日
編集済み: Image Analyst 2013 年 9 月 10 日
What happens when you follow the normal debugging process and step through your code? I bet that would explain things. http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ And what do you expect to happen when you say "if [-5,-4,-3,-2,-1,0,1,2,3,4,5] < 0"? Do you expect it to automatically somehow form a loop over all those numbers and do the if statement for each number? It doesn't.

サインインしてコメントする。

採用された回答

Roger Stafford
Roger Stafford 2013 年 9 月 10 日
The "if-elseif-etc" construct you have there doesn't work the way you want for vectors. For example if you say "if t<8", for t a vector, it comes true only in case the proposition is true for all elements of t. You need something different for your problem to work with vectors.
One way is to write a for-loop that goes through the values of t one element at a time and figures out the corresponding value of v for each one using your "if elseif ..." stuff.
A vectorized method could also be done as follows, though I think you will find that the above for-loop method is easiest to do.
v = (10*t.^2-5*t).*((t>=0)&(t<8)) + ...
(624-5*t).*((t>=8)&(t<16)) + ...
(36*t+12*(t-16).^2).*((t>=16)&(t<26)) + ...
(2136*exp(-0.1*(t-26))).*(t>=26);
(Note that the t<0 case automatically gives zero for this and doesn't have to be written in specifically.)
  3 件のコメント
Roger Stafford
Roger Stafford 2013 年 9 月 11 日
You have to store the results of pwise(k) somewhere if you want to use it later such as in 'plot'. Remember though, its indices must be positive integers. If I were you I would write it like this where index k is known to be positive:
t = -5:1:50;
v = zeros(1,length(t));
for k = 1:length(t)
v(k) = pwise(t(k));
end
plot(t,v,'yo')
Of course if you use the vectorized version of pwise I gave you earlier, you can do the plot directly without the for-loop and its k index.
Daniel
Daniel 2013 年 9 月 11 日
編集済み: Daniel 2013 年 9 月 11 日
Beautiful! That's what I wanted to do, just didn't know how to say it. Thanks so much Mr. Stafford. I've learned a lot tonight. Have a good night.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 9 月 10 日
Try using .^ instead of ^ so that you square all elements, element by element.
  2 件のコメント
Daniel
Daniel 2013 年 9 月 10 日
I had tried that, didn't work. I think because of the reason below.
Image Analyst
Image Analyst 2013 年 9 月 11 日
Well it does work - it was at least part of the solution, as Roger showed, but not the whole solution. You should really learn when to use the dot in front of the operators.

サインインしてコメントする。


Sally Al Khamees
Sally Al Khamees 2016 年 12 月 23 日
編集済み: Sally Al Khamees 2017 年 2 月 21 日
If you have R2016b and the Symbolic Math Toolbox, you can just use the piecewise function with a vector
:

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by