Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Need help resolving error "Index exceeds matrix dimensions"

1 vue (au cours des 30 derniers jours)
Amanda Lococo
Amanda Lococo le 6 Avr 2018
Clôturé : MATLAB Answer Bot le 20 Août 2021
I keep getting this error when trying to run my code, but I'm not sure how to resolve it. Here is the error I'm getting:
"Index exceeds matrix dimensions.
Error in code>@(x)([exp(im*k(j,1)*x(j,1)),0;0,exp(-im*k(j,1)*x(j,1))])
Error in code (line 57)
C = C(x);
Thanks in advance!
clear all;
format long;
im = sqrt(-1);
CellLength = 1;
ibeta = 1;
%Define materal properties
CellLength = 1;
layers = 2;
d = [0.4;0.6];
dTotal = d(1,1)+d(2,1);
xc = [0;0.4];
Ef = 12;
pf = 3;
cf = sqrt(Ef/pf);
Em = 1;
pm = 1;
cm = sqrt(Em/pm);
w = 5;
T1 = [cos(.2*w) (1/(6*w))*sin(.2*w); -6*w*sin(.2*w) cos(.2*w)];
T2 = [cos(.6*w) (1/w)*sin(.6*w); -w*sin(.6*w) cos(.6*w)];
T = T2*T1;
Z1 = 6*w;
Z2 = w;
Z = [Z1;Z2];
%Solve eigenvalue problem for k
[V,D] = eig(T); %D = eigenvalues, %V = eigenvectors
k1 = log(D(1,1))/(im*dTotal);
k2 = log(D(2,2))/(im*dTotal);
k = [k1;k2];
for j = 1:layers
B = @(j)([1 1; im*Z(j,1) -im*Z(j,1)]);
B = B(j);
C_a = @(j)([exp(im*k(j,1)*xc(j,1)) 0; 0 exp(-im*k(j,1)*xc(j,1))]);
C_a = C_a(j);
if j == 1
a = (inv(B)*V(:,1));
alpha = a;
beta = B;
else
a = inv(C_a)*inv(B)*T*beta*alpha;
end
for x = 0:0.1:5
C = @(x)([exp(im*k(j,1)*x(j,1)) 0; 0 exp(-im*k(j,1)*x(j,1))]);
C = C(x);
y = @(x)(B*C*a);
y = y(x);
end
end

Réponses (1)

Andrew Davies
Andrew Davies le 6 Avr 2018
Your problem is that the loop starts with x=0. You then try to access C(x). Arrays start indexing at 1 in MATLAB, so you can't have the zeroth element. Hence the error.
  1 commentaire
Amanda Lococo
Amanda Lococo le 6 Avr 2018
Hmm... I changed it to 1 and still got the same error

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by