Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Line Styles in a Graph in MATLAB

  1. Jan 26, 2017 #1

    Drakkith

    User Avatar

    Staff: Mentor

    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.

    Code (Matlab M):

    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:

    Code (Matlab M):

    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?
     
  2. jcsd
  3. Jan 26, 2017 #2

    Student100

    User Avatar
    Education Advisor
    Gold Member

    Have you tried plotting one, using hold on; and plotting the next?
     
  4. Jan 26, 2017 #3

    Student100

    User Avatar
    Education Advisor
    Gold Member

    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: Jan 26, 2017
  5. Jan 26, 2017 #4

    Drakkith

    User Avatar

    Staff: Mentor

    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.

    Code (Matlab M):
    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;
     
  6. Jan 26, 2017 #5

    Student100

    User Avatar
    Education Advisor
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted