Dimension mismatch error trying to build 2D array in loop
10 次查看(过去 30 天)
显示 更早的评论
Hi, I am trying to build a matrix in a loop. Each row of the matrix should be a vector that describes a curve in in one variable (A) with the other (Co) held constant. The next loop should be the same vector as before but with Co advanced by one index. I need to then plot the final matrix with surf. I am getting a dimension mismatch when I try to write the vectors to one row (or column) of the matrix. Here is my code
clc
clear
eps0 = 8.854188e-12;
epsr = 11.2;
a = 1e-6:1e-6:20e-6; %half trace width
B =(6e-6)+a; %half trace + gap width
h = 1e-3; %dielectric substrate thickness
len=length(a);
co = linspace(80e-6,100e-6,len);
[A,Co] = meshgrid(a,co);
Z0=zeros(len);
for n=1:len
%==== C1 calculation =======================================
B =(6e-6)+A;
num3 = 1-(B./Co(n)).^2; %numerator for k3
den3= 1-(A./Co(n)).^2; %denominator for k3
k3 =(A./B).*sqrt(num3./den3);
k3_prime = (sqrt(1-(k3).^2)); %complimentary argument
C1 = 2*eps0*ellipke(k3)./ellipke(k3_prime);
%==== C2 calculation ========================================
num4 = 1-(sinh(pi*B/(2*h))).^2/(sinh(pi*Co(n)/(2*h))).^2;
den4 = 1-(sinh(pi*A/(2*h))).^2/(sinh(pi*Co(n)/(2*h))).^2;
k4 = (sinh(pi*A/(2*h))./sinh(pi*B/(2*h))).*sqrt(num4./den4);
k4_prime = (sqrt(1-(k4).^2)); %complimentary arguement
C2 = 2*eps0*(epsr-1)*ellipke(k4)./ellipke(k4_prime);
%==== Eps_re calculation ====================================
eps_re = 1+ C2./(2*C1);
%==== Z0 calculation ========================================
Z0(:,n) = 30*pi./(sqrt(eps_re)).*(ellipke(k3_prime)./ellipke(k3)); %This part broken
end
surf(A,Co,Z0);
Thanks in advance for any help, Brian
1 个评论
Roger Stafford
2017-3-15
I can’t tell how you indexed the rhs to get Z0(:,n), but if you did it in a similar manner to the way you indexed Co, you might be getting the wrong result. Co(n) gives you the equivalent of co(n) in this case because Co was the second output in meshgrid. However, if you write B(n), the result will always be the same, namely 6e-6+a(1), because A was the first output in meshgrid. To avoid confusion, you should always write Co(n,1) and B(1,n) if that is what you mean. (I am just guessing about your problem here since I don’t know the underlying theory.)
采纳的回答
Roger Stafford
2017-3-15
I believe you will find that the right hand side of
Z0(:,n) = 30*pi./(sqrt(eps_re)).*(ellipke(k3_prime)./ellipke(k3));
is a 20-by-20 matrix while it is attempting to stuff it into a 20-element column vector space. It simply won’t fit!
0 个评论
更多回答(2 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

