Need help with subplot
Show older comments
Instructions:
Create a MATLAB function called plot05 that generates a 3x1 matrix of subplots, where yi = sin(x*i*π) appears in the ith subplot and i = 1, 2, 3. Define the length of the guitar string to be an array of 100 equally spaced values over the interval [0, 1]. Plot the first harmonic in subplot 1 and set the linewidth property to 1. Plot the second harmonic in subplot 2 and set the linewidth property to 2. And plot the third harmonic in subplot 3 and set the linewidth property to 3.
so i need to create a subplot that looks like this:
but my graph is way off--what is wrong with my code?
function [y] = plot05(x,i)
x= 0:.01:1
i= [ 1 2 3]
y = sin (x.' * i .* pi)
hold on
x = 0:.01:1;
subplot(3,3,1);
linewidth = 1;
plot(x,y,'LineWidth',linewidth)
hold off
hold on
x = 0:0.01:1;
subplot(3,3,4);
linewidth = 2;
plot(x,y,'LineWidth',linewidth)
hold off
hold on
x = 0:0.01:1;
subplot(3,3,7);
linewidth = 3;
plot(x,y,'LineWidth',linewidth)
hold off
Accepted Answer
More Answers (2)
Wayne King
on 15 Oct 2012
Edited: Wayne King
on 15 Oct 2012
Because you want
subplot(3,1,1)
subplot(3,1,2)
and not
subplot(3,3, ...)
subplot(3,3, ) means 3 rows and 3 columns, you just want 3 rows and 1 column
Image Analyst
on 15 Oct 2012
Try this:
x= 0:.01:1
i= [ 1 2 3]
y = sin (x.' * i .* pi)
hold on
x = 0:.01:1;
subplot(3,3,1:3);
linewidth = 1;
plot(x,y,'LineWidth',linewidth)
x = 0:0.01:1;
subplot(3,3,4:6);
linewidth = 2;
plot(x,y,'LineWidth',linewidth)
x = 0:0.01:1;
subplot(3,3,7:9);
linewidth = 3;
plot(x,y,'LineWidth',linewidth)
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
3 Comments
Image Analyst
on 15 Oct 2012
Edited: Image Analyst
on 15 Oct 2012
The key was doing 1:3, 4:6, and 7:9 to get all three slots in the row. Of course you could do subplot(3,1,1) which works okay in this case but sometimes you want to mix things up like have the top plot be three wide and have 6 single wide plots below it. So that's why using a range gives you additional flexibility that you might want to know about, and might need at some point. Also, your "hold" commands were unnecessary.
Ping
on 15 Oct 2012
Image Analyst
on 15 Oct 2012
make i 1, 2, or 3, not an array [1 2 3].
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!