Filter löschen
Filter löschen

Why can't I concatenate simple matrix? vertcat Dimensions of matrices being concatenated are not consistent

1 Ansicht (letzte 30 Tage)
I have the following 'simple' code whereby I make 6 complex numbers which are defined by a vector. I then want to stack these numbers in a matrix. It seems to me that if I define each number as a variable I can simply stack the variable in matrix C but if I try and just directly input them from a calculation then I can't stack the numbers:
clear
A = [1,2,3,4,5,6];
x = 2;
w0 = 2*pi*67*10^9;
wj = 2*pi*86*10^9;
wp = 2*pi*12*10^9;
maxBeta = 0.45;
wsfac = 0.6;
wifac = 1-wsfac;
ws = wsfac*wp;
wi = wifac*wp;
wshg = 2*wp;
wps = wp+ws;
wpi = wp+wi;
Ap0 = 0.5*w0/wp;
As0 = Ap0*sqrt(0.0057*wp/ws);
Ashg0 = 0;
Aps = 0;
Spi = 0;
kp = (wp/w0)*(1/(sqrt(1-(wp/wj)^2)));
ks = (ws/w0)*(1/(sqrt(1-(ws/wj)^2)));
ki = (wi/w0)*(1/(sqrt(1-(wi/wj)^2)));
kshg = (wshg/w0)*(1/(sqrt(1-(wshg/wj)^2)));
kps = (wps/w0)*(1/(sqrt(1-(wps/wj)^2)));
kpi = (wpi/w0)*(1/(sqrt(1-(wpi/wj)^2)));
delk = 3*ws*wi*wp/(2*w0*(wj^2));
modk = sqrt(wp*Ap0^2/(ws*As0^2+wp*Ap0^2));
dp = -(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x)+(maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x)+(maxBeta/2)*kps*ks*A(5)*conj(A(2))*exp(1i*(kps-ks-kp)*x)+(maxBeta/2)*kpi*ki*A(6)*conj(A(3))*exp(1i*(kpi-ki-kp)*x);
ds = (maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kps*conj(A(1))*A(5)*exp(1i*(kps-kp-ks)*x)+(maxBeta/2)*kshg*kps*conj(A(5))*A(4)*exp(1i*(kshg-kps-ks)*x);
di = (maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kpi*conj(A(1))*A(6)*exp(1i*(kpi-kp-ki)*x)+(maxBeta/2)*kshg*kpi*conj(A(6))*A(4)*exp(1i*(kshg-kpi-ki)*x);
dshg = -(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)-(maxBeta/2)*ki*kps*A(3)*A(5)*exp(1i*(ki+kps-kshg)*x) -(maxBeta/2)*kpi*ks*A(6)*A(2)*exp(1i*(kpi+ks-kshg)*x);
dps = -(maxBeta/2)*kp*ks*A(1)*A(2)*exp(1i*(kp+ks-kps)*x)+(maxBeta/2)*ki*kshg*conj(A(3))*A(4)*exp(1i*(kshg-ki-kps)*x);
dpi = -(maxBeta/2)*kp*ki*A(1)*A(3)*exp(1i*(kp+ki-kpi)*x)+(maxBeta/2)*ks*kshg*conj(A(2))*A(4)*exp(1i*(kshg-ki-kpi)*x);
B = [dp
ds
di
dshg
dps
dpi]
D = [-(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x)+(maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x)+(maxBeta/2)*kps*ks*A(5)*conj(A(2))*exp(1i*(kps-ks-kp)*x)+(maxBeta/2)*kpi*ki*A(6)*conj(A(3))*exp(1i*(kpi-ki-kp)*x);
(maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kps*conj(A(1))*A(5)*exp(1i*(kps-kp-ks)*x)+(maxBeta/2)*kshg*kps*conj(A(5))*A(4)*exp(1i*(kshg-kps-ks)*x);
(maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kpi*conj(A(1))*A(6)*exp(1i*(kpi-kp-ki)*x)+(maxBeta/2)*kshg*kpi*conj(A(6))*A(4)*exp(1i*(kshg-kpi-ki)*x);
-(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)-(maxBeta/2)*ki*kps*A(3)*A(5)*exp(1i*(ki+kps-kshg)*x) -(maxBeta/2)*kpi*ks*A(6)*A(2)*exp(1i*(kpi+ks-kshg)*x);
-(maxBeta/2)*kp*ks*A(1)*A(2)*exp(1i*(kp+ks-kps)*x)+(maxBeta/2)*ki*kshg*conj(A(3))*A(4)*exp(1i*(kshg-ki-kps)*x);
-(maxBeta/2)*kp*ki*A(1)*A(3)*exp(1i*(kp+ki-kpi)*x)+(maxBeta/2)*ks*kshg*conj(A(2))*A(4)*exp(1i*(kshg-ki-kpi)*x)];
I don't understand why MATLAB can stack the variables but not the calculated values which are the same.
This is for use in a coupled differential equation solver whereby A(1:6) are unknown and thus defining variables initially is not an option.
Pre-thanks for any help
Tom
  2 Kommentare
jonas
jonas am 13 Sep. 2018
Bearbeitet: jonas am 13 Sep. 2018
It looks like the variables are concatinated horizontally whereas the other one is vertical. When you copied the equations the ; sign went from being a 'do not display' sign to 'next row please' sign.
Thomas Dixon
Thomas Dixon am 13 Sep. 2018
Hi Jonas,
Thanks for your input. I believe I want a new row/column for example when I had 4 coupled equations I used the following code:
dA = @(x,A)[-(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x) + (maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x);
(maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x);
(maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x);
-(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)];
However now when I add some furhter terms it says it can not concatenate them. I have then simplified this to show the problem but essentially I don't understand why matrix B can be made and matrix D cannot where:
dp = -(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x)+(maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x)+(maxBeta/2)*kps*ks*A(5)*conj(A(2))*exp(1i*(kps-ks-kp)*x)+(maxBeta/2)*kpi*ki*A(6)*conj(A(3))*exp(1i*(kpi-ki-kp)*x);
ds = (maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kps*conj(A(1))*A(5)*exp(1i*(kps-kp-ks)*x)+(maxBeta/2)*kshg*kps*conj(A(5))*A(4)*exp(1i*(kshg-kps-ks)*x);
di = (maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kpi*conj(A(1))*A(6)*exp(1i*(kpi-kp-ki)*x)+(maxBeta/2)*kshg*kpi*conj(A(6))*A(4)*exp(1i*(kshg-kpi-ki)*x);
dshg = -(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)-(maxBeta/2)*ki*kps*A(3)*A(5)*exp(1i*(ki+kps-kshg)*x) -(maxBeta/2)*kpi*ks*A(6)*A(2)*exp(1i*(kpi+ks-kshg)*x);
dps = -(maxBeta/2)*kp*ks*A(1)*A(2)*exp(1i*(kp+ks-kps)*x)+(maxBeta/2)*ki*kshg*conj(A(3))*A(4)*exp(1i*(kshg-ki-kps)*x);
dpi = -(maxBeta/2)*kp*ki*A(1)*A(3)*exp(1i*(kp+ki-kpi)*x)+(maxBeta/2)*ks*kshg*conj(A(2))*A(4)*exp(1i*(kshg-ki-kpi)*x);
B = [dp;
ds;
di;
dshg;
dps;
dpi]
D = [-(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x)+(maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x)+(maxBeta/2)*kps*ks*A(5)*conj(A(2))*exp(1i*(kps-ks-kp)*x)+(maxBeta/2)*kpi*ki*A(6)*conj(A(3))*exp(1i*(kpi-ki-kp)*x);
(maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kps*conj(A(1))*A(5)*exp(1i*(kps-kp-ks)*x)+(maxBeta/2)*kshg*kps*conj(A(5))*A(4)*exp(1i*(kshg-kps-ks)*x);
(maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kpi*conj(A(1))*A(6)*exp(1i*(kpi-kp-ki)*x)+(maxBeta/2)*kshg*kpi*conj(A(6))*A(4)*exp(1i*(kshg-kpi-ki)*x);
-(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)-(maxBeta/2)*ki*kps*A(3)*A(5)*exp(1i*(ki+kps-kshg)*x) -(maxBeta/2)*kpi*ks*A(6)*A(2)*exp(1i*(kpi+ks-kshg)*x);
-(maxBeta/2)*kp*ks*A(1)*A(2)*exp(1i*(kp+ks-kps)*x)+(maxBeta/2)*ki*kshg*conj(A(3))*A(4)*exp(1i*(kshg-ki-kps)*x);
-(maxBeta/2)*kp*ki*A(1)*A(3)*exp(1i*(kp+ki-kpi)*x)+(maxBeta/2)*ks*kshg*conj(A(2))*A(4)*exp(1i*(kshg-ki-kpi)*x)];
Where the variables are defined as above. The problem I have is that I see each row as a simple number in the simplified version and in my workspace I can see the numbers corresponding to the variables in matrix B. But then for some reason when I put in the equations relating to the variables it stops working. In the full code i would not know the variables A(n) because it is being simultaneously solved in the ode45.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 13 Sep. 2018
You have a stray space in your expression for the 4th entry of D. This makes Matlab see two entries instead of one. Copy over it with the following,
D = [-(maxBeta/2)*ks*ki*A(2)*A(3)*exp(-1i*delk*x)+(maxBeta/2)*kshg*A(4)*kp*conj(A(1))*exp(1i*(kshg-2*kp)*x)+(maxBeta/2)*kps*ks*A(5)*conj(A(2))*exp(1i*(kps-ks-kp)*x)+(maxBeta/2)*kpi*ki*A(6)*conj(A(3))*exp(1i*(kpi-ki-kp)*x);
(maxBeta/2)*ki*kp*conj(A(3))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kps*conj(A(1))*A(5)*exp(1i*(kps-kp-ks)*x)+(maxBeta/2)*kshg*kps*conj(A(5))*A(4)*exp(1i*(kshg-kps-ks)*x);
(maxBeta/2)*ks*kp*conj(A(2))*A(1)*exp(1i*delk*x)+(maxBeta/2)*kp*kpi*conj(A(1))*A(6)*exp(1i*(kpi-kp-ki)*x)+(maxBeta/2)*kshg*kpi*conj(A(6))*A(4)*exp(1i*(kshg-kpi-ki)*x);
-(maxBeta/4)*kp^2*A(1)^2*exp(1i*(2*kp-kshg)*x)-(maxBeta/2)*ki*kps*A(3)*A(5)*exp(1i*(ki+kps-kshg)*x)-(maxBeta/2)*kpi*ks*A(6)*A(2)*exp(1i*(kpi+ks-kshg)*x);
-(maxBeta/2)*kp*ks*A(1)*A(2)*exp(1i*(kp+ks-kps)*x)+(maxBeta/2)*ki*kshg*conj(A(3))*A(4)*exp(1i*(kshg-ki-kps)*x);
-(maxBeta/2)*kp*ki*A(1)*A(3)*exp(1i*(kp+ki-kpi)*x)+(maxBeta/2)*ks*kshg*conj(A(2))*A(4)*exp(1i*(kshg-ki-kpi)*x)];
  6 Kommentare
Thomas Dixon
Thomas Dixon am 13 Sep. 2018
Not all heroes wear their pants on the outside.
This has been incredibly helpful, thanks a lot.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by