Help Needed with Plotting 2d graph

3 vues (au cours des 30 derniers jours)
Ping
Ping le 15 Oct 2012
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)

Réponse acceptée

Walter Roberson
Walter Roberson le 15 Oct 2012
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 commentaire
Ping
Ping le 15 Oct 2012
oh thanks for point that out!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by