Matlab 代码中的步骤有问题。需要帮助

Matlab 代码中的步骤有问题。需要帮助,matlab,plot,Matlab,Plot,晚上好,, 我有一个代码可以正常工作,如下所示: r=1.6; M=0.000207; D=-0.0256; kappa=0.5; gamma=20;%\W\km Pp=0.2; L=462.5; SQ=sqrt(-(0.5*D)^2 +M); z=0:0.1:L; A=kappa*SQ.*(z-L); B=((sqrt(M)/r)+(D/2))/(SQ); C=EA(B); E=SQ*cot(A+C); Pminus=E-(D/2); Pplus=M./Pminus; P0=Pplus+Pmi

晚上好,, 我有一个代码可以正常工作,如下所示:

r=1.6;
M=0.000207;
D=-0.0256;
kappa=0.5;
gamma=20;%\W\km
Pp=0.2;
L=462.5;
SQ=sqrt(-(0.5*D)^2 +M);
z=0:0.1:L;
A=kappa*SQ.*(z-L);
B=((sqrt(M)/r)+(D/2))/(SQ);
C=EA(B);
E=SQ*cot(A+C);
Pminus=E-(D/2);
Pplus=M./Pminus;
P0=Pplus+Pminus+D;
CST1=Pplus.*Pminus;
CST2=P0-Pplus-Pminus;
dP0dz=kappa.*P0.*(Pplus-Pminus);
figure
set(gca,'fontsize',18)
plot(z,Pplus*1000, 'b',z,Pminus*1000,'r',z,P0*1000,'k','Linewidth',4);
xlabel('z(m)')
ylabel('Power (mW)')
legend('P_+','P_-','P_0','D','M','r','Orientation','horizontal')
%str = sprintf('D = %.4f , M = %.9f, and r =%.3f ',D,M,r);
%title(str);
xlim([0 L])
N=gamma.*Pp;%gamma Pp with units \km
y1=0.5*kappa.*1000*(Pplus+Pminus);
y2 =y1./N;
figure
hax=axes;
[ax,p1,p2] = plotyy(z,y1,z,y2,'plot','plot');
 set(ax,{'ycolor'},{'k';'k'}) 
set(ax,{'fontsize'},{18;18})
 set(p1,'linewidth',4)% to change the first line
 set(p2,'linewidth',4) % to change the second line
 set(p1,'Color','b')% to change the first line
 set(p2,'Color','b') % to change the second line
%str = sprintf('D = %.6f  ',D);
%title(str);
ylabel(ax(1),'K_{SBS}(km^{-1})','fontsize',18,...
 'Color','k') % label left y-axis
ylabel(ax(2),'K_{SBS}( \gamma P )','fontsize',18,...
'Color','k') % label right y-axis
xlabel(ax(2),'z(m)','fontsize',18,...
'Color','k')% label x-axis
set(ax(1),'XLim',[0 L])
set(ax(2),'XLim',[0 L])
set(ax(1),'YLim',[min(y1) max(y1)])
set(ax(2),'YLim',[min(y2) max(y2)])
zz1=(max(y1)-min(y1))./4;
zz2=(max(y2)-min(y2))./4;
set(ax(1),'YTick',min(y1):zz1: max(y1) )
set(ax(2),'YTick',min(y2):zz2: max(y2) )
legend('K_{SBS}(km^{-1})')
% figure
% set(gca,'fontsize',18)
% plot(z,1000*dP0dz,'Linewidth',4);
% xlabel('z(m)')
% ylabel('dP_0/dz')
% xlim([0 L])
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=gamma.*Pp;%\km
lamdazero=1550;
lamdapump=linspace(1535,1560,100000);
lamdasignal=1545;
beta3=0.06;
beta4=-1*10^-4;
c=2*pi*299792458/1000;

A0=(1./lamdapump) -(1./lamdazero);
B0=(1./lamdapump) -(1./lamdasignal);

Third0=beta3.*(c.^3).*A0.*(B0.^2);
Fourth0=beta4.*(1./2).*c.^4.*(A0.^2).*(B0.^2);

