ODE45: how to print a variable

조회 수: 8 (최근 30일)
Marco Sammito
Marco Sammito 2016년 11월 10일
댓글: Jan 2017년 1월 17일
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.

답변 (3개)

Jan
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
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
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
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
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
Walter Roberson 2016년 11월 11일
The line
recordings(Iters, :) = [t, v, Delta];
should be
recordings(Iters, :) = [t, v.', Delta];

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


Marco Sammito
Marco Sammito 2016년 11월 11일
Thank you for answering. I tried your code but MATLAB returns this error:
  댓글 수: 1
Jan
Jan 2016년 11월 11일
Please post comments in the comment section, not as an answer and prefer text copies instead of screenshots. Thanks.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by