Could someone check my work?

1 vue (au cours des 30 derniers jours)
Aaron
Aaron le 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!

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 6 Sep 2013
if 0 > t & t > 8 is always false
  3 commentaires
Mahdi
Mahdi le 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 le 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

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 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.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by