Unable to perform assignment because the left and right sides have a different number of elements in the ode45
Show older comments
Hi.I have a code error.
error:Unable to perform assignment because the left and right sides have a different number of elements.
Error in rate_eq_program_1 (line 26)
ys(1) = I./(E.*Vp)-(z(1)./tc)-GN.*(z(1)-Ntr).*sigmas.*((abs(z(3)).^2)./Vc);
%%%%%%%%%%%%%%%%%%%%%%%% constants %%%%%%%%%%%%%%%%%%%%%%%%%
q = 1.6021766208e-19;
c = 2.99792458e8;
kk = 1.38e-23;
kk_eV = kk/q;
T = 300;
h = 6.626068e-34;
h_eV = h/q;
hbar = h/(2*pi);
hbar_eV = hbar/q;
epsilon0 = 8.854187817e-12;
P = -1;
rL = 1;
rB = 0.2 ;
tB = sqrt(1-rB^2);
Qt =500;
% Qc =180;
Qi = 14300;
Qp = 10000;
%Qc = 1800;
L = 5e-6;
ng = 3.5;
sigma = 3;
m = 25;
A = 0.105e-12;
confine = 0.5;
Vp = 0.576e-18;
Vc = confine*A*L;
gN =5e-20;
Ntr = 1e24;
tc = 0.5e-9;
%tc = 0.7e-9;
alphai = 1000;
%alpha = 1.5;
alpha =1;
confinec = 0.3;
vg = c/ng;
vgp = c/6/ng;
Vpc = 0.22e-18;
KD = confinec*vgp*alpha*gN/2;
KA = -confinec*vgp*gN/2;
lambda0 = 1554e-9;
deltalambda = 0.01e-9;
lambda_start = 1540e-9;
lambda_end = 1560e-9;
lambda = lambda_start:deltalambda:lambda_end;
omega = 2*pi*c./lambda;
omega0 = omega(find(lambda==lambda0));
E = hbar_eV.*omega;
gamai = omega./2/Qi;
gamap = omega./2/Qp;
gamat = omega./2/Qt;
%gamac = omega./2/Qc;
%gamac = omega./2/Qc;
gamac = gamat-gamai-gamap;
gama1 = gamac./2;
gama2 = gama1;
COS2 = 0.5*gama1./gama1.*tB^2/rB-0.5*tB^2/rB-rB;
SIN2 = P*tB./(2.*gama1*rB).*sqrt(4.*gama1.*gama1 - tB^2.*(gamac.^2));
% CS12 = 1/(1i*tB).*(1i*SIN2 + rB);
GLA = gN*vgp./(E.*Vpc*q);
GN = confine*vg*gN;
tin = 2*L/vg;
M = 0.5*(length(E)-1);
Tmin = 0;
Tmax = 5e-9;
dt = 0.01e-12;
Time = Tmin:dt:Tmax;
N0 = zeros(1,4);
N0(3) = 0.2;
%I =2e-3;TA I =12e-3;
I =0.5e-3;
Vnc= 0.24e-18;
%%%%%%%%%%%%%%%%%%%%%%% main code %%%%%%%%%%%%%%%%%%%%%%%%%%%
Time = Tmin:dt:Tmax;
z0 = [0.2,0.2,0.2,0.2];
[t,z] = ode45(@rate_eq_program_1,Time,z0);
param_rate_eq
figure(1)
plot(t,z(:,1)); % divided to normalize
xlabel('time [ns]','FontSize',14); % size of x label
ylabel('Arbitrary units','FontSize',14); % size of y label
set(gca,'FontSize',14); % size of tick marks on both axis
legend('N', 'Location','SE') % legend inside the plot
figure(2)
plot(t,z(:,2)); % divided to normalize
xlabel('time [ns]','FontSize',14); % size of x label
ylabel('Arbitrary units','FontSize',14); % size of y label
set(gca,'FontSize',14); % size of tick marks on both axis
legend('NC', 'Location','SE') % legend inside the plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function %%%%%%%%%%%%%%%%%%%%%%%%
function ys = rate_eq_program_1(t,z)
param_rate_eq_fano
ys=zeros(4,1);
%rR=(-p*gamma_c)/(1i*(delta_c)+gamma_T-1/2*(1-1i*henry)*conf_NC*V_g*g_n*(z(2)-N_0));
deltawc=KD.*(z(2))-1i*KA.*(z(2));
deltac=omega0+deltawc-omega;
rR=(-P.*gamac)./(1i*(deltac)+gamat-(1/2*(1-1i)*confinec*vg*gN).*(z(2)-Ntr));
rRS=-P.*gamac./(1i.*(deltac)+gamat);
%rRS=rB+(-1i*tB-rB).*gamac./(1i.*(deltac)+gamat);
g=alphai+(1/(2*L)).*log(1./(abs(rRS)).^2);
A=(2*epsilon0*n*ng)./(hbar.*omega0);
B=(1+(abs(rRS))).*(1-(abs(rRS)));
D=((g)-alphai);
H=c./(omega0.*n);
F=imag(rRS)./abs(rRS);
sigmas=A.*((B./D)+(H.*F));
ys(1) = I./(E.*Vp)-(z(1)./tc)-GN.*(z(1)-Ntr).*sigmas.*((abs(z(3)).^2)./Vc);
ys(2) = (-z(2)./tc)-GLA.*(z(2)-Ntr).*(abs(z(4)).^2)./Vnc;
ys(3) = 1/2*(1-1i)*GN.*(z(1)-Nss).*z(3)+(1/tin.*((z(4)./rR)-z(3)));
ys(4) =((-1i.*deltawc-gamat).*z(4))-(P.*gamac.*z(3))+(1/2*(1-1i).*confinec*vg*gN.*(z(2)-Ntr).*z(4));
ys = ys';
end
i tried ys=zeros(4,M) But it's also a mistake.
i tried ys=zeros(4,length(E)) But it's also a mistake.
....
Thanks for helping someone.
6 Comments
Walter Roberson
on 1 Feb 2020
omega0 = omega(find(lambda==lambda0));
You used a floating point equality test on values that are calculated. The == operator does bit-for-bit exact comparison without accepting even a little rounding. Therefore the test will usually come out with no matches.
You should only ever use == with floating point numbers to compare values that have been extracted from the same calculation, such as x==min(x) where you know that a value is bit-for-bit identical. (There are also some good reasons to check for exact 0, and sometimes to compare to integers but less often than you would expect).
Otherwise you should only compare within a tolerance, perhaps using ismembertol()
mohammad heydari
on 1 Feb 2020
Ioannis Andreou
on 1 Feb 2020
Instead of
lambda=lambda0
use
abs(lambda-lambda0) < 1e-15
mohammad heydari
on 1 Feb 2020
Walter Roberson
on 1 Feb 2020
You should format your code as a block to make it easier for people to copy it.
Walter Roberson
on 1 Feb 2020
What is param_rate_eq_fano ? Is it a script that defines the n used in
A=(2*epsilon0*n*ng)./(hbar.*omega0);
Accepted Answer
More Answers (1)
Walter Roberson
on 2 Feb 2020
The plotting needs to be fixed on this. And it takes a long time!!
function rate_eq_program
%%%%%%%%%%%%%%%%%%%%%%%% constants %%%%%%%%%%%%%%%%%%%%%%%%%
q = 1.6021766208e-19;
c = 2.99792458e8;
kk = 1.38e-23;
kk_eV = kk/q;
T = 300;
h = 6.626068e-34;
h_eV = h/q;
hbar = h/(2*pi);
hbar_eV = hbar/q;
epsilon0 = 8.854187817e-12;
P = -1;
rL = 1;
rB = 0.2 ;
tB = sqrt(1-rB^2);
Qt =500;
% Qc =180;
Qi = 14300;
Qp = 10000;
%Qc = 1800;
L = 5e-6;
ng = 3.5;
sigma = 3;
m = 25;
A = 0.105e-12;
confine = 0.5;
Vp = 0.576e-18;
Vc = confine*A*L;
gN =5e-20;
Ntr = 1e24;
tc = 0.5e-9;
%tc = 0.7e-9;
alphai = 1000;
%alpha = 1.5;
alpha =1;
confinec = 0.3;
vg = c/ng;
vgp = c/6/ng;
Vpc = 0.22e-18;
KD = confinec*vgp*alpha*gN/2;
KA = -confinec*vgp*gN/2;
lambda0 = 1554e-9;
deltalambda = 0.01e-9;
lambda_start = 1540e-9;
lambda_end = 1560e-9;
lambda = lambda_start:deltalambda:lambda_end;
omega = 2*pi*c./lambda;
omega0 = omega(lambda==lambda0); %MARK
E = hbar_eV.*omega;
gamai = omega./2/Qi;
gamap = omega./2/Qp;
gamat = omega./2/Qt;
%gamac = omega./2/Qc;
%gamac = omega./2/Qc;
gamac = gamat-gamai-gamap;
gama1 = gamac./2;
gama2 = gama1;
COS2 = 0.5*gama1./gama1.*tB^2/rB-0.5*tB^2/rB-rB;
SIN2 = P*tB./(2.*gama1*rB).*sqrt(4.*gama1.*gama1 - tB^2.*(gamac.^2));
% CS12 = 1/(1i*tB).*(1i*SIN2 + rB);
GLA = gN*vgp./(E.*Vpc*q);
GN = confine*vg*gN;
tin = 2*L/vg;
M = 0.5*(length(E)-1);
Tmin = 0;
Tmax = 5e-9;
dt = 0.01e-12;
Time = Tmin:dt:Tmax;
N0 = zeros(1,4);
N0(3) = 0.2;
%I =2e-3;TA I =12e-3;
I =0.5e-3;
Vnc= 0.24e-18;
n=3.5;
Nss=(I./(E.*Vp))*tc;
NO = length(omega);
%% main code
%%%%%%%%%%%%%%%%%%%%%%% main code %%%%%%%%%%%%%%%%%%%%%%%%%%%
Time = Tmin:dt:Tmax;
z0 = repmat([0.2;0.2;0.2;0.2], 1, NO);
[t,z] = ode45(@rate_eq_program_1,Time,z0);
param_rate_eq
%all the plotting is wrong.
figure(1)
plot(t,z(:,1)); % divided to normalize
xlabel('time [ns]','FontSize',14); % size of x label
ylabel('Arbitrary units','FontSize',14); % size of y label
set(gca,'FontSize',14); % size of tick marks on both axis
legend('N', 'Location','SE') % legend inside the plot
figure(2)
plot(t,z(:,2)); % divided to normalize
xlabel('time [ns]','FontSize',14); % size of x label
ylabel('Arbitrary units','FontSize',14); % size of y label
set(gca,'FontSize',14); % size of tick marks on both axis
legend('NC', 'Location','SE') % legend inside the plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function %%%%%%%%%%%%%%%%%%%%%%%%
function ys = rate_eq_program_1(t,z)
%{
param_rate_eq_fano
%}
z = reshape(z, 4, []);
ys = zeros(size(z));
%rR=(-p*gamma_c)/(1i*(delta_c)+gamma_T-1/2*(1-1i*henry)*conf_NC*V_g*g_n*(z(2)-N_0));
deltawc=KD.*(z(2,:))-1i*KA.*(z(2,:));
deltac=omega0+deltawc-omega;
rR=(-P.*gamac)./(1i*(deltac)+gamat-(1/2*(1-1i)*confinec*vg*gN).*(z(2,:)-Ntr));
rRS=-P.*gamac./(1i.*(deltac)+gamat);
%rRS=rB+(-1i*tB-rB).*gamac./(1i.*(deltac)+gamat);
g=alphai+(1/(2*L)).*log(1./(abs(rRS)).^2);
A=(2*epsilon0*n*ng)./(hbar.*omega0);
B=(1+(abs(rRS))).*(1-(abs(rRS)));
D=((g)-alphai);
H=c./(omega0.*n);
F=imag(rRS)./abs(rRS);
sigmas=A.*((B./D)+(H.*F));
ys(1,:) = I./(E.*Vp)-(z(1,:)./tc)-GN.*(z(1,:)-Ntr).*sigmas.*((abs(z(3,:)).^2)./Vc);
ys(2,:) = (-z(2,:)./tc)-GLA.*(z(2,:)-Ntr).*(abs(z(4,:)).^2)./Vnc;
ys(3,:) = 1/2*(1-1i)*GN.*(z(1,:)-Nss).*z(3,:)+(1/tin.*((z(4,:)./rR)-z(3,:)));
ys(4,:) =((-1i.*deltawc-gamat).*z(4,:))-(P.*gamac.*z(3,:))+(1/2*(1-1i).*confinec*vg*gN.*(z(2,:)-Ntr).*z(4,:));
ys = ys(:);
end
end
11 Comments
Walter Roberson
on 2 Feb 2020
You have very bad scaling problems. You divide by very small quantities a lot, giving large results, and your GLA is on the order of 1e25 so your ys(2,:) values end up in the 1e24 range. Because the ys(2,:) values are so large, ode45 takes very small steps to try to compute it accurately -- after 9 million function evaluations it was barely up to 1.1e-36 .
You have no hope with this system with ode45. Maybe you would get further with a "stiff" solver such as ode15s .
mohammad heydari
on 3 Feb 2020
Edited: mohammad heydari
on 3 Feb 2020
Walter Roberson
on 3 Feb 2020
I guarantee that none of them used the source code that you gave us.
mohammad heydari
on 3 Feb 2020
Edited: mohammad heydari
on 3 Feb 2020
mohammad heydari
on 3 Feb 2020
Edited: mohammad heydari
on 3 Feb 2020
Walter Roberson
on 4 Feb 2020
Your planck constant is not accurate. It is 6.62607015e-34 by definition https://en.wikipedia.org/wiki/Planck_constant but you were using 6.626068e-34 (which I do see in some other places.)
Your Boltzman constant could be more precise. You are using 1.38e-23 when you could be using 1.380649e-23
Walter Roberson
on 4 Feb 2020
I started annotating your constants, but I got bored. You should continue so that people can understand what you are talking about. Make it easy for people to work with your code. Make it easy for you to work with your code, as you will not remember some of these things if you take a long weekend off.
I do not see the Laser cavity cross-section variable ?
%%%%%%%%%%%%%%%%%%%%%%%% constants %%%%%%%%%%%%%%%%%%%%%%%%%
q = 1.6021766208e-19; %q = electron charge, coulombs
c = 2.99792458e8; %c = speed of light, m/s
kk = 1.380649e-23; %k or $k_B$ Bolzmann constant, J/K
kk_eV = kk/q;
T = 300; %temperature, K
%{
h = 6.626068e-34;
%}
h = 6.62607015e-34; %planck constant, J*s, definition
h_eV = h/q;
hbar = h/(2*pi);
hbar_eV = hbar/q;
epsilon0 = 8.854187817e-12; %\epsilon_0$ = permittivity of vacuum, F/m
P = -1;
rL = 1; %$R_l$ = Left mirror reflectivity, unit-less
rB = 0.2 ;
tB = sqrt(1-rB^2);
Qt = 500; %Q_T = Total Q-factor, unit-less
% Qc =180;
Qi = 14300; %Q_i = Intrinsic Q-factor, unit-less
Qp = 10000; %Q_p = cross-port Q-factor, unit-less
%Qc = 1800;
L = 5e-6; %L = waveguide cavity length, m
ng = 3.5; %$\eta_g$ = group refractive index, unit-less
sigma = 3;
m = 25;
A = 0.105e-12; %2->0 quenching rate coefficient ??
confine = 0.5;
Vp = 0.576e-18;
Vc = confine*A*L;
gN =5e-20;
Ntr = 1e24;
tc = 0.5e-9;
%tc = 0.7e-9;
alphai = 1000; %$\alpha_i$ = internal loss factor, 1/m
%alpha = 1.5;
alpha = 1; %$\alpha$ = Linewidth enhancement factor, unit-less
confinec = 0.3;
vg = c/ng;
vgp = c/6/ng;
Vpc = 0.22e-18;
KD = confinec*vgp*alpha*gN/2;
KA = -confinec*vgp*gN/2;
lambda0 = 1554e-9; %$\lambda_r$ = Col resonance wavelength
deltalambda = 0.01e-9;
lambda_start = 1540e-9;
lambda_end = 1560e-9;
lambda = lambda_start:deltalambda:lambda_end;
omega = 2*pi*c./lambda;
omega0 = omega(lambda==lambda0); %MARK
omega = omega0;
E = hbar_eV.*omega;
gamai = omega./2/Qi;
gamap = omega./2/Qp;
gamat = omega./2/Qt;
%gamac = omega./2/Qc;
%gamac = omega./2/Qc;
gamac = gamat-gamai-gamap;
gama1 = gamac./2;
gama2 = gama1;
COS2 = 0.5*gama1./gama1.*tB^2/rB-0.5*tB^2/rB-rB;
SIN2 = P*tB./(2.*gama1*rB).*sqrt(4.*gama1.*gama1 - tB^2.*(gamac.^2));
% CS12 = 1/(1i*tB).*(1i*SIN2 + rB);
GLA = gN*vgp./(E.*Vpc*q);
GN = confine*vg*gN;
tin = 2*L/vg;
M = 0.5*(length(E)-1);
Tmin = 0;
Tmax = 5e-9;
dt = 0.01e-12;
Time = Tmin:dt:Tmax;
N0 = zeros(1,4);
N0(3) = 0.2;
%I =2e-3;TA I =12e-3;
I =0.5e-3;
Vnc= 0.24e-18;
n=3.5;
Nss=(I./(E.*Vp))*tc;
Walter Roberson
on 4 Feb 2020
I ran a modified version of the code that restricts omega to one value so that the variable sizes all match up in the function. Almost immediately it started needing to use a timestep of approximately 1e-37 to try to deal with how steep the variables are -- since you do, after-all, go from a boundary condition of 0.2 to an output value above 1e24. With a time-step of about 1e-37, you would take a very long time to finish.
When ode45 needs very fine time steps, it is often an indication that the system is "stiff" and needs to use a stiff solver, ode15s or ode23s.
When I use ode23s, it gives a large number of warnings about matrix being singular, but makes it through the complete time span. The z outputs are all complex-valued, and it is difficult to see how any of them could be useful.
When I use ode15s, it does not give any warnings about matrix being singular, but it stops at about time 1e-10 saying that it has probably encountered a singularity and cannot meet integration tolerances. The partial output it produces when it stops has z outputs that are all complex-valued and it is difficult to see how any of them could be useful.
mohammad heydari
on 4 Feb 2020
Walter Roberson
on 4 Feb 2020
Sorry, I do not appear to be able to communicate successfully with you on this matter. Perhaps some other volunteer will have more success. However, in my experience, most of the other volunteers are much stricter about requiring commented code than I am.
mohammad heydari
on 4 Feb 2020
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!