I am confused as to what the matlab ODE function does. If i have the system dynamics, why can't i just manually integrate it myself and get the result. What exactly is the purpose of the ode45 function?
2 Answers
You are asking about the preference between analytical (manual) vs numerical integration. If you can manually solve an ODE, then, yes, you don't need to use the ODE command to integrate it numerically. You can just plug in the initial/final time, initial conditions, and system model parameters into the analytical solution to compute the numerical value of the solution.
However, as it is often the case, finding the analytical solution of the ODE is very difficult or time consuming. Especially, if you have a system of nonlinear differential equations, manually computing the solution is practically impossible. In that case, the only practical solution is to use numerical integration, such as the capability provided by the ODE45 command.
Also, as you probably know, the solution of the ODEs might be quite complicated depending on the inputs to the system (ie, the forcing function). So, if your goal is to analyze the response of the system to various types of inputs (sinusoidal, step, impulse, etc), manually deriving the analytical solution for each input type would be very hard. Whereas, changing the input type and rerunning the ODE45 command is virtually trivial.
Quoting the official docs:
All MATLAB® ODE solvers can solve systems of equations of the form y′=f(t,y), or problems that involve a mass matrix, M(t,y)y′=f(t,y). The solvers all use similar syntaxes.
The ode23s solver only can solve problems with a mass matrix if the mass matrix is constant.
ode15s and ode23t can solve problems with a mass matrix that is singular, known as differential-algebraic equations (DAEs). Specify the mass matrix using the Mass option of odeset.
ode45 is a versatile ODE solver and is the first solver you should try for most problems. However, if the problem is stiff or requires high accuracy, then there are other ODE solvers that might be better suited to the problem. See Choose an ODE Solver for more information.