Finding the trajectory of a ball

7 visualizzazioni (ultimi 30 giorni)
Nicolae Lungu
Nicolae Lungu il 21 Gen 2021
Risposto: Steven Lord il 21 Gen 2021
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 Commenti
Image Analyst
Image Analyst il 21 Gen 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
Nicolae Lungu il 21 Gen 2021
arguments: the time t and the vector y(t).....

Accedi per commentare.

Risposta accettata

Steven Lord
Steven Lord il 21 Gen 2021
Do not call ode45 with fball as the ODE function from within fball itself! That way lies (at best) a recursion error. Call ode45 from outside fball.
If after making that change you're still receiving errors, please show us your updated code (with the ode45 call moved out of fball) and the full and exact text (all the text displayed in red and/or orange in the Command Window) of any errors and/or warnings you received when running your code.

Più risposte (0)

Prodotti


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by