Fourorder=c.^4.*beta4.*(1/12).*(B0).^4;
deltabeta=Third0+Fourth0+Fourorder;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%NO SBS
total0=deltabeta+(2.*N);
g0=((sqrt((N.^2)-(total0./2).^2)));
Gain0=((((N./(g0)).^2).*(sinh(g0.*L/1000)).^2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
g0=1.1*10^-11;
Aeff=1.1*10^-11;
PSBS=0.016;
SBS=(g0.*PSBS.*1000)./Aeff; 
totalsbs=deltabeta+(2.*N)-SBS/2;
gsbs=((sqrt((N.^2)-(totalsbs./2).^2)));
Gainsbs=((((N./(gsbs)).^2).*(sinh(gsbs.*L/1000)).^2));
n=35;
first=round(1*(L/n));
step=round(L/n);
last=L;
 for jj=1:length(deltabeta)
 M = eye(2);
 for ii = first:step:last;
 tsv=deltabeta(jj)+(2*N)-(y1(z==(ii-round(0.5*(L/n)))));
 gsv=((sqrt((N.^2)-(tsv./2).^2)));
 F1=1i*tsv;
 F2=2*gsv;
 F3=F1./F2;
 F4=1i*(N./gsv);
 M1=cosh(gsv.*(step/1000))+(F3.*sinh(gsv.*(step/1000)));
 M2=F4.*sinh(gsv.*(step/1000));
 M3=-F4.*sinh(gsv.*(step/1000));
 M4=cosh(gsv.*(step/1000))-(F3.*sinh(gsv.*(step/1000)));
 M=M*[M1 M2;M3 M4];
 TeRm=P0(z==ii)/P0(z==(ii-step));
 M=[sqrt(TeRm) 0;0 1]*M;
 end
 Gv(1,jj)=M(1,1).*conj(M(1,1));
 end
FigHandle = figure;
figureh=plot(lamdapump,10*log10(1+Gain0),'b',lamdapump,10*log10(1+Gainsbs),'g',lamdapump,10*log10(Gv),'r','Linewidth',5);
hold on
xlabel('\lambda_P (nm)','Fontsize',18)
ylabel('Signal Gain (dB)','Fontsize',18)
legend('Without SBS',' With SBS ideal ',' With SBS actual')
% title('Gain curves with SBS','fontsize',18)
set(gca,'XTick',1536:3:1560)
set(gca,'fontsize',18)
ylim([0 12])
hold off
我的问题是,当将参数L更改为小于1的值时,如对参数进行一些更改:

    r=1.8;
M=0.80322;
D=-1.69;
kappa=700/4.6;
gamma=5285;%\W\km
Pp=7.6;
L=0.045333;
SQ=sqrt(-(0.5*D)^2 +M);
z=0:0.00001:L;
A=kappa*SQ.*(z-L);
B=((sqrt(M)/r)+(D/2))/(SQ);
C=EA(B);
E=SQ*cot(A+C);
Pminus=E-(D/2);
Pplus=M./Pminus;
P0=Pplus+Pminus+D;
CST1=Pplus.*Pminus;
CST2=P0-Pplus-Pminus;
dP0dz=kappa.*P0.*(Pplus-Pminus);
figure
set(gca,'fontsize',18)
plot(z,Pplus*1000, 'b',z,Pminus*1000,'r',z,P0*1000,'k','Linewidth',4);
xlabel('z(m)')
ylabel('Power (mW)')
legend('P_+','P_-','P_0','D','M','r','Orientation','horizontal')
%str = sprintf('D = %.4f , M = %.9f, and r =%.3f ',D,M,r);
%title(str);
xlim([0 L])
N=gamma.*Pp;%gamma Pp with units \km
y1=0.5*kappa.*1000*(Pplus+Pminus);
y2 =y1./N;
figure
hax=axes;
[ax,p1,p2] = plotyy(z,y1,z,y2,'plot','plot');
 set(ax,{'ycolor'},{'k';'k'}) 
set(ax,{'fontsize'},{18;18})
 set(p1,'linewidth',4)% to change the first line
 set(p2,'linewidth',4) % to change the second line
 set(p1,'Color','b')% to change the first line
 set(p2,'Color','b') % to change the second line
%str = sprintf('D = %.6f  ',D);
%title(str);
ylabel(ax(1),'K_{SBS}(km^{-1})','fontsize',18,...
 'Color','k') % label left y-axis
ylabel(ax(2),'K_{SBS}( \gamma P )','fontsize',18,...
'Color','k') % label right y-axis
xlabel(ax(2),'z(m)','fontsize',18,...
'Color','k')% label x-axis
set(ax(1),'XLim',[0 L])
set(ax(2),'XLim',[0 L])
set(ax(1),'YLim',[min(y1) max(y1)])
set(ax(2),'YLim',[min(y2) max(y2)])
zz1=(max(y1)-min(y1))./4;
zz2=(max(y2)-min(y2))./4;
set(ax(1),'YTick',min(y1):zz1: max(y1) )
set(ax(2),'YTick',min(y2):zz2: max(y2) )
legend('K_{SBS}(km^{-1})')
% figure
% set(gca,'fontsize',18)
% plot(z,1000*dP0dz,'Linewidth',4);
% xlabel('z(m)')
% ylabel('dP_0/dz')
% xlim([0 L])
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=gamma.*Pp;%\km
lamdazero=1556;
lamdapump=linspace(1300,1800,100000);
lamdasignal=1536;
beta3=1.26;
beta4=0;
c=2*pi*299792458/1000;

A0=(1./lamdapump) -(1./lamdazero);
B0=(1./lamdapump) -(1./lamdasignal);

Third0=beta3.*(c.^3).*A0.*(B0.^2);
Fourth0=beta4.*(1./2).*c.^4.*(A0.^2).*(B0.^2);

Fourorder=c.^4.*beta4.*(1/12).*(B0).^4;
deltabeta=Third0+Fourth0+Fourorder;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%NO SBS
total0=deltabeta+(2.*N);
g0=((sqrt((N.^2)-(total0./2).^2)));
Gain0=((((N./(g0)).^2).*(sinh(g0.*L/1000)).^2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
g0=0.7*10^-9;
Aeff=2.3*10^-12;
PSBS=Pp*4/57.5;
SBS=(g0.*PSBS.*1000)./Aeff; 
totalsbs=deltabeta+(2.*N)-SBS/2;
gsbs=((sqrt((N.^2)-(totalsbs./2).^2)));
Gainsbs=((((N./(gsbs)).^2).*(sinh(gsbs.*L/1000)).^2));

n=35;
first=(1*(L/n));
step=(L/n);
last=L;
 for jj=1:length(deltabeta)
 M = eye(2);
 for ii = first:step:last;
 tsv=deltabeta(jj)+(2*N)-(y1(z==(ii-(0.5*(L/n)))));
 gsv=((sqrt((N.^2)-(tsv./2).^2)));
 F1=1i*tsv;
 F2=2*gsv;
 F3=F1./F2;
 F4=1i*(N./gsv);
 M1=cosh(gsv.*(step/1000))+(F3.*sinh(gsv.*(step/1000)));
 M2=F4.*sinh(gsv.*(step/1000));
 M3=-F4.*sinh(gsv.*(step/1000));
 M4=cosh(gsv.*(step/1000))-(F3.*sinh(gsv.*(step/1000)));
 M=M*[M1 M2;M3 M4];
 TeRm=P0(z==ii)/P0(z==(ii-step));
 M=[sqrt(TeRm) 0;0 1]*M;
 end
 Gv(1,jj)=M(1,1).*conj(M(1,1));
 end
FigHandle = figure;
figureh=plot(lamdapump,10*log10(1+Gain0),'b',lamdapump,10*log10(1+Gainsbs),'g',lamdapump,10*log10(Gv),'r','Linewidth',5);
hold on
xlabel('\lambda_P (nm)','Fontsize',18)
ylabel('Signal Gain (dB)','Fontsize',18)
legend('Without SBS',' With SBS ideal ',' With SBS actual')
title('Gain curves with SBS','fontsize',18)
%set(gca,'XTick',1300:10:1800)
set(gca,'fontsize',18)
%ylim([0 12])
hold off
代码不起作用。在我看来,原因是当使用舍入函数时,它会舍入为零。如果我删除round函数,它将不再工作。关于如何使我的代码再次工作,有什么想法吗? 因为我需要采取n个步骤,所以是否有机会处理循环函数。但是我不能这样做,因为我很小。我需要L的值来保持小。
我为冗长的代码和解释道歉。请随便问!]

我无法运行您的代码,这使得在没有具体问题的情况下很难帮助您。请提供(1)一个最小的工作示例和(2)非常具体地定义您不工作的意思(错误消息,错误结果,…)早上好,我必须道歉,因为缺少函数EA:function y=EA(x)y=acot(x);Y(关于为什么它不起作用,我有一种感觉,因为参数L现在小得多,我想采取某些步骤,它只会给出零步,因此没有做功。如果你使用
轮,那肯定是这样的,但是你在第二个代码中删除了它。在第二个例子中,我必须删除轮。否则它会被删除将我的步骤四舍五入为零,然后错误显示:使用==>vertcat参数时出错维度不一致。我不知道这意味着什么,我不确定我还能做什么:(