How to solve ODE (vector of first order equations)?
2 views (last 30 days)
Show older comments
Hello fellow community
I want to solve an ODE for particle trajectory;
function dpos = Throw(pos)
mu = 1;
g = 10;
dpos(1) = pos(3);
dpos(2) = pos(4);
dpos(3) = -mu * pos(3)* sqrt(pos(3)^2 + pos(4)^2);
dpos(4) = -g - mu * pos(4) * sqrt(pos(3)^2 + pos(4)^2);
end
and there is a problem i encountered right in the beginning:
ode45(@Throw,[0 2],[0 1 1 1])
Error using Throw
Too many input arguments.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Your help will be apreciated.
Jan
0 Comments
Accepted Answer
Star Strider
on 16 Nov 2020
The numerical ODE solvers pass a time scalar and a vector to the ODE functions they integrate, in that order, so the function must account for them. Also, by default, MATLAB will create a row vector for the ‘dpos’ elements, so it is necessary to define them as a column vector, since the ODE solvers require that.
This version runs without error:
function dpos = Throw(t,pos)
mu = 1;
g = 10;
dpos = zeros(4,1);
dpos(1) = pos(3);
dpos(2) = pos(4);
dpos(3) = -mu * pos(3)* sqrt(pos(3)^2 + pos(4)^2);
dpos(4) = -g - mu * pos(4) * sqrt(pos(3)^2 + pos(4)^2);
end
[t,pos] = ode45(@Throw,[0 2],[0 1 1 1]);
figure
plot(t, pos)
grid
.
2 Comments
More Answers (0)
See Also
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!