Substitutions with ODE23/45 in MATLAB

  • MATLAB
  • Thread starter bflbfl
  • Start date
  • Tags
    Matlab
In summary, the author is trying to solve a laser rate equation using MATLAB, but is having difficulty because of the redundant variables that are created. He suggests using substitutions to get rid of the derivatives on the right hand side, or just writing the equations in the standard way.
  • #1
bflbfl
10
0
Hi everybody,

I am trying to solve laser rate equations with MATLAB using ODE23/45. For that I have to write the derivative terms always on the left hand side. This creates a lot of redundant variables in the function. Here is a part of the code:

dy(1) = I/(q*V) - y(1)/Tau_n - y(4)*y(2);
% derivative of the no. of carriers, N(t)

dy(2) = (y(4)-1/Tau_p+log(100)/Tau_D) * y(2) + R_sp;
% derivative of the no. of photons, S(t)

dy(3) = (alpha/2)*3.2e3*(y(1)-N_th) + 20/Tau_D;
% derivative of the optical phase, phy(t)

dy(4) = y(5)*(-Kappa*y(6))+(1-Kappa*y(2))*y(7);
% derivative of g(N,S)

dy(5) = 3.2e3 * y(7);
% derivative of g(N)

dy(2) = y(6);
dy(1) = y(7);

Because of y(6) and y(7), I have to define initial values for them as well, for instance y(6) = y(7) = 1. This is now making problems for y(1) and y(2) since they are changing in each iteration, but because of the constant values of y(6) = y(7) = 1, MATLAB considers y(1) = y(2) = 1. Obviously, I do not want this to happen. I cannot write the dy(2) and dy(1) terms on the right hand side, since it is not allowed by MATLAB. Any suggestions to overcome this problem would be welcome. Thanks :)
 
Physics news on Phys.org
  • #2
Just to mention that Tau, Kapa, Tau_n, Tau_p and alpha are constants.
 
  • #3
Can you post the mathematical equations you are trying to solve, not just your failed attempt at writing Matlab code?

I am guessing you have some basic misunderstanding about what you need to do here.
 
  • #4
Thank you for your reply. So here are the equations:

dN/dt = I/q - N/Tau_n - g(N,S)*S
% derivative of the no. of carriers, N(t)

dS/dt = [g(N,S) - 1/Tau_p + log(100)/Tau_D] * S + R_sp;
% derivative of the no. of photons, S(t)

dphy/dt = (alpha/2) * 3.2e3 * (N-N_th) + 20/Tau_D;
% derivative of the optical phase, phy(t)

dg(N,S)/dt = dg*[-Kappa*g(N)]+[1-Kappa*S]*dS/dt;
% derivative of g(N,S)

dg(N)/dt = 3.2e3 * dN/dt;
% derivative of g(N)

There are more equations in fact but I have written the basic simplified ones, for the sake of understanding.

According to MATLAB rules, one cannot write derivative terms on the right hand side. So I am using substitutions, as per the information available on mathworks website. This is what I did with y(6) and y(7) in the code for dS/dt and dN/dt in the code respectively. I look forward to your suggestions in this connection !
 
  • #5
I don't see why you need to introduce y(6) and y(7) at all.

If you wanted, you could substutute for dN/dt in
dg(N)/dt = 3.2e3 * dN/dt
to get
dg(N)/dt = 3.2e3 * [ I/q - N/Tau_n - g(N,S)*S ]
and similarly for dg(N,S)/dt.

That "gets rid of the derivatives on the right hand side" without introducing any more variables.

That's the way I would code it in matlab. Just write

dy(1) = I/(q*V) - y(1)/Tau_n - y(4)*y(2);
dy(2) = (y(4)-1/Tau_p+log(100)/Tau_D) * y(2) + R_sp;
dy(3) = (alpha/2)*3.2e3*(y(1)-N_th) + 20/Tau_D;
dy(4) = y(5)*(-Kappa*y(6))+(1-Kappa*y(2))*dy(1);
dy(5) = 3.2e3 * dy(2);

There is no problem using the value of dy(1) and dy(2) again after you have calculated them.

Or if you feel worried about doing that, you could write

temp1 = I/(q*V) - y(1)/Tau_n - y(4)*y(2);
temp2 = (y(4)-1/Tau_p+log(100)/Tau_D) * y(2) + R_sp;
dy(1) = temp1;
dy(2) = temp2;
dy(3) = (alpha/2)*3.2e3*(y(1)-N_th) + 20/Tau_D;
dy(4) = y(5)*(-Kappa*y(6))+(1-Kappa*y(2))*temp1;
dy(5) = 3.2e3 * temp2;
 

Related to Substitutions with ODE23/45 in MATLAB

What is ODE23/45 in MATLAB?

ODE23/45 is a numerical method used in MATLAB for solving ordinary differential equations (ODEs). It is based on the Runge-Kutta method and is designed to efficiently handle stiff ODEs.

What is the difference between ODE23 and ODE45?

The main difference between ODE23 and ODE45 is the order of accuracy. ODE45 is a 4th order method, meaning it is more accurate but also more computationally expensive. ODE23 is a 2nd order method and is less accurate but faster.

How do I use ODE23/45 in MATLAB?

To use ODE23/45 in MATLAB, you must first define the ODE you want to solve as a function. Then, you can use the ode23 or ode45 function, passing in the ODE function, initial conditions, and other parameters as inputs. The output will be a vector containing the solution at specified time points.

What is a stiff ODE?

A stiff ODE is an ODE with solutions that exhibit behavior that changes rapidly over a small range of the independent variable. These types of ODEs can be challenging to solve numerically, and ODE23/45 is specifically designed to handle them efficiently.

When should I use ODE23/45 instead of other numerical methods?

ODE23/45 is most useful for solving stiff ODEs. If your ODE is not stiff, other methods such as ode113 or ode15s may be more efficient. It is also important to consider the accuracy requirements and computational costs when choosing a numerical method.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
375
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
904
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
969
Back
Top