MATLAB Line Styles in a Graph in MATLAB

AI Thread Summary
The discussion centers on plotting five periodic functions in MATLAB, with initial difficulties related to line styles and matrix size mismatches. The user initially attempted to plot using a basic loop but encountered errors when trying to apply different line styles. A key solution involved using the matrix multiplication operator '.*' instead of the standard '*', which resolved the size mismatch issue. After implementing this change, the user successfully plotted the functions with distinct line styles for each damping coefficient value. The final code included a loop to calculate and plot the functions while applying different styles based on the damping coefficient, along with appropriate labels and a legend for clarity.
Drakkith
Mentor
Messages
23,175
Reaction score
7,625
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?
 
Physics news 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 Drakkith

Similar threads

Replies
1
Views
2K
Replies
8
Views
2K
Replies
4
Views
4K
Replies
1
Views
2K
Replies
4
Views
1K
Replies
2
Views
3K
Replies
1
Views
4K
Back
Top