MATLAB: Plot of a function

plottingvector

I´ve created a script that calculates the speed and altitude of a rocket vertically lauched and i don´t know how to plot the aceleration during the flight. This is one of the functions that I want and I use it to create the movement equation
function acel=aceleracion(V,h,t)
alfa=0; delta=0;
if t>=0 & t<=tcomb1
acel=empuje(h,t)./(Wmisil-t.*gasto1) - ...
resistencia_aerodinamica(V,h,alfa,delta,t)./(Wmisil-t.*gasto1)...
-g ;
end
if t>tcomb1 & t<tcomb2
acel=empuje(h,t)./(Wmisil2-t.*gasto2) - ...
resistencia_aerodinamica(V,h,alfa,delta,t)./(Wmisil2-t.*gasto2)...
-g ;
end
if t>=tcomb2
acel=empuje(h,t)./(W3) - ...
resistencia_aerodinamica(V,h,alfa,delta,t)./(W3)...
-g ;
end
end
whit the main script I have acomplished the values of "V" (velocity) and "h" (altitude) and also a vector "tiempo" . The problem appears when I try to plot the aceleration like this
figure(3);
plot(tiempo, aceleracion(altura(:,2),altura(:,1),tiempo),'-b', 'LineWidth',1)
xlabel('t [s]', 'FontSize',11);
ylabel('a [ m / s^2 ]', 'FontSize',11);
axis([0,tfin(end),-2*9.8,2*9.8]);
where "altura" is a vector with the solutio.ns for V and h. Can you see what is wrong? thanks

Best Answer

As Walter notes (and I missed in your original posting) since you've defined the time variable t as a vector going into the function, the vectorized definition of the '==' operation in Matlab is significant.
As given in the doc, Matlab evaluates the condition for each position in the comparison and returns another (logical) vector that is T by position in the vector. But a logical test in an if is T iff every location in the vector is true. Since that isn't the case in your function, the if fails and nothing is set.
You would have to either use a loop or the "Matlab-y" way is to write it as a vectorized function instead...
function acel=aceleracion(V,h,t)
alfa=0; delta=0;
accel=zeros(size(t)); % preallocate
ix=iswithin(t,0,tcomb1); % first section
acel(ix)=empuje(h,t)./(Wmisil-t.*gasto1) - ...
ix=iswithin(t,tcomb1,tcomb2); % second
acel=empuje(h,t)./(Wmisil2-t.*gasto2) - ...
ix = t>=tcomb2; % third...
acel=empuje(h,t)./(W3) - ...
end
where iswithin is my helper function at a higher level of abstraction.
NB: its test is inclusive so you'll have one point at the junction between R1 and R2 that's not the same as yours--if you want precisely yours need the case of noninclusive on left for the second region boundary.
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);