Plotting multiple line on Matlab but only last plot showed

Below is my code. I would like to have two lines show on the same plot, but the code below only plot the very last plot command. Can you advise me what I need to change to get the code to have two lines show, one for v_trace1 and second for vtrace2 on the same graph.
Ia = 1.5; Ib = 1; C = 2; R = 10; tau = R*C;
tstop = 200; % ms
V_inf = Ia*R;
h = 5;
V1 = 0; V_trace1 = [V1];
V2 =0; V_trace2 = [V2];
for t = h:h:tstop+100
% Euler method: V(t+h) = V(t) + h*dV/dt
V1 = V1 +h*(- (V1/(R*C)) + (Ia/C));
V2 = V2 +h*(- (V2/(R*C)) + (Ib/C));
% Stop current injection
if (t == tstop)
Ia = 0;
Ib = 0;
end
V_trace1 = [V_trace1 V1];
V_trace2 = [V_trace2 V2];
plot(0:h:t,V_trace1,'r')
plot(0:h:t,V_trace2,'r')
axis([0 tstop+100 -V_inf V_inf])
end

回答(2 个)

Hold the plot on:
Ia = 1.5; Ib = 1; C = 2; R = 10; tau = R*C;
tstop = 200; % ms
V_inf = Ia*R;
h = 5;
V1 = 0; V_trace1 = [V1];
V2 =0; V_trace2 = [V2];
for t = h:h:tstop+100
% Euler method: V(t+h) = V(t) + h*dV/dt
V1 = V1 +h*(- (V1/(R*C)) + (Ia/C));
V2 = V2 +h*(- (V2/(R*C)) + (Ib/C));
% Stop current injection
if (t == tstop)
Ia = 0;
Ib = 0;
end
V_trace1 = [V_trace1 V1];
V_trace2 = [V_trace2 V2];
plot(0:h:t,V_trace1,'b'); hold on;
plot(0:h:t,V_trace2,'r')
axis([0 tstop+100 -V_inf V_inf])
end

类别

帮助中心File Exchange 中查找有关 Material Sciences 的更多信息

标签

提问:

2012-9-16

Community Treasure Hunt

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

Start Hunting!

Translated by