Line Styles in a Graph in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter Drakkith
  • Start date Start date
  • Tags Tags
    Graph Line Matlab
Click For Summary

Discussion Overview

The discussion revolves around plotting multiple periodic functions in MATLAB, specifically focusing on how to correctly implement line styles for clarity in the graphical output. Participants share code snippets and troubleshooting advice related to MATLAB's plotting functions.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in plotting five periodic functions with distinct line styles and shares their initial code.
  • Another participant suggests using the 'hold on;' command to plot multiple functions on the same graph.
  • A participant points out that an error occurs due to mismatched matrix sizes when trying to plot, recommending adjustments to ensure compatibility.
  • A later reply indicates that using the matrix multiplication operator '.*' instead of '*' resolves the plotting issue, allowing for the correct implementation of line styles.
  • Participants discuss the logic for setting different line styles based on the damping coefficient 'r' values within the loop.

Areas of Agreement / Disagreement

Participants generally agree on the need for matrix multiplication in MATLAB and the use of 'hold on;' for plotting multiple lines. However, there are variations in the specific implementations and troubleshooting approaches, indicating that multiple views remain on the best practices for plotting in MATLAB.

Contextual Notes

Some limitations include potential dependencies on specific MATLAB versions and the need for clarity on matrix dimensions when performing operations. The discussion does not resolve all uncertainties regarding the best methods for plotting functions.

Who May Find This Useful

This discussion may be useful for MATLAB users, particularly those interested in plotting techniques, handling matrix operations, and visualizing multiple datasets effectively.

Drakkith
Mentor
Messages
23,205
Reaction score
7,687
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   Reactions: Drakkith

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K