Line Styles in a Graph in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter Drakkith
  • Start date Start date
  • Tags Tags
    Graph Line Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
Messages
23,209
Reaction score
7,696
I'm trying to plot 5 periodic functions on the same graph in MATLAB and I can't seem to figure out how to do it correctly. The following code will work, except that I still need to change the line style for each line so that they are easy to tell apart.

Matlab:
k = 1;     %spring constant
w0 = sqrt(k);
z = 0;     %Used to count the elements of y, which is used in the for loops below.

%loop to plot a periodic function for 5 different values of r, the damping coefficient.
for r = 0:0.5:2
    w1 = sqrt(w0^2-(r^2/4));
    for t = 0:0.1:10
        z=z+1;
        %Performs the calculation for each data point.
        y(z) = exp(-r*t/2)*sin(w1*t);
    
    end
    %Performs the plot of each function for each iteration of the outer loop.
    plot(y);
 
    z=0;
end

According to my book, the command used to plot with a different line style is: plot(x, y, '--')
However, any time I try to plot the above function using a command similar to plot(x, y), I just get a blank plot.

I've also tried using:

Matlab:
t = 0:0.1:10;
plot(t, exp(-r*t/2)*sin(w1*t));
But this just gives me an error, saying that the inner matrices must match or something.

Any ideas?
 
on Phys.org
Drakkith said:
I'm trying to plot 5 periodic functions on the same graph in MATLAB and I can't seem to figure out how to do it correctly. The following code will work, except that I still need to change the line style for each line so that they are easy to tell apart.

Have you tried plotting one, using hold on; and plotting the next?
 
Drakkith said:
Matlab:
t = 0:0.1:10;
plot(t, exp(-r*t/2)*sin(w1*t));
But this just gives me an error, saying that the inner matrices must match or something.

Any ideas?

It's giving you an error because the sizes don't match. Try specifying the t element you want for the particular plot. Or changing r/w1 to match sizes.
 
Last edited by a moderator:
Thanks Student100.
I managed to figure it out right after you posted, lol.
Turns out I needed to use the matrix multiplication operator '.*' instead of just '*' when multiplying sin() by exp().
Once I changed that my plot(x, y) started working correctly and I could add in the logic to set the line style.

Matlab:
k = 1;  %spring constant
w0 = sqrt(k);

%Calculations and Printout
hold;

%loop to plot a periodic function for 5 different values of r
for r = 0:0.5:2
    w1 = sqrt(w0^2-(r^2/4));  
    t = 0:0.1:10
    %Performs the calculation for each data point
    y = exp(-r*t/2).*sin(w1*t); 
    if (r == 0.5)
        plot(t, y, '--');
    end
    if (r == 1)
        plot(t, y, ':');
    end
    if (r==1.5)
        plot(t, y, '-.');
    end
    if (r == 2|| r==0)
        plot(t, y);
    end
end
legend('r=0', 'r=0.5', 'r=1', 'r=1.5', 'r=2');
xlabel('Seconds');
ylabel('Amplitude');
title('Amplitude vs Time');
hold;
 
Drakkith said:
Thanks Student100.
I managed to figure it out right after you posted, lol.
Turns out I needed to use the matrix multiplication operator '.*' instead of just '*' when multiplying sin() by exp().
Once I changed that my plot(x, y) started working correctly and I could add in the logic to set the line style.

Matlab:
k = 1;  %spring constant
w0 = sqrt(k);

%Calculations and Printout
hold;

%loop to plot a periodic function for 5 different values of r
for r = 0:0.5:2
    w1 = sqrt(w0^2-(r^2/4)); 
    t = 0:0.1:10
    %Performs the calculation for each data point
    y = exp(-r*t/2).*sin(w1*t);
    if (r == 0.5)
        plot(t, y, '--');
    end
    if (r == 1)
        plot(t, y, ':');
    end
    if (r==1.5)
        plot(t, y, '-.');
    end
    if (r == 2|| r==0)
        plot(t, y);
    end
end
legend('r=0', 'r=0.5', 'r=1', 'r=1.5', 'r=2');
xlabel('Seconds');
ylabel('Amplitude');
title('Amplitude vs Time');
hold;

Cool glad you figured it out. :smile:

Was trying to get it to run in my version, but was having some problems. :sorry: Finally just got it working so I could see what it was trying to do. New code is working pretty flawlessly here.
 
  • Like
Likes   Reactions: Drakkith