Could someone check my work?

조회 수: 1 (최근 30일)
Aaron
Aaron 2013년 9월 6일
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