I couldn't understand what's wrong in this.

3 次查看(过去 30 天)
%Unable to perform assignment because the left and right sides have a different number of elements.
clc
%defining constant
ti = 0; %inital time
tf = 100E-4;% final time
tspan=[ti tf];
o = 2E5; % detuning frequency
tc = 30E-9; %photon life time in cavity
tf = 230E-6; %flouroscence lifetime
a1 = 0.1; %round trip loss
a2 = 0.1;
P1 = 0.2; %pump strenght
P2 = 0.2;
kc = 3E-3; %critical coupling strength
%s = 0.17;
%k = s.*kc;
I = 1; %saturation intensity
V = 1;
% define function
%y(1) = A1
%y(2) = A2
%y(3) = G1
%y(4) = G2
%y(5) = phase difference
f = @(t,y,k) [
(((y(3)./V) - a1) * y(1) + k * y(2) * cos(y(5))) ./ tc;
(((y(4)./V) - a2) * y(2) + k * y(1) * cos(y(5))) ./ tc;
(P1 - ((y(3)./V) * (((abs(y(1)))^2 ./I) + 1))) / tf;
(P2 - ((y(4)./V) * (((abs(y(2)))^2 ./I) + 1))) / tf;
o - (k / tc) * (((y(1) ./ y(2)) + (y(2) ./ y(1))) * sin(y(5)));
];
k =0:0.01E-3:3.2E-3;
Vf = zeros(length(k),1) ;
for i = 1:length(k)
[T,Y] = ode45(@(t,y)f(t,y,k(i)),tspan,[1;1;1;1;0].*10E-2);
Vf(i) = (2.*(((mean(Y(:,1).*Y(:,2).*cos(Y(:,5)))).^2 + (mean(Y(:,1).*Y(:,2).*sin(Y(:,5)))).^2).^0.5))/(mean((Y(:,1)).^2)+mean((Y(:,2)).^2));
end
k =[0:0.01E-3 : 3.2E-3] ;
Vp = zeros(length(k),1) ;
for i = 1:length(k)
[T,Y]= ode45(@(t,y)f(t,y,k(i)),tspan,[1;1;1;1;0].*10E-2);
Vp(i) = ((mean(cos(Y(:,5)))).^2 + (mean(sin(Y(:,5)))).^2).^0.5;
end
%maybe here is the problem
k =[0:0.01E-3 : 3.2E-3] ;
T = zeros(length(k),1) ;
for i = 1:length(k)
[T,Y]= ode45(@(t,y)f(t,y,k(i)),tspan,[1;1;1;1;0].*10E-2);
T(i) = 1 - (k./kc).*(1/2).*(((Y(:,1) ./ Y(:,2)) + (Y(:,2) ./ Y(:,1))).* sin(Y(:,5)))
end
Unable to perform assignment because the left and right sides have a different number of elements.
subplot 221
plot((k./3E-3),Vf)
subplot 222
plot((k./3E-3),Vp)
subplot 223
plot((k./3E-3),T)
  1 个评论
Abderrahim. B
Abderrahim. B 2022-7-17
编辑:Abderrahim. B 2022-7-17
Hi!
You can not assign a matrix of size 3501 x 321 to a matrix (row vector) of size 3501x1.
% The error is in this line:
T(i) = 1 - (k./kc).*(1/2).*(((Y(:,1) ./ Y(:,2)) + (Y(:,2) ./ Y(:,1))).* sin(Y(:,5)));
I recommend to debug your code.
HTH

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-7-17
for i = 1:length(k)
[T,Y]= ode45(@(t,y)f(t,y,k(i)),tspan,[1;1;1;1;0].*10E-2);
T(i) = 1 - (k./kc).*(1/2).*(((Y(:,1) ./ Y(:,2)) + (Y(:,2) ./ Y(:,1))).* sin(Y(:,5)))
end
Your tspan is a vector of length exactly 2. When tspan is a vector with exactly two elements, ode45() will return a vector of T values and a 2D array of Y values, with it internally deciding which times to emit results for. The number of entries returned is relatively unpredictable. The number of rows of Y will be the same as the number of entries in T..
You then index particular columns of the 2D array of Y values -- making calculations for all of the times. The number of results on the right side will be the same as the number of times.
You then try to overwrite one particular time entry with the vector calculated on the right hand side.
Note that before the for loop you pre-allocated T, but your call to ode45() then overwrites all of T. You probably want to use different variables there.
On the other ode45 loops, you were calculating mean() of values, and so only returned a single value rather than a vector. And in the other loops, you did not accidentally use the same variable for two different purposes.
  7 个评论
SAHIL SAHOO
SAHIL SAHOO 2022-7-18
plot((k./3e-3), mean(M,2));
why you mean by mean(M,2) ?
Torsten
Torsten 2022-7-18
From the documentation of "mean":
M = mean(A,dim) returns the mean along dimension dim. For example, if A is a matrix, then mean(A,2) is a column vector containing the mean of each row.

请先登录,再进行评论。

更多回答(1 个)

KSSV
KSSV 2022-7-17
The error is simple. You are trying to save more number of elements in LHS than it is initialized for.
Demo
A = zeros(1,3) ; % 1x3 array intialized
A(1) = rand ; % no error, you can save one element in one place
A(2) = rand(1,2) ; % error, you cannot save two elements in a sinlge element place
Unable to perform assignment because the left and right sides have a different number of elements.
Similarly, check the dimensions in your case, where error occurs.

类别

Help CenterFile Exchange 中查找有关 Data Synthesis 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by