Plotting Y as a Function of Time: MATLAB Homework Help

  • Thread starter bcjochim07
  • Start date
In summary: To simplify the code, you could also do:y(i)=y(i-1)+v(i-1)*h+0.5*a*h*h;asy(i) = y(i-1) + v(i-1)*h + 0.5*(fdrag/m + g)*h*h;In summary, the conversation is about writing a program to plot the displacement of a 75.0 g ball with radius 10cm as a function of time, taking into account the acceleration due to gravity and drag force. The code provided attempts to do this, but is producing an error due to incorrect use of matrix multiplication. It is recommended to have a thorough understanding of the problem before coding and to use the correct formula
  • #1
bcjochim07
374
0

Homework Statement


A 75.0 g ball with radius 10cm is dropped. I am supposed to write a program that plots y as a function of time, including acceleration due to gravity and drag force of D=0.25*area*velocity.


Homework Equations





The Attempt at a Solution


Here's what I tried:

>> n=21;
>> t=zeros(1,n);
>> v=zeros(1,n);
>> y=zeros(1,n);
>> h=0.5;
>> g=-9.80;
>> m=0.075;
>> A=pi*0.10*0.10;
>> for i = 2:n
t(i)=(i-1)*h;
fdrag=0.25*A*v*v;
adrag=fdrag/mass;
a=adrag+g;
v(i)=v(i-1)+a*h;
y(i)=y(i-1)+v(i-1)*h+0.5*a*h*h;
end;
? Error using ==> mtimes
Inner matrix dimensions must agree.
>>

t is time, h is time increment, m is mass, a is total acceleration, A is area

I don't understand the error that I am making. Could someone please help me?
 
Physics news on Phys.org
  • #2
Well, the cause for that error is that the default multiplication operation in Matlab is the matrix multiply. For something like this, you do not want the matrix multiplication - you want to extract just the item you are currently working on and use that value. There are some other problems too, but we can start by fixing that.

Also, it is always best to have a thorough understanding of exactly what you are trying to get the code to do in each section before coding - do you have all of that worked out?
 
  • #3
Complementing what cjl said, you should make:
fdrag=0.25*A*v(i-1)*v(i-1);
 

1. What is the purpose of plotting Y as a function of time in MATLAB?

The purpose of plotting Y as a function of time in MATLAB is to visualize the relationship between two variables. This can help in understanding patterns, trends, and changes over time.

2. How do I plot Y as a function of time in MATLAB?

To plot Y as a function of time in MATLAB, you can use the plot function with the time values as the x-axis and the Y values as the y-axis. You can also use other plot types, such as scatter or line, depending on your data and the desired visualization.

3. What are the benefits of using MATLAB for plotting Y as a function of time?

MATLAB offers a variety of tools and functions that make plotting Y as a function of time efficient and customizable. It also allows for easy manipulation and analysis of the data, as well as the ability to add annotations, labels, and other visual elements to the plot.

4. Can I customize the appearance of the plot when plotting Y as a function of time in MATLAB?

Yes, you can customize the appearance of the plot in MATLAB by using various formatting options such as changing the color, line style, and markers. You can also add titles, axes labels, legends, and other annotations to make the plot more visually appealing and informative.

5. Can I plot multiple Y variables as functions of time in one plot using MATLAB?

Yes, you can plot multiple Y variables as functions of time in one plot using MATLAB. Simply include all the Y values in the plot function, and specify the corresponding time values for each Y variable. You can also add a legend to differentiate between the different Y variables on the plot.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
825
  • Engineering and Comp Sci Homework Help
Replies
6
Views
855
  • Engineering and Comp Sci Homework Help
Replies
3
Views
806
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
954
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
939
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
Back
Top