Plotting Y as a Function of Time: MATLAB Homework Help

  • Thread starter Thread starter bcjochim07
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on a MATLAB programming challenge involving the simulation of a 75.0 g ball dropped from a height, accounting for gravitational acceleration and drag force. The user encountered an error related to matrix multiplication in MATLAB while attempting to calculate the drag force and update the ball's velocity and position over time. The solution involves correcting the drag force calculation to use the previous velocity value and ensuring proper indexing in the loop. Key corrections include changing the drag force calculation to fdrag=0.25*A*v(i-1)*v(i-1); to avoid matrix dimension errors.

PREREQUISITES
  • Understanding of MATLAB programming syntax and operations
  • Familiarity with basic physics concepts such as gravity and drag force
  • Knowledge of numerical methods for simulating motion
  • Experience with array indexing and manipulation in MATLAB
NEXT STEPS
  • Review MATLAB's matrix operations and how they differ from element-wise operations
  • Learn about numerical integration techniques for simulating motion
  • Explore MATLAB's plotting functions to visualize results effectively
  • Investigate the effects of varying drag coefficients on motion simulations
USEFUL FOR

Students studying physics or engineering, MATLAB programmers working on simulations, and anyone interested in numerical modeling of motion under forces.

bcjochim07
Messages
366
Reaction score
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
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?
 
Complementing what cjl said, you should make:
fdrag=0.25*A*v(i-1)*v(i-1);
 

Similar threads

Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
15
Views
3K