How can I extract some variables inside ode45 solver?

Hello,
I need to extract a matrix inside my ode45 solver, to use later. I couldn't find any method to do so. is there any method I can extract and save this matrix?
Thanks,
Sali

1 Comment

It depends on what "extract a matrix inside my ode45 solver" means. Please explain this with any details. ode45 a tool to approximate the integral. It is not clear where you want to extract what.

Sign in to comment.

 Accepted Answer

Jan
Jan on 19 Jun 2018
Edited: Jan on 19 Jun 2018
It would not be useful to extract the values during the integration, because the function is evaluated several time for each step and a step can be rejected by the step size controller. So better run the integration ar first:
[z, A] = ode45('Coupled',z_values,initial_cond,options);
and call the function for these z, A afterwards and return Temp also as output:
function [dAdz, Temp] = Coupled(z, A)
...
And a loop:
TempC = cell(1, numel(z));
for iz = 1:numel(z)
[dAdz, Temp] = Coupled(z(iz), A(iz, :)); % [EDITED]
TempC{iz} = Temp;
end
By the way: What is N01, N11, rcore? If they are constants, you could accelerate the computation by using a persistent variable:
function dAdz = Coupled(z, A)
persistent Is
if isempty(Is)
N=63;
L=500e-6;
dx=L/(N);
x=(-L/2:dx:L/2);
y=(-L/2:dx:L/2);
[X,Y]=meshgrid(x,y);
nx=N+1;
r=sqrt((X).^2+Y.^2);
E1=(N01*besselj(0,sqrt((X).^2+Y.^2)*U01/rcore));
E2=(N11*besselj(1,sqrt((X).^2+Y.^2).*U11/rcore).*cos(atan(Y./(X))));
Is=((abs(A1).^2.*abs(E1).^2+abs(A2).^2.*abs(E2).^2+...
A1.*conj(A2).*E1.*conj(E2).*exp(1i*(beta01-beta11).*z)+...
A2.*conj(A1).*E2.*conj(E1).*exp(1i*(beta11-beta01).*z)));
Is=abs(Is);
end
...
Now this part is computed once only.

10 Comments

