How do I extract extra parameters from the ode45 ordinary differential equation solver function?
Show older comments
I have the following function which represents an ever increasing mass on a spring according to Hooke's Law:
function dydt = HookeWithRisingMass(t, y, m0, k, Cload)
%HookeWithRisingMass defines Hooke's Law but with a mass increasing at a constant rate over time.
%INPUTS:
%t = [start, finish], time interval
%y = [disp; velocity], initial conditions of the system; displacement from equilibrium and starting velocity
%k = the spring constant
%m0 = initial mass
%Cload = constant of mass increase
%OUTPUTS:
%dydt = [time, [diplacement, velocity]]
%See also
mass = m0 + Cload*t; %mass increases linearly with time
disp = y(1); %Initial displacement
dxdt = y(2); % set dx/dt = velocity
dvdt = -(k/mass).*disp; % set dv/dt = acceleration
dydt = [dxdt; dvdt]; % return the derivatives
It works correctly when called by e.g. [timeout,out] = ode45(@HookeWithRisingMass, tin, y, [], m0, k, Cload).
However I really need to get a time series of values of the mass vs. time for use elsewhere. At the moment I can do this by adding the following to the HookeWithRisingMass function definition:
paff = 'C:\matlabtemp\';
fileName = num2str(t);
fileName(regexp(fileName, '\D')) = 'D';
fileName = ['mass' fileName];
save([paff fileName], 'm', 't');
I can then sequentially load the saved files and store the values of m, t in an array. This is a slow method and gets very unwieldy when the maximum value of time, t, is large. Is there a method of extracting t, m, pairs and storing them in an array that is available in the workspace of the script/function that makes the ode45 call?
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!