# duffing equation ODE45

37 views (last 30 days)
Ramya Raman on 7 Mar 2019
Commented: ARPIT KUMAR on 16 Nov 2020
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];
x0=[1,0];
[t,y]=ode45(@duff1,tspan,x0)
figure(1)
%first value
% subplot(221)
plot(t,y(:,1)); %plot(t,y);
xlabel('Time');
ylabel('State');
title('k3=0,k=1,x0=1');
function result = duff1(t,y)
c = 0.1;
k3=0;
k=1;
result= [y(2); -c*y(2)-k*y(1)-k3*y(1).^3]
end
ARPIT KUMAR on 16 Nov 2020
I did the coding for duffing eq but getting error , I am attaching the code below, kindly help me rectify the error.
clc % Time Response and Phase portrait of Duffing Oscillator global muglobal mu om1 om2 alpha mu=0.8; alpha=0.1 om1=2; om2=2*sqrt(2); [T,Y] = ode45(@ff22,[0 5000],[0.01 -0.3]); ns=length(Y); nm=floor(ns*0.9); figure(1) plot(Y(nm:ns,1),Y(nm:ns,2),'r','linewidth',2) set(gca,'FontSize',15) xlabel('\bfx','Fontsize',15) ylabel('\bf u','Fontsize',15) grid on function dy = ff22(t,y) global mu om1 om2 alpha dy = zeros(2,1); % a column vector dy(1) = y(2); dy(2) =cos(om1)+cos(om2) -y(1)-alpha*y(1)^3-2*mu*y(2); figure(2) subplot(2,1,1) plot(T(nm:ns),Y(nm:ns,1),'linewidth',2) grid on set(gca,'FontSize',15) xlabel('\bf Time','Fontsize',15) ylabel('\bfx','Fontsize',15) subplot(2,1,2) plot(T(nm:ns),Y(nm:ns,2),'linewidth',2) grid on set(gca,'FontSize',15) xlabel('\bf Time','Fontsize',15) ylabel('\bfx','Fontsize',15) end

Star Strider on 7 Mar 2019
Use:
tspan = linspace(0, 100, 50);
to create a ‘tspan’ vector with 50 elements between 0 and 100.
##### 2 CommentsShowHide 1 older comment
Star Strider on 7 Mar 2019
As always, my pleasure!

Daniel Dolan on 7 Mar 2019
Pass an array of tpan points. From the ode45 documentation:
tspan Interval of integration
vector
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]