MATLAB: How to use ode45 for an equation with space dependent coefficients

mathieuode45

Consider an equation of form: where so that after each time iteration I have to update to be used in the next time iteration. I have hard coded Runge-Kutta scheme but I believe that there are some tolerance problems. Therefore, I want to try ODE45 and check the difference.

Best Answer

p = @(x) sin(x).*sqrt(tan(x));
% x(1) == x
% x(2) == x'
f = @(t,x) [x(2); -p(x(1)).*x(1)];
tspan = [0 2];
x0 = [0.5 0.1];
[t, x] = ode45(f,tspan,x0);
plot(t,x(:,1),'r',t,x(:,2),'b')