Passing extra output arguments from ode

조회 수: 34 (최근 30일)
Deepa Maheshvare
Deepa Maheshvare 2020년 3월 25일
편집: Stephen23 2020년 3월 28일
I've the following code to obtain [t,x] after simulating odes . In addition to the solution, I also want to obtain the functiion values F(x,t) in the below equation.
% obtains the dynamics from a system of odes
x0 = initial values;
tspan = 0:0.0001:0.5;
options = odeset('abstol', 1e-10, 'reltol', 1e-9);
% generate solution
[t, x] = ode15s(@(t,x) fun(t,x), tspan , x0 ,options);
% obtain function values ,f, from ode
[dy f] = fun([ ], [ ])
function [dy f] = fun(t,x)
persistent f
f = []
A = % Matrix
dy = A*x
if nargout >1
f = horzcat(f, A*x)
end
end
In the code, I've defined a persistent variable, f, that stores F for each time step that I solver takes. And I receive f using `[dy f] = fun([ ], [ ])``
But the size of f and x are not equl because from what I understand the output of x is given (after interpolation I guess) only at the time points defined in tspan. Whereas, f is saved for very time instant t that the solver takes internally.
In summary my question is, how do I obtain f for the same time instants at which x is obtained.

채택된 답변

Stephen23
Stephen23 2020년 3월 25일
편집: Stephen23 2020년 3월 25일
"In summary my question is, how do I obtain f for the same time instants at which x is obtained."
Using a persistent variable or OutputFcn is a red-herring. The simple solution is to call the function after calling the ODE solver, using exactly the values that the ODE solver returns. Here is a complete working example simply based on the first example from the ode15s documentation:
tspan = [0,2];
x0 = 1;
fun = @(t,x) -10*t;
[t,x] = ode15s(fun, tspan, x0)
Then you just need this:
dy = arrayfun(fun,t,x)
  댓글 수: 15
Deepa Maheshvare
Deepa Maheshvare 2020년 3월 28일
Thank you. After correcting the stupid mistake from my side,
line 18 is now:
f = cellfun(afun,num2cell(t),num2cell(y,2),num2cell(D),'UniformOutput',false);
Error:
Error using cellfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 51 in dimension 1. Input #4 has size 9
Error in simulate (line 18)
f = cellfun(afun,num2cell(t),num2cell(y,2),num2cell(D),'UniformOutput',false);
Stephen23
Stephen23 2020년 3월 28일
편집: Stephen23 2020년 3월 28일
D is not an input to cellfun or ode15s.
The array D is passed to fun solely via the anonymous function definition, which access D directly from the workspace where the anonymous function is defined. You do NOT need pass D as an input to cellfun.
What you did:
f = cellfun(afun,num2cell(t),num2cell(y,2),num2cell(D),'UniformOutput',false);
% ^^^^^^^^^^^^ does not belong here
What you need to do:
dydt = cellfun(afun,num2cell(t),num2cell(y,2),'UniformOutput',false);
% ^^^^ D is already in this function definition!!!!!!!
I suggest that you read the link I gave you earlier on how to parameterize functions. Then you would get it working in 20 seconds. Making code changes randomly, without understanding how the anonymous function is used, is not an efficient use of your time:

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 3월 25일
You need to use OutputFcn to get the value of a variable at the same time instant as the value in the output vector: https://www.mathworks.com/help/matlab/ref/odeset.html#d120e857290. Following code shows an example
clear global
global x_vector;
opts = odeset('OutputFcn', @outfcn);
[t,x] = ode45(@(t,x) [x(2); -x(1)], [0, 2*pi], [0; 1], opts);
plot(t, x_vector)
function s = outfcn(t,x,f)
if strcmp(f, 'done')
return
end
global x_vector;
x_vector = [x_vector x(1, :).^2];
s = 0;
end
Note that in this code, I used the global variable to get value out of outfcn, to show a general idea. In practice, global variables are not recommended. I suggest you look at John D'Errico's answer here: https://www.mathworks.com/matlabcentral/answers/510713-is-it-possible-to-store-the-intermediate-values-of-fmincon to see a more efficient way to get value out of the outfcn.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by