Unable to perform assignment because the left and right sides have a different number of elements.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Athira T Das
el 28 de Jun. de 2022
Comentada: KSSV
el 28 de Jun. de 2022
clear all
clc
syms s1 s2 m1 m2 j1 j2
lambda = 1060*10^-9;
wo = 0.02;
C = 10^(-7);
M=0;
b=0.1;
x=0
y=0
z=linspace(0,2000)
I = zeros(size(z));
k=2*pi/lambda;
po=(0.545*C^2*k^2*z).^(-(3/5));
delta= ((1i*k)./(2*z))+(1./(wo.^2))+(1./(po.^2));
c_delta= subs(delta, 1i, -1i);
etta = c_delta - (1./(delta.*po.^4));
alpha = (1i.*k./(2.*z)).*(1./(delta.*po.^2)-1);
beta_p = (b./(2.*wo)).*(1./(delta.*po.^2)+1);
beta_n = (b./(2.*wo)).*(1./(delta.*po.^2)-1);
A = (alpha.^2./etta)-((k.^2)-(4.*z.^2.*delta));
B1_p = ((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_p)./etta);
B1_n = ((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_n)./etta);
B2_p = -(((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_p)./etta));
B2_n = -(((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_n)./etta));
C1_p = ((b.^2)./(4.*wo.^2.*delta))+(((beta_p).^2)./etta);
C1_n = ((b.^2)./(4.*wo.^2.*delta))+(((beta_n).^2)./etta);
C2_p = ((b.^2)./(4.*wo.^2.*delta))+(((beta_n).^2)./etta);
C2_n = ((b.^2)./(4.*wo.^2.*delta))+(((beta_p).^2)./etta);
for i = 1:100
for m1=0:M
for m2=0:M
for s1=0:m1/2
for j1=0:m1-2*s1
for s2=0:(M-m1)/2
for j2=0:M-m1-2*s2
G_1=sum((factorial(m1).*(-1).^s1)./(factorial(s1).*factorial(m1-2.*s1)).*(2.*1i./sqrt(delta)).^(m1-2*s1),'all');
G_2=sum(factorial(m1-2.*s1)./(factorial(j1).*factorial(m1-2.*s1-j1)).*((1./(2.*1i.*sqrt(etta))).^(m2+j1)).*(1./po.^2).^j1,'all');
G_p3=((1i.*k.*x)./(2.*z(i))+(b./2.*wo)).^(m1-2.*s1-j1).*exp((A.*x.^2)+(B1_p.*x)+C1_p).*hermiteH(m2+j2,((1i.*alpha.*x)./sqrt(etta))+((1i.*beta_p)./sqrt(etta)));
G_p4=((1i.*k.*x)./(2.*z(i))-(b./2.*wo)).^(m1-2.*s1-j1).*exp((A.*x.^2)+(B2_n.*x)+C2_n).*hermiteH(m2+j2,((1i.*alpha.*x)./sqrt(etta))-((1i.*beta_n)./sqrt(etta)));
G_p=G_1.*G_2.*(G_p3 + G_p4);
G_n3=((1i.*k.*x)./(2.*z(i))+(b./2.*wo)).^(m1-2.*s1-j1).*exp((A.*x.^2)+(B1_n.*x)+C1_n).*hermiteH(m2+j2,((1i.*alpha.*x)./sqrt(etta))+((1i.*beta_n)./sqrt(etta)));
G_n4=((1i.*k.*x)./(2.*z(i))-(b./2.*wo)).^(m1-2.*s1-j1).*exp((A.*x.^2)+(B2_p.*x)+C2_p).*hermiteH(m2+j2,((1i.*alpha.*x)./sqrt(etta))-((1i.*beta_p)./sqrt(etta)));
G_n=G_1.*G_2.*(G_n3 + G_n4);
H_1=sum((factorial(M-m1).*(-1).^s2)./(factorial(s2).*factorial(M-m1-2.*s2)),'all').*(2.*1i./sqrt(delta)).^(M-m1-2.*s2);
H_2=sum(factorial(M-m1-2.*s2)./(factorial(j2).*factorial(M-m1-2.*s2-j2)),'all')*((1./(2.*1i.*sqrt(etta))).^(M-m2+j2)).*(1./po.^2).^j2;
H_p3=((1i.*k.*y)./(2.*z(i))+(b./2.*wo)).^(M-m1-2.*s2-j2).*exp((A.*y.^2)+(B1_p.*y)+C1_p).*hermiteH(M-m2+j2,((1i.*alpha.*y)./sqrt(etta))+((1i.*beta_p)./sqrt(etta)));
H_p4=((1i.*k.*y)./(2.*z(i))-(b./2.*wo)).^(M-m1-2.*s2-j2).*exp((A.*y.^2)+(B2_n.*y)+C2_n).*hermiteH(M-m2+j2,((1i.*alpha.*y)./sqrt(etta))-((1i.*beta_n)./sqrt(etta)));
H_p=H_1.*H_2.*(H_p3 + H_p4);
H_n3=((1i.*k.*y)./(2.*z(i))+(b./2.*wo)).^(M-m1-2.*s2-j2).*exp((A.*y.^2)+(B1_n.*y)+C1_n).*hermiteH(M-m2+j2,((1i.*alpha.*y)./sqrt(etta))+((1i.*beta_n)./sqrt(etta)));
H_n4=((1i.*k.*y)./(2.*z(i))-(b./2.*wo)).^(M-m1-2.*s2-j2).*exp((A.*y.^2)+(B2_p.*y)+C2_p).*hermiteH(M-m2+j2,((1i.*alpha.*y)./sqrt(etta))-((1i.*beta_p)./sqrt(etta)));
H_n=H_1.*H_2.*(H_n3 + H_n4);
G=G_p+G_n;
H=H_p+H_n;
I(i) =G.*H;
end
end
end
end
end
end
endUnable to perform assignment because the left and right sides have a different number of elements.
plot(z,I)
xlabel('z')
ylabel('I')
xlim([0 8000])
ylim([0 1])
0 comentarios
Respuesta aceptada
KSSV
el 28 de Jun. de 2022
Error is simple.... you are trying to save more number of elements in an array then it is initialized for.
Example:
A = zeros(3,3) ; % 3x3 matrix initialized
A(1,:) = rand(1,3) ; % no error, in first row three elements saved
A(2,:) = 0 ; % no error, in second row all zeros saved
A(3,:) = rand(1,4); % error, you canntt save four elemnts in a place of three elements.
Similiarly, this line:
I(i) =G.*H;
RHS is 1x100 array; you are trying to save 100 elements in a single elemet. Replace that line with:
I(i,:) =G.*H;
2 comentarios
KSSV
el 28 de Jun. de 2022
It looks like all your values are NaN. Print them and see. You need to re check your code. To be precise, your code can be vectorised and many loops are not required.
Más respuestas (1)
Garmit Pant
el 28 de Jun. de 2022
Hello Athira
It is my understanding that you need help in resolving the error message :
% Unable to perform assignment because the left and right sides have a different number of elements.
Having gone through the code and running it myself, I can see the problem is arrising due to the following code snippet:
G=G_p+G_n;
H=H_p+H_n;
I(i) =G.*H;
G and H are row vectors with size 1x100. Since you are using the element wise multiplication operation .* the result of G.*H will also be a row vector of the size 1x100.
I has been defined as:
I = zeros(size(z))
Thus I has size 1x100, with each element of I being a zero. Thus, I(i) has a size of 1x1. It is due to this mismatch of sizes that the assignment cannot be performed.
You can refer to this article to learn more about Array and Matrix operations: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!