Could someone check my work?

2 views (last 30 days)
Aaron
Aaron on 6 Sep 2013
I've created a function that will calculate a piecewise function to be called on. I want to make sure that it is okay.
Here is my code:
function v = vfun(t)
t = -5:50;
if 0 > t & t > 8
v = (10.*t)^2 - (5.*t);
elseif 8 <= t & t <= 16
v = 624 - 5.*t;
elseif 16 <= t & t <= 26
v = 36.*t + 12.*(t-16).^2;
elseif t > 26
v = 2136*exp(-0.1.*(t-26));
end
I am also going to be creating a script that will plot v vs t for t = -5 to 50. I'm not too sure that I should have included that above.
Thanks!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 6 Sep 2013
if 0 > t & t > 8 is always false
  3 Comments
Mahdi
Mahdi on 6 Sep 2013
Also, if you are using it as a function, then you shouldn't define t again (because it is an input).
t = -5:50;
Based on what you're doing, you should probably remove that line.
Azzi Abdelmalek
Azzi Abdelmalek on 6 Sep 2013
t = -5:50;
v=zeros(1,numel(t))
idx1=0<= t & t <8
v(idx1)=(10.*t(idx1)).^2 - (5.*t(idx1));
idx2=8 <= t & t <= 16
v(idx2) = 624 - 5.*t(idx2);
% and so on

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 6 Sep 2013
Mahdi: Check this out and you will never have to wait for us to answer again: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ You'd be able to see exactly where in your code it was executing, and the variable values.

Categories

Find more on Introduction to Installation and Licensing 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!