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

1 visualizzazione (ultimi 30 giorni)
%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 Commento
Abderrahim. B
Abderrahim. B il 17 Lug 2022
Modificato: Abderrahim. B il 17 Lug 2022
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

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 17 Lug 2022
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 Commenti
SAHIL SAHOO
SAHIL SAHOO il 18 Lug 2022
plot((k./3e-3), mean(M,2));
why you mean by mean(M,2) ?
Torsten
Torsten il 18 Lug 2022
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.

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 17 Lug 2022
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.

Categorie

Scopri di più su Data Synthesis in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by