Borrar filtros
Borrar filtros

Could someone check my work?

1 visualización (últimos 30 días)
Aaron
Aaron el 6 de Sept. de 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!

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 6 de Sept. de 2013
if 0 > t & t > 8 is always false
  3 comentarios
Mahdi
Mahdi el 6 de Sept. de 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 el 6 de Sept. de 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

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 6 de Sept. de 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.

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by