Hie, Please, how do i remove the error tha arises from running this code?
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
function Gain_ripples_12()
clear all; clc;
global P_1 P_2 B_2 B_3 B_4 delta_w_p delta_w_s delta_w_sb r gamma
% parameters of the model
pi= 3.143; P_1 =1;P_2 =1 ; c = 299792458;
lambda_1 = 1536;
lambda_2 = 1540;
lambda_0 = 1535;
lambda_c = (lambda_1 + lambda_2)/2;
w_0 = (2*pi*c)/lambda_0*10^-3;
w_1 = (2*pi*c)/lambda_1*10^-3;
w_2 = (2*pi*c)/lambda_2*10^-3;
w_c = (2*pi*c)/lambda_c*10^-3;
B_3 =2.0881*10^-20;B_4 =-1.852*10^-20;
gamma =8*10^-3;
B_2 = B_3*(w_c - w_0)^2 + (B_4/2)*(w_c - w_0)^4;
delta_w_p = (w_2 - w_1)/2;
%w_sb2 = 2*delta_w_p - delta_w_s;
r = 2*gamma*sqrt(P_1*P_2);
% options for the ode45
options=odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4]);
% Solve the equations using 4th order Runge-Kutta
lambda_s=1350:5:1900;
Gs={};
for ii=lambda_s
w_s = (2*pi*c)./ii;
delta_w_s = w_s - w_c;
w_sb1 = (2.*delta_w_p - delta_w_s);
delta_w_sb = w_c - w_sb1;
[~,U]=ode45(@sytem,[0 1],[0 1 0 0]);
A_sb1=U(:,1); A_s=U(:,2);
A_1 = U(:,3); A_sb2 =U(:,4);
G12 = abs(A_s).^2;
Gs12= 10.*log10(G12);
Gs{ii}=Gs12;
end
% extract solution for y1 and y2
Cunq=unique(horzcat(Gs{:}),'stable');
EF=Cunq(1:length(lambda_s));
%plot the solutions on the same graph
plot(lambda_s,EF,'r')
%legend({'$y_{1}$','$y_{2}$'},'FontSize',17,'Interpreter','latex')
ylabel('Gain','FontSize',17,'Interpreter','latex')
xlabel('Wavelength','FontSize',17,'Interpreter','latex')
%title('','FontSize',17,'Interpreter','latex')
% A function for the differential Equations
function dz=sytem(~,A)
% A_sb1 = A(5), A_s= A(3), A_1 =A(1), A_sb2=A(6)
dz=zeros(4,1);
dz(1)= 1i.*(B_2./2).*(delta_w_sb.^2 - delta_w_p.^2).*A(1) - 1i.*(B_3./6)...
.*(delta_w_sb.^3 - delta_w_p.^3).*A(1) + 1i.*(B_4./24).*(delta_w_sb.^4 - delta_w_p.^4)...
.* A(1) + 1i.*gamma.*P_1.*(A(1) + conj(A(2)))+1i.* r.*(A(3)+conj(A(4)));
dz(2)=1i.*(B_2./2).*(delta_w_sb.^2 - delta_w_p.^2).*A(2) - 1i.*(B_3./6)...
.*(delta_w_sb.^3 - delta_w_p.^3).*A(2) + 1i.*(B_4./24).*(delta_w_sb.^4 - delta_w_p.^4)...
.* A(2) + 1i.*gamma.*P_1.*(A(2) + conj(A(1)))+1i.* r.*(A(4)+conj(A(3)));
dz(3)=1i.*(B_2./2).*(delta_w_sb.^2 - delta_w_p.^2).*A(3) + 1i.*(B_3./6)...
.*(delta_w_sb.^3 - delta_w_p.^3).*A(3) + 1i.*(B_4./24).*(delta_w_sb.^4 - delta_w_p.^4)...
.* A(3) + 1i.*gamma.*P_2.*(A(3) + conj(A(4)))+1i.* r.*(A(1)+conj(A(2)));
dz(4)=1i.*(B_2./2).*(delta_w_sb.^2 - delta_w_p.^2).*A(4) + 1i.*(B_3./6)...
.*(delta_w_sb.^3 - delta_w_p.^3).*A(4) + 1i.*(B_4./24).*(delta_w_sb.^4 - delta_w_p.^4)...
.* A(4) + 1i.*gamma.*P_2.*(A(4) + conj(A(3)))+1i.* r*(A(2)+conj(A(1)));
end
end
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Gain_ripples_12 (line 41)
Cunq=unique(horzcat(Gs{:}),'stable');
0 件のコメント
回答 (1 件)
Hi,
there are 2 Problems in your code:
1. The Integration with ode45 leads to different lengths for the entries in GS{ii} - which causes the error. To solve this i changed:
[~,U]=ode45(@system,[0 1],[0 1 0 0],options);
to
[~,U]=ode45(@system,[0:0.0005:1],[0 1 0 0],options);
which guarantees same length of every cell in Gs.
2. The stepwide of:
lambda_s=1350:5:1900;
leads to a cell array Gs which has 4 empty entries, then one cell with results and so on, because the not adressed cells are automatically filled up by Zero entries. I used a addtionally counter variable k to solve this. Try this code in the for loop:
% Solve the equations using 4th order Runge-Kutta
lambda_s=1350:5:1900;
Gs={};
k = 1;
for ii=lambda_s
w_s = (2*pi*c)./ii;
delta_w_s = w_s - w_c;
w_sb1 = (2.*delta_w_p - delta_w_s);
delta_w_sb = w_c - w_sb1;
[~,U]=ode45(@system,[0:0.0005:1],[0 1 0 0],options);
A_sb1=U(:,1);
A_s=U(:,2);
A_1 = U(:,3);
A_sb2 =U(:,4);
G12 = abs(A_s).^2;
Gs12= 10.*log10(G12);
Gs{k}=Gs12;
k = k+1;
end
After making these corrections you code runs good for me and gave this result:

.
Best regards
Stephan
0 件のコメント
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!