duffing equation ODE45
21 views (last 30 days)
I solved the duffing equation using ODE45. Below is the code.
It works fine and plots as expected. However, I noticed that the difference of 't' is not same. How can I have the 't' value equally spaced?
tspan = [0 100];
function result = duff1(t,y)
c = 0.1;
result= [y(2); -c*y(2)-k*y(1)-k3*y(1).^3]
More Answers (1)
Daniel Dolan on 7 Mar 2019
Pass an array of tpan points. From the ode45 documentation:
tspan — Interval of integration
Interval of integration, specified as a vector. At minimum, tspan must be a two element vector [t0 tf] specifying the initial and final times. To obtain solutions at specific times between t0 and tf, use a longer vector of the form [t0,t1,t2,...,tf]. The elements intspan must be all increasing or all decreasing.
The solver imposes the initial conditions given by y0 at the initial time tspan(1), then integrates from tspan(1) to tspan(end):
- If tspan has two elements, [t0 tf], then the solver returns the solution evaluated at each internal integration step within the interval.
- If tspan has more than two elements [t0,t1,t2,...,tf], then the solver returns the solution evaluated at the given points. However, the solver does not step precisely to each point specified in tspan. Instead, the solver uses its own internal steps to compute the solution, then evaluates the solution at the requested points in tspan. The solutions produced at the specified points are of the same order of accuracy as the solutions computed at each internal step.Specifying several intermediate points has little effect on the efficiency of computation, but for large systems it can affect memory management.
The values of tspan are used by the solver to calculate suitable values for InitialStep and MaxStep:
- If tspan contains several intermediate points [t0,t1,t2,...,tf], then the specified points give an indication of the scale for the problem, which can affect the value of InitialStep used by the solver. Therefore, the solution obtained by the solver might be different depending on whether you specify tspan as a two-element vector or as a vector with intermediate points.
- The initial and final values in tspan are used to calculate the maximum step size MaxStep. Therefore, changing the initial or final values in tspan could lead to the solver using a different step sequence, which might change the solution.
Example: [1 10]
Example: [1 3 5 7 9 10]