How do I use subplot and hold command simultaneously?

This code works fine until I attempt to plot the bottom portion of the script. I can tell there is an issue with the way I'm using the hold command, but can't pinpoint exactly what is wrong. Any guidance on this is appreciated.
t=0:.75:9.75;
ThB=30-9.684*10^(-4).*t.^5+.02421.*t.^(4)-.1614.*t.^(3);
ThJ=20+6.1002*10^(-3).*t.^(5)-.152505.*t.^(4)+1.0167.*t.^(3);
for n=2:length(t)
vB_estimate(n)=(ThB(n)-ThB(n-1))/(t(n)-t(n-1));
vJ_estimate(n)=(ThJ(n)-ThJ(n-1))/(t(n)-t(n-1));
end
vB_actual=-5*9.684*10^(-4).*t.^(4)+4*.02421.*t.^(3)-3*.1614.*t.^(2);
vJ_actual=5*6.1002*10^(-3).*t.^(4)-4*.152505.*t.^(3)+3*1.0167.*t.^(2);
subplot(2,1,1);
plot(t,vB_estimate);
hold on;
plot(t,vB_actual);
xlabel('Time, s');
ylabel('Angular Velocity, deg/s');
title('Angular Velocity of Base Angle');
legend('Estimate','Actual');
hold off;
subplot(2,1,2);
plot(t,vJ_estimate);
hold on;
plot(t,vJ_actual);
xlabel('Time, s');
ylabel('Angular Velocity, deg/s');
title('Angular Velocity of Joint Angle');
legend('Estimate','Actual');
hold off;
vB_error=abs(vB_estimate-vB_actual);
vJ_error=abs(vJ_estimate-vJ_actual);
plot(t,vB_error);
xlabel('Time, s');
ylabel('Error of Angular Velocity Estimate, deg/s');
title('Absolute Error of Angular Velocity Estimates');
hold on;
plot(t,vJ_error);
legend('Base Angle','Joint Angle');
hold off;

5 Comments

When you get to
plot(t,vB_error);
The current axes is 212, but you turned hold off already, so this new plot() erases what you just drew in the subplot.
Okay, that's useful to know. Then how can I create an entirely new plot?
place a figure() command prior to the plot command.
figure()
plot(t_vB_error)
Hey you need to initialize the arrays which are in loop.
vB_estimate = zeros(1,length(t)) ;
vJ_estimate = zeros(1,length(t)) ;

Sign in to comment.

Answers (0)

Asked:

on 9 Nov 2018

Commented:

on 9 Nov 2018

Community Treasure Hunt

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

Start Hunting!