Ben L
Ben L

Reputation: 83

Plot Multiple Lines with Different Scaling Matlab

I'm trying to plot a few waveforms onto the same plot in Matlab; brand new to it. I've tried plotting them all together with plot() but it doesn't scale them all appropriately. How would I go about scaling them? I read something online that uses hold on, but I'm getting the same issue I believe. What's the simple solution here?

t1 = 0:0.1:1000;
y1 = t1.^5-5*t1.^3+4*t1;

plot(t1, y1)
hold on

t2 = 0:0.0001:0.01;
y2 = -8*exp(-1000*t2) + 3;

plot(t2, y2)
hold on

t3 = 0:0.0001:0.6;
y3 = exp(-10*t3).*cos(100*t3);

plot(t3, y3)
hold on

%plot(t1, y1, t2, y2, t3, y3)

Upvotes: 0

Views: 246

Answers (1)

Max
Max

Reputation: 4045

Matlab is doing what you ask it to do: plotting everything to the same axis system (by the way, you only need to use hold on once, it is active until you change the axis or command hold off) You have three options

  • define the limits of the axis explicitly, either separately using xlim([xmin xmax]) and ylim([ymin ymax]) or jointly using axis([xmin xmax ymin ymax]) (in your case e.g. axis([0 0.6 0 3.3])
  • you may want to use separate axis in one plot, see yyaxis yyaxis left / yyaxis right to activate the axes. Note that this only provides two different axes scales
  • use subplots! (as Cris Luengo already stated) That would be in your case


% 1st subplot
t1 = 0:0.1:1000;
y1 = t1.^5-5*t1.^3+4*t1;

ax(1) = subplot(3,1,1);
plot(t1, y1)

% 2nd subplot
t2 = 0:0.0001:0.01;
y2 = -8*exp(-1000*t2) + 3;

ax(2) = subplot(3,1,2);
plot(t2, y2)

% 3rd subplot
t3 = 0:0.0001:0.6;
y3 = exp(-10*t3).*cos(100*t3);

ax(3) = subplot(3,1,3);
plot(t3, y3)

% link all axes (but only x direction)
linkaxes(ax,'x')
% your axes limits are so large that you probably don't want to link them

Upvotes: 1

Related Questions