ODE45: how to print a variable

Hi, I have the following code
.m file
function vdot=diffeq(t,v)
a=2;
b=3;
c=4;
d=5;
e=a*b/c;
%
vdot=zeros(2,1);
vdot(1)=v(2);
Delta=e*b*v(2)*sqrt(t);
vdot(2)=0.5*v(1)+3/4*v(2)+sin(t)+Delta;
run.m file
R0=510e-6;
tf=40e-6;
options = odeset('RelTol',1e-8,'AbsTol',[1e-8 1e-8]);
[t,v]=ode45('diffeq',[0,tf],[R0,3],options);
[t,v(:,1)]
How can I print the variable Delta in the command window and afterthat export it to an Excel file? At the moment I cannot simply type in filename='excel_file.xls'; xlswrite(filename,Delta)
Thank you.

Answers (3)

Jan
Jan on 11 Nov 2016
Edited: Jan on 11 Nov 2016
function [vdot, Delta] = diffeq(t,v)
a=2;
b=3;
c=4;
d=5;
e=a*b/c;
%
vdot = zeros(2, numel(t));
vdot(1, :) = v(2, :);
Delta = e * b * v(2, :) .* sqrt(t);
vdot(2, :) = 0.5 * v(1, :) + 3 / 4 * v(2, :) + sin(t) + Delta;
end
Now integrate as before and afterwards request Delta only for the accepted time steps:
[t, v] = ode45(@diffeq, [0,tf], [R0,3], options);
[vt, Delta] = diffeq(t.', v.')

3 Comments

@Jan Could you explain to me what vt means in [vt, Delta] = diffeq(t.', v.')
I am surprised this works. My tests with anonymous functions seemed to imply that you cannot have multiple outputs for the objective function.
@Marco: "vt" means "v transposed".
@Walter: When called from ODE45, the Delta output is ignored. It matters only in the specific call after the intergration. This can be triggered explicitly:
function [vdot, DeltaOut] = diffeq(t, v)
a=2; b=3; c=4; d=5; e=a*b/c;
vdot = zeros(2, numel(t));
vdot(1, :) = v(2, :);
Delta = e * b * v(2, :) .* sqrt(t);
vdot(2, :) = 0.5 * v(1, :) + 3 / 4 * v(2, :) + sin(t) + Delta;
if nargout > 1
DeltaOut = Delta;
end
end
I do not see the relation to anonymous functions.

Sign in to comment.

The ode*() routines, when processing with multiple variables (e.g., your v is length 2), do not proceed in a linear fashion over time: they need to explore different boundaries at the same time point. The ode*() routines also do not simply sample at a bunch of times and v values and report at the locations that they sampled at: they probe at a variety of locations and use that to interpolate at the times that they report about.
It is therefore not beneficial to just output the delta values: you would need to also output the t and v values so you would have the context for the delta values.
The best way to record values like these is to use nested functions with shared variables:
function run
R0=510e-6;
tf=40e-6;
options = odeset('RelTol',1e-8,'AbsTol',[1e-8 1e-8]);
guess_at_iterations = 10000;
Iters = 0;
recordings = nan(guess_at_iterations, 4); %shared variable!
[t,v]=ode45( @diffeq, [0,tf], [R0,3], options);
[t,v(:,1)]
recordings_cell = [{'t', 'v1', 'v2', 'delta'}; num2cell(recordings(1:Iters,:))];
xlswrite('YourFile.xlsx', recordings_cell);
function vdot=diffeq(t,v)
a=2;
b=3;
c=4;
d=5;
e=a*b/c;
%
vdot=zeros(2,1);
vdot(1)=v(2);
Delta=e*b*v(2)*sqrt(t);
vdot(2)=0.5*v(1)+3/4*v(2)+sin(t)+Delta;
Iters = Iters + 1;
recordings(Iters, :) = [t, v, Delta];
end %this must be here to match the function
end %this must be here after everything else

3 Comments

Jan
Jan on 11 Nov 2016
Edited: Jan on 11 Nov 2016
@Walter: "run"? Like the built-in function to start scripts?
Collecting the values inside the function to be integrated includes the values of rejected steps.
Ah, yes, wasn't thinking. I was just grabbing the name the poster had used, to show that it was all one file.
The line
recordings(Iters, :) = [t, v, Delta];
should be
recordings(Iters, :) = [t, v.', Delta];

Sign in to comment.

Marco Sammito
Marco Sammito on 11 Nov 2016
Thank you for answering. I tried your code but MATLAB returns this error:

1 Comment

Please post comments in the comment section, not as an answer and prefer text copies instead of screenshots. Thanks.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 10 Nov 2016

Commented:

Jan
on 17 Jan 2017

Community Treasure Hunt

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

Start Hunting!