ODE45, use ode45 iteratively

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.

 Akzeptierte Antwort

Friedrich
Friedrich am 6 Feb. 2012

0 Stimmen

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

2 Kommentare

shravan
shravan am 7 Feb. 2012
Thank you Frieidrich.it worked. Not clear with function handles.. any reference with more examples ?
Friedrich
Friedrich am 7 Feb. 2012
Mhh, maybe have a look at the documentation:
http://www.mathworks.com/help/techdoc/matlab_prog/f2-38133.html
Or
http://www.mathworks.com/help/techdoc/ref/function_handle.html

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by