MATLAB: ODE45, use ode45 iteratively

iterationode45

Hi, I have the equation m.(xdbldot)+c.(xdot)+k.x = A.w^2; for which obtained the solution through ODE45 using state space form; function ydot=diffeqn(t,y) w=50;m=2;k=500;d=16; ydot(1)=y(2); ydot(2)=-d/m*y(2)-k/m*y(1)+A*w^2; ydot=ydot(:);
followed by
tspan=0:0.001:5;
[t,y]=ode45(@diffeqn,tspan,[0 0]);
yy1=y(:,1);
yy2=y(:,2);
acc=diff(yy2/(t(2)-t(1)));
Now i try to solve the ode for different values of the right side variable 'w'. I tried to use functions,but as ODE45 is inbuilt source code, i could not edit it. Is there any other means of doing it. note: I have also tried Rungekutta 4th order code without using ode45, yet ode45 seems to give finer results.
Thanks, Shravan.

Best Answer

Hi,
try using a different function handle
[t,y]=ode45(@(x,t) diffeqn(x,t,w),tspan,[0 0]);
where w is defined before the ODE45 call. In addition change the diffeqn function signature to
function [...] = diffeqn(x,t,w)
So an easy overall example would be (we are looking for the function f = w*x) (so we dont need t and x in our function, since f' = w)
function out = diffeqn( x,t,w )
out = w;
end
We call it with different w and plot it:
w = 2
[t,y]=ode45(@(x,t) diffeqn(x,t,w),[0,5],0);
plot(t,y)
hold on
w = 4
[t,y]=ode45(@(x,t) diffeqn(x,t,w),[0,5],0);
plot(t,y,'r')
legend({'2x','4x'})
hold off
Related Question