Help Needed with Plotting 2d graph

조회 수: 3 (최근 30일)
Ping
Ping 2012년 10월 15일
I am getting the error in my function code: Error using .* Matrix dimensions must agree, so i tried it without the "." and it still doesn't work. what am i doing wrong?
I am plotting a function called plot04 that plots yi = sin(x*i*π), where i = 1, 2, 3. I am defining the length of the guitar string to be an array of 100 equally spaced values over the interval [0, 1].
I know I am supposed to enable hold using the command hold on. Then, call plot three times to plot each harmonic. For the first harmonic, I have to set the linewidth property to 1. For the second harmonic, I have to set the linewidth property to 2. And for the third harmonic, I have to set the linewidth property to 3. Third, I have to disable hold using the command hold off.
code
function [y] = plot04(x,i)
x= 0:.01:1
i= [ 1 2 3]
y= sin (x .* i .* pi)
hold on
plot(x,y,'LineWidth',1)
plot(x,y,'LineWidth',2)
plot(x,y,'LineWidth',3)
hold off
ylabel('Amplitude')
legend('first harmonic','second harmonic','third harmonic')
lines = {'first harmonic','second harmonic','third harmonic'}
legend(lines)

채택된 답변

Walter Roberson
Walter Roberson 2012년 10월 15일
y = sin (x.' * i .* pi)
to get y to be correct for your purposes.
Setting the line width the way you do is not correct: you are drawing the same lines over and over again, rather than drawing different lines each time or rather than setting the linewidth property of existing lines. Consider assigning the output of plot() to a variable and working with that variable.
  댓글 수: 1
Ping
Ping 2012년 10월 15일
oh thanks for point that out!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by