ODE45: how to print a variable
8 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (3 件)
Jan
2016 年 11 月 11 日
編集済み: Jan
2016 年 11 月 11 日
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 件のコメント
Walter Roberson
2016 年 11 月 11 日
I am surprised this works. My tests with anonymous functions seemed to imply that you cannot have multiple outputs for the objective function.
Jan
2017 年 1 月 17 日
@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
2016 年 11 月 10 日
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 件のコメント
Walter Roberson
2016 年 11 月 11 日
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
2016 年 11 月 11 日
The line
recordings(Iters, :) = [t, v, Delta];
should be
recordings(Iters, :) = [t, v.', Delta];
Marco Sammito
2016 年 11 月 11 日
1 件のコメント
Jan
2016 年 11 月 11 日
Please post comments in the comment section, not as an answer and prefer text copies instead of screenshots. Thanks.
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!