Finding the trajectory of a ball
Show older comments
Hello, I'm trying to find the trajectory of a ball thrown in the air. And while trying to implement the code into MATLAB, I get errors with it.
function yp = fball(t,y)
g = 9.81; % Gravitational field
c = 0.479; % Scaled drag coefficient
w = 40; % Horizontal wind speed
% Unpack for position and velocity
x = y(1:2); %I get an error here telling that i dont have enough input arguments
v = y(3:4); %I get an error here telling that i dont have enough input arguments
% Velocity relative to wind
vv = v;
vv(1) = vv(1)-w;
% Compute for acceleration
a = -c*norm(vv)*vv;
a(2) = a(2)-g;
% Return
yp = [v; a ];
% −− Sanity check trajectories computed with and without drag −−
theta = pi/3;
v0 = s*[cos(theta); sin(theta)];
% Computing the reference trajectory (absent air resistance )
x(y) = s*cos(theta)*t
y(t) = -g*t^2/2 + s*sin(theta)*t
% up to time tfinal = 2∗s∗sin(theta)/g
tfinal = 2*v0(2)/g;
tref = linspace(0,tfinal );
xref = v0(1)*tref;
yref = (v0(2)-g/2*tref).*tref;
% Compute the same reference trajectory with ode45
y0 = [0; 0; v0];
refopt = opt;
refopt .c = 0;
[tout,yout] = ode45(@(t,y) fball(t,y ), tref , y0);
% Compute a similar trajectory with air drag on (no wind)
dopt = opt;
dopt.w = 0;
[toutd,youtd] = ode45(@(t,y) fball(t,y), tref , y0);
% Visually comparing solutions for drag and no drag
plot(xref, yref , ' r: ' , youtd (:,1), youtd (:,2), 'b-');
legend('No drag', 'Drag');
end
Can someone help me with this problem?
2 Comments
Image Analyst
on 21 Jan 2021
I formatted your code for you. You can do this next time by clicking the CODE icon after you paste and highlight the code. What values did you pass in for t and y?
Nicolae Lungu
on 21 Jan 2021
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!