1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab plotting

  1. Jun 8, 2016 #1
    1. The problem statement, all variables and given/known data
    I'm a little lost on how to plot this data and function. I included the homework question and my attempt at plotting in the attached picture. I'm pretty sure what I have is completely wrong and I honestly don't have much of an understanding of matlab, so the more you dumb it down the better. Any help is greatly appreciated! :)

    2. Relevant equations


    3. The attempt at a solution
     

    Attached Files:

  2. jcsd
  3. Jun 8, 2016 #2

    jedishrfu

    Staff: Mentor

    Your t array has more elements than your Q array and so MATLAB can't pair them together for an (x,y) point to plot.

    As an example:

    Code (Matlab M):

    x=[0:1:10]
    y=x.*x
    plot(x,y)
     
    produces two vectors ##x## and ##y## where ##y=x^2## and so the plot(x,y) draws a simple parabola.

    In your case, MATLAB can't match up each value in t with one in Q and hence issues the error message you got.

    Ask yourself why does t have a different length from Q? Isn't Q dependent on t somehow?

    Here's the definition of linspace which may be the source of your problem:

    http://www.mathworks.com/help/matlab/ref/linspace.html?s_tid=gn_loc_drop

    Notice you used a 3 argument version where you want to create a vector t from 0 to 10 and you want 100 points and thats what you got but the Q is only eight points.

    So one solution is to modify the linspace arguments.
     
  4. Jun 9, 2016 #3

    donpacino

    User Avatar
    Gold Member

    You can use the length function to evaluate vector lengths.
    Then make sure they are the same size
     
  5. Jun 9, 2016 #4
    Ok, I tried changing a few things and managed to get a plot to come up, but I'm not super confident that its right. I was thinking that I was supposed to use linspace somehow to plug t values into my q function and then plot the actual Q and t vectors as data points on the same graph.. I hope that makes sense.

    Or could I just say something like plot q and then somehow add the Q and T vectors onto the same plot as data points?

    Also, did I type the q function correctly? Matlab keeps giving me errors when I try to run it.
     

    Attached Files:

  6. Jun 9, 2016 #5

    donpacino

    User Avatar
    Gold Member

    A few things....
    1. that plot looks correct.
    2. you can use the following commands to make your plot look better
    xlabel('string');
    ylabel('string');
    title('string')
    legend('plot1','plot2',...)

    3. exp is a function, get rid of the ^
    exp(stuff) will execute the math function e^stuff

    4. just do this

    plot( t , vector1 , t , vector2 );
     
  7. Jun 9, 2016 #6

    donpacino

    User Avatar
    Gold Member

    in general the matlab forums are VERY good.

    when you get an error, just toss it in google.
    when you want to say plot two vectors, type "matlab plot two vectors" into google. You'll be surprised by what has already been answered and the guides mathworks have put together.
     
  8. Jun 9, 2016 #7
    You were so much help!! Thanks a bunch!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Matlab plotting
  1. Matlab plot (Replies: 10)

  2. Plot in matlab (Replies: 1)

  3. Matlab Plots (Replies: 2)

  4. Matlab, plot (Replies: 5)

  5. Matlab plots (Replies: 1)

Loading...