How can I modify an object within ode45?

2 次查看(过去 30 天)
My code creates and reference an object, then ode45 is run. Within the function that I am integrating, I am trying to save some parameters so that I can plot then later. To do so, I want to modify the class that was referenced before ODE45. the problem is that, while the parameter value exists within the function being run in ode45, it does not recognize my attempt to reference the object. Here's a snippet:
if iter == 1
QP = QuadPlot(1, x0, L, 0.04, quadcolors(1,:), max_iter, h_3d);
current_state = stateToQd(x);
desired_state = trajhandle(time, current_state);
QP.UpdateQuadPlot(x, [desired_state.pos; desired_state.vel], time, zeros(6,1));
h_title = title(sprintf('iteration: %d, time: %4.2f', iter, time));
end
% Run simulation
[tsave, xsave] = ode45(@(t,s) quadEOM(t, s, controlhandle, trajhandle, params), timeint, x);
x = xsave(end, :)';
load('plotter');
%disp(plotter)
% Save to traj
xtraj((iter-1)*nstep+1:iter*nstep,:) = xsave(1:end-1,:);
ttraj((iter-1)*nstep+1:iter*nstep) = tsave(1:end-1);
% Update quad plot
current_state = stateToQd(x);
desired_state = trajhandle(time + cstep, current_state);
%des_xtraj = desired_state;
QP.UpdateQuadPlot(x, [desired_state.pos; desired_state.vel], time + cstep, plotter);
set(h_title, 'String', sprintf('iteration: %d, time: %4.2f', iter, timque + cstep))
I can't reference QP within quadEOM :-(

回答(1 个)

Jan
Jan 2017-4-7
编辑:Jan 2017-4-7
How did you try to reference it? You have to provide QP as input argument to have it available inside quadEOM. I guess:
[tsave, xsave] = ode45(@(t,s) quadEOM(t, s, controlhandle, trajhandle, params, QP), timeint, x);
But then notice, that the function to be integrated will be called even for rejected steps. It would not be useful to store the trajectories using theses points. So better use the OutputFcn, which considers accepted steps only.
I would not solve the problem in this indirect nested way. Better let ODE45 calculate the trajectory and call quadEOM afterwards with tsave, xsave as inputs. Now let quadEOM reply the needed parameters if nargout > 1:
function [ds, extraOutput] = quadEOM(t, s, controlhandle, trajhandle, ...
params)
% Consider that [t] is a vector and [s] a matrix.
ds = ...
if nargout > 1
extraOutput = ...
end

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by