Finding the trajectory of a ball
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2021-1-21
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?
采纳的回答
Steven Lord
2021-1-21
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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!