Need help with subplot

조회 수: 9 (최근 30일)
Ping
Ping 2012년 10월 15일
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

채택된 답변

Matt Fig
Matt Fig 2012년 10월 15일
편집: Matt Fig 2012년 10월 15일
x = 0:.01:pi;
subplot(3,1,1)
plot(x,sin(x))
subplot(3,1,2)
plot(x,sin(x*3))
subplot(3,1,3)
plot(x,sin(x*6))
Think of the arguments to SUBPLOT as setting up a grid. You want a 3-by-1 grid, so the first two arguments to subplot should be 3,1. The third argument is which of the axes on that grid you want to create. So when you say subplot(3,1,3) you are asking subplot to create the third axes on a 3-by-1 grid. This lets you do neat things like this:
x = 0:.01:pi;
subplot(3,1,1)
plot(x,sin(x))
subplot(3,1,2)
plot(x,sin(x*pi))
subplot(3,2,5)
plot(x,sin(x*2*pi))
subplot(3,2,6)
plot(x,sin(x*3*pi))
  댓글 수: 1
Ping
Ping 2012년 10월 15일
thanks! The lines are a bit different than from the graph but i think I can figure it out from here.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Wayne King
Wayne King 2012년 10월 15일
편집: Wayne King 2012년 10월 15일
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
Image Analyst 2012년 10월 15일
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
Ping
Ping 2012년 10월 15일
thanks! I got 3 different subplots just like in the picture. But how do I get 3 separate lines? My 3 graphs all have 3 lines on each plot instead of 1 line per plot.
Image Analyst
Image Analyst 2012년 10월 15일
make i 1, 2, or 3, not an array [1 2 3].

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by