ODE45: how to print a variable
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (3)
Jan
am 11 Nov. 2016
Bearbeitet: Jan
am 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 Kommentare
Walter Roberson
am 11 Nov. 2016
I am surprised this works. My tests with anonymous functions seemed to imply that you cannot have multiple outputs for the objective function.
Jan
am 17 Jan. 2017
@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.
Walter Roberson
am 10 Nov. 2016
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 Kommentare
Walter Roberson
am 11 Nov. 2016
Ah, yes, wasn't thinking. I was just grabbing the name the poster had used, to show that it was all one file.
Walter Roberson
am 11 Nov. 2016
The line
recordings(Iters, :) = [t, v, Delta];
should be
recordings(Iters, :) = [t, v.', Delta];
Marco Sammito
am 11 Nov. 2016
1 Kommentar
Jan
am 11 Nov. 2016
Please post comments in the comment section, not as an answer and prefer text copies instead of screenshots. Thanks.
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!