Thank you very much Jan for your answer.
Yes, N01, N11, rcore are constants.
I have 2 questions here:
1-What should be the content of this function: function [dAdz, Temp] = Coupled(z, A)? I mean, it is not going to be a command line and the loop you mentioned. Do I need to use the main code for this function as well?
2-Temp matrix at each z step gives us a variable which I insert it into my coupled equations. it is like this:
dAdz=[K1n.*A1+K3n.*A2.*exp(1i*(beta11-beta01).*z)
;K2n.*A2+K3n.*A1.*exp(1i*(beta01-beta11).*z);
Gain*A3/Aclad ];
in this equations, K1n,k2n k3n are calculated from Temp, something like this:
K1n= E1*Temp*E1
k2n=E1*Temp*E2
k3n=E2*Temp*E1
would your final call for Temp through function [dAdz,Temp]=Coupled(z,A) still valid?
Thank you very much.
Sali
The content of
function [dAdz, Temp] = Coupled(z, A)
is exactly the same content as of your
function dAdz = Coupled(z, A)
All you have to change is to return the wanted variable as 2nd output. This is ignored, when Coupled() is called from ODE45, but you catch it afterwards. See [EDITED] in my code: It was a typo to use the first output, but Temp is the 2nd output.
Thank you very much Jan. it worked!
Now, I have another step in my code. I need to use a time loop for all the z steps. That means I need to put all these functions and ode45 solutions into a for loop.
In this for loop, all initial parameters are the same as the first time=0, but I need to insert the TempC(z) as initial temp for the next time loop. It is going to be something like this:
for time=0:dt:T
[z,A]=ode45('Coupled',z_values,initial_cond,options);
TempC = cell(1, numel(z));
for iz = 1:numel(z)
[dAdz, Temp] = Coupled(z(iz), A(iz, :)); % [EDITED]
TempC{iz} = Temp;
end
end
but the Coupled function should read the TempC from previous time step for each z value and set it as Temp0 to find Temp for new time step:
Temp0=TempC{zi};
for i=2:nx-1
for j=2:ny-1
Temp(i,j)=rr*(Temp0(i-1,j)+Temp0(i+1,j)+Temp0(i,j-1)+Temp0(i,j+1))+(1-4*rr)*Temp0(i,j)+rr*dx^2/K*Q(i,j);
end
end
How can I do this?
The body of the loop does not depend on the loop counter time, so its purpose is not clear.
Can you be sure that the 2nd integration uses the same time steps? If not, recording the former Temp values is not useful, is it?
But you can provide a parameter in general:
fcn = @(t, x) Coupled(t, x, Temp);
[z,A] = ode45(fcn, z_values,initial_cond,options);
...
And the function to be integrated:
function [dAdz, Temp] = Coupled(z, A, Temp)
... and as the original except for using Temp if it is provided
Thank you so much Jan for your kind reply. Actually I was working on this and I implemented almost the same thing, and it is working. I used this (yours give me some error though, but it is almost the same):
function [dAdz, Temp] = Coupled(z, A, flag,TempC)
...my code
end
and then I called it like this, in such a way that TempC can be defined here::
initial_cond=[sqrt(power01) ; sqrt(power11); pp0];
z_values=0:dz:Z;
options = odeset('RelTol',1e-4,'AbsTol',1e-4);
for ij=1:round(Z/dz+1)
TempC{ij}=zeros(64,64);
end
[z,A]=ode45('Coupled',z_values,initial_cond,options,TempC);
% TempB is updated TempC and is extracted like this:
TempB = cell(1, numel(z));
for iz = 1:numel(z)
[dAdz, Temp] = StaticNaderiCoupledNumeric6(z(iz), A(iz, :),flag,TempC); % [EDITED]
TempB{iz} = Temp;
end
Now, for the time loop, I just need to replace new TempC to the function Coupled as Temp0. That means I should set the for loop so that the ode45 reads this new TempC as input to function [dAdz, Temp] = Coupled(z, A, flag,TempC) .
Do you have any suggestion for this?
To answer to your question on time loop, time just changes the initial value of the Temp, everything else is the same. I just need to solve the function for the each time step, extract the Temp and pass it to the function again as initial Temp for the next time step. Hope it is clear now.
I really appreciate your help.
Sali
I'm not sure if I understand your approach correctly. But one hint:
See Use anonymous functions to provide parameters (link): Do not define the function to be integrated as string and do not append the parameters as 6th input - both methods are kept only for backward compatibility with Matlab 5.3 and are outdated for 19 years now.
% [z,A] = ode45('Coupled',z_values,initial_cond,options,TempC);
fcn = @(z, A) Coupled(z, A, TempC);
[z,A] = ode45(fcn, z_values, initial_cond, options);
Safer and smarter.
Thank you very much. I used your method and it also works properly. I appreciate it.
I am still working on time loop. Since the ODE equations Iam working on, depend on z and time. so, I try to solve these ODEs first in z, by using ode45 and then see the evolution by time as well. that is why I need to use a for loop for time to see what happens after a while to the outputs of z. It is going to be like this:
power01=28.5;
power11=1.5;
dz=1e-3 ;
Z=.1;
pp0=850;
initial_cond=[sqrt(power01) ; sqrt(power11); pp0];
z_values=0:dz:Z;
options = odeset('RelTol',1e-4,'AbsTol',1e-4);
for ij=1:round(Z/dz+1)
TempC{ij}=zeros(64,64);
end
for t=0:dt:T
[z,A] = ode45(@(z,A) Coupled(z, A, flag,TempC), z_values,initial_cond,options);
% Extracting Temp
TempB = cell(1, numel(z));
for iz = 1:numel(z)
[dAdz, Temp] = Coupled(z(iz), A(iz, :),flag,TempC);
TempB{iz} = Temp;
end
% Replace TempC by TemB and send it to the solver as TempC for the next time step
for ij=1:round(Z/dz+1)
TempC{ij}=TempB{ij};
end
end % time steps
So, the solver should solve the PDE using TempC as set for the first time step. then, we extract the new TempC, set it as TempC to the solver for the next time step and extract a new Temp and so on.
The code with for loop is not correct right now. But this is what I am looking for.
Please let me know which part you don't understand correctly.and I appreciate any suggestion on this.
Thanks,
Sali
May I ask another question please?
May you please explain the point you mentioned to accelerate the simulation by:
persistent Is
in my code, since all of my parameters are z and A dependent, I have defined them in separate functions inside the main "Coupled" function. Do I really need to do this? I mean how should I define the z and A dependent variables in my function?
here is my code:
function [dAdz, Temp] = Coupled(z, A, flag,TempC)
A1=A(1);
A2=A(2);
A3=A(3);
N=63;
L=500e-6;
dx=L/(N);
x=(-L/2:dx:L/2);
y=(-L/2:dx:L/2);
[X,Y]=meshgrid(x,y);
nx=N+1;
ny=nx;
r=sqrt((X).^2+Y.^2);
% the rest of parameters are just constant.
E1=(N01*besselj(0,sqrt((X).^2+Y.^2)*U01/rcore));
E2=(N11*besselj(1,sqrt((X).^2+Y.^2).*U11/rcore).*cos(atan(Y./(X))));
K01n=(conj(E_ini01Normal).*dn(A,N2(A)).*E_ini01Normal);
K1n=sum(sum(K01n)).*dx^2;
K02n=(conj(E_ini11Normal).*dn(A,N2(A)).*E_ini11Normal);
K2n=sum(sum(K02n)).*dx^2;
K03n=(conj(E_ini01Normal).*dn(A,N2(A)).*E_ini11Normal);
K3n=sum(sum(K03n)).*dx^2;
Gp1=(N2(A).*sigma_ep-(Nyb-N2(A)).*sigma_ap);
Gp2=sum(sum(Gp1))*dx^2;
dAdz=[(K1n.*A1+K3n.*A2.*exp(1i*(beta11-beta01).*z));
(K2n.*A2+K3n.*A1.*exp(1i*(beta01-beta11).*z));
Gp2*A3/Aclad ];
%*******************************
% Defining N2(z) as a function here:
function FN2=N2(A)
Is=((abs(A1).^2.*abs(E1).^2+abs(A2).^2.*abs(E2).^2+...
A1.*conj(A2).*E1.*conj(E2).*exp(1i*(beta01-beta11).*z)+...
A2.*conj(A1).*E2.*conj(E1).*exp(1i*(beta11-beta01).*z)));
Is=abs(Is);
FN2=((A3+Is./(A3+ Is.*(sigma_as+sigma_es)).*Nyb;
end
%*******************************
% Defining dn(A,N2) as a function here:
function Fdn=dn(A,N2)
Gain=(sigma_ap.*Nyb-(sigma_ap+sigma_ep).*N2);
Q=((1-lambda_p/lambda_s)/Aclad.*Gain.*A3);
Temp0=TempC{round(z/dz+1)};
for i=2:nx-1
for j=2:ny-1
Temp(i,j)=rr*(Temp0(i-1,j)+Temp0(i+1,j)+Temp0(i,j-1)+Temp0(i,j+1))+(1-4*rr)*Temp0(i,j)+rr*dx^2/K*Q(i,j);
end
end
Fdn=dndT.*Temp;
end
end
I really need to figure out this.
Thank you very much in advance.
Sali
Do I really need to do this?
No. Using the persistent variable was just an idea to save processing time. Unfortunately I do not have further for your problem currently.
Thank you so much Jan for your reply. But I guess I didn't explain it very well. The question of" do I really need this?" Was for defining function s for A,z dependent variables and not for persistent.
May you please explain how I can use persistent in my code. Because it is slow do to z and then time loop.
Thank you very much.

Sign in to comment.

More Answers (2)

Here is my code:
function dAdz=Coupled(z,A)
dAdz = zeros(size(A));
A1=A(1); % lp01 Amplitude
A2=A(2); % lp11 Amplitude
A3=A(3); % pump power
N=63;
L=500e-6;
dx=L/(N);
x=(-L/2:dx:L/2);
y=(-L/2:dx:L/2);
[X,Y]=meshgrid(x,y);
nx=N+1;
r=sqrt((X).^2+Y.^2);
E1=(N01*besselj(0,sqrt((X).^2+Y.^2)*U01/rcore));
E2=(N11*besselj(1,sqrt((X).^2+Y.^2).*U11/rcore).*cos(atan(Y./(X))));
Is=((abs(A1).^2.*abs(E1).^2+abs(A2).^2.*abs(E2).^2+...
A1.*conj(A2).*E1.*conj(E2).*exp(1i*(beta01-beta11).*z)+...
A2.*conj(A1).*E2.*conj(E1).*exp(1i*(beta11-beta01).*z)));
Is=abs(Is);
N2=((A3.*(sigma_ap/(h*vp*Aclad))+Is.*sigma_as/(h*vs))./(A3.*((sigma_ap+sigma_ep)./(h*vp*Aclad))+...
Is.*(sigma_as+sigma_es)./(h*vs)+1/to))*Nyb;
Gain=(sigma_ap.*Nyb-(sigma_ap+sigma_ep).*N2);
Q=((1-lambda_p/lambda_s)/Aclad.*Gain.*A3);
for i=2:nx-1
for j=2:ny-1
Temp(i,j)=rr*(Temp0(i-1,j)+Temp0(i+1,j)+Temp0(i,j-1)+Temp0(i,j+1))+(1-4*rr)*Temp0(i,j)+rr*dx^2/K*Q(i,j);
end
end
%***************************************
In the above code other parameters are just some constant values. However, Q depends on A3 and Gain(N2) and N2(A3,Is), and Is(A1,A2). therefore, Q at each z step depends on A1,A2,A3. and finally Temp(i,j) depend on Q. I need to extract Temp(x,y) for each z step. it is a matrix which I solved numerically.
%****************************
and here is the code to call ode45:
clc
clear all
close all
power01=28.5;
power11=1.5;
dz=1e-3 ;
Z=.2;
pp0=850;
initial_cond=[sqrt(power01) ; sqrt(power11); pp0];
z_values=0:dz:Z;
options = odeset('RelTol',1e-4,'AbsTol',1e-4);
[z,A]=ode45('Coupled',z_values,initial_cond,options);
How can I see Temp Matrix at each z step???

Asked:

on 18 Jun 2018

Community Treasure Hunt

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

Start Hunting!