Help with Plotting Data & Function in Matlab

In summary, the student attempted to plot data but ran into problems because t had more elements than Q. Linspace may be the source of the problem. The student was able to get a plot to come up but is not confident it is correct.
  • #1
jdawg
367
2

Homework Statement


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! :)

Homework Equations

The Attempt at a Solution

 

Attachments

  • matlabhw1.png
    matlabhw1.png
    57.6 KB · Views: 455
  • matlab_question.png
    matlab_question.png
    8.8 KB · Views: 444
Physics news on Phys.org
  • #2
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:

Matlab:
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 that's what you got but the Q is only eight points.

So one solution is to modify the linspace arguments.
 
  • Like
Likes jdawg
  • #3
You can use the length function to evaluate vector lengths.
Then make sure they are the same size
 
  • Like
Likes jdawg
  • #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.
 

Attachments

  • mlab2.png
    mlab2.png
    34.4 KB · Views: 430
  • #5
jdawg said:
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.

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 );
 
  • Like
Likes jdawg
  • #6
jdawg said:
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.
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.
 
  • Like
Likes jdawg
  • #7
You were so much help! Thanks a bunch!
 

1. How do I plot data in Matlab?

To plot data in Matlab, you can use the plot function. First, create two vectors of the same length, one for the x-axis values and one for the y-axis values. Then, use the plot function and pass in the two vectors as arguments. You can also customize the plot with different colors, labels, and titles.

2. Can I plot multiple data sets on the same graph in Matlab?

Yes, you can plot multiple data sets on the same graph in Matlab by using the hold on command. This will allow you to plot multiple lines or data sets on the same figure. Just make sure to use the hold off command when you are finished plotting to prevent any additional data from being added to the same figure.

3. How do I plot a function in Matlab?

To plot a function in Matlab, you can follow a similar process as plotting data. First, define the function using the function keyword and specify the input and output variables. Then, create a vector of x-values and use the defined function to calculate the corresponding y-values. Finally, use the plot function to plot the x and y vectors.

4. Is it possible to change the scale of the axes on a plot in Matlab?

Yes, you can change the scale of the axes on a plot in Matlab by using the xlim and ylim functions. These functions allow you to specify the minimum and maximum values for the x and y axes, respectively. You can also use the axis function to set the limits for both axes at the same time.

5. Are there any other plotting options in Matlab besides the plot function?

Yes, there are other plotting options in Matlab besides the plot function. Some other commonly used functions for plotting data include scatter for creating scatter plots, bar for creating bar charts, and surf for creating 3D surface plots. You can also explore other options and customizations by looking at the documentation for the plot function and its related functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
816
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top