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
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
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!

更多回答(2 个)

brian egenriether
brian egenriether 2017-3-15
Thanks Roger! You're right as usual. I fixed that problem by indexing the right hand side with (n), but I came across another issue. I have several versions of this code (the cleanest I have posted here), but the problem with the others is similar to my present result. I get a result that could only be due to my final result, Z0, being constantly overwritten. I get a surface that looks like one curve repeated over the variable Co (pictured below). For whatever reason I don't seem to be able to redefine each curve of the final surf plot. Also by themselves the plots range from Z0 = 35 to 85. This problem is mine to deal with but why are my matrix rows being overwritten to result in a curve that seems to be constant over the variable on the right axis in the picture?

brian egenriether
brian egenriether 2017-3-15
编辑:brian egenriether 2017-3-15
By the way, what I meant by the statement "...by themselves the plots range from Z0 = 35 to 85..." is this plot of another version of the code (where Co is a constant; Co=100) in the picture below:

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by