Could someone check my work?

1 次查看(过去 30 天)
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!

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-9-6
if 0 > t & t > 8 is always false
  3 个评论
Mahdi
Mahdi 2013-9-6
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 2013-9-6
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

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2013-9-6
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by