Effacer les filtres
Effacer les filtres

Troubleshooting tips

2 vues (au cours des 30 derniers jours)
Alex
Alex le 6 Mar 2012
Commenté : sabeeh ullah le 14 Oct 2021
Hey all,
I'm creating a function to pass to fsolve, to be solved in terms of V. When I run my program, however, Matlab returns an "Index Exceeds matrix Dimensions" error. I have a feeling I might have dropped a dot in one of my dot-operators in my function, an error which the debugger won't pick up because matlab is still performing a valid operation within the equation itself. Is there any way to find the mistake causing the error? I'm posting the equation below, so you can see why this is causing me a headache. Maybe you can find the mistake? Thanks!
P.S.: This is all a single equation, and yes, this is real.
F = @(V) (((((exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) - ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y)))).*(exp(((2.*pi().*k2.*d2)./y)).*cos(((2.*pi().*n2.*d2)./y))) - ((exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y)))).*(exp(((2.*pi().*k2.*d2)./y)).*sin(((2.*pi().*n2.*d2)./y))) + ((exp((-1).*((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) - ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp((-1).*((2.*pi().*k2.*d2)./y)).*(((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2)).*cos(((2.*pi().*n2.*d2)./y)) + ((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2)).*sin(((2.*pi().*n2.*d2)./y)))) - (((-1).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp((-1).*((2.*pi().*k2.*d2)./y)).*(((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2)).*cos(((2.*pi().*n2.*d2)./y)) - ((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2)).*sin(((2.*pi().*n2.*d2)./y))))).^2 + (((exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y)))).*(exp(((2.*pi().*k2.*d2)./y)).*cos(((2.*pi().*n2.*d2)./y))) + ((exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) - ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y)))).*(exp(((2.*pi().*k2.*d2)./y)).*sin(((2.*pi().*n2.*d2)./y))) + (((-1).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp((-1).*((2.*pi().*k2.*d2)./y)).*(((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2)).*cos(((2.*pi().*n2.*d2)./y)) + ((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2)).*sin(((2.*pi().*n2.*d2)./y)))) + ((exp((-1).*((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) - ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp(((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp((-1).*((2.*pi().*k2.*d2)./y)).*(((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2)).*cos(((2.*pi().*n2.*d2)./y)) - ((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2)).*sin(((2.*pi().*n2.*d2)./y))))).^2)./((((exp(((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) - ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp(((2.*pi().*k2.*d2)./y)).*cos(((2.*pi().*n2.*d2)./y))) - ((exp(((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y))) + ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp(((2.*pi().*k2.*d2)./y)).*sin(((2.*pi().*n2.*d2)./y))) + ((exp(((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) - ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*((-1).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp((-1).*((2.*pi().*k2.*d2)./y)).*(((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2)).*cos(((2.*pi().*n2.*d2)./y)) + ((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2)).*sin(((2.*pi().*n2.*d2)./y)))) - ((exp(((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*((-1).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp((-1).*((2.*pi().*k2.*d2)./y)).*(((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2)).*cos(((2.*pi().*n2.*d2)./y)) - ((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2)).*sin(((2.*pi().*n2.*d2)./y))))).^2 + (((exp(((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y))) + ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp(((2.*pi().*k2.*d2)./y)).*cos(((2.*pi().*n2.*d2)./y))) + ((exp(((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) - ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp(((2.*pi().*k2.*d2)./y)).*sin(((2.*pi().*n2.*d2)./y))) + ((exp(((2.*pi().*V(2).*d1)./y)).*(((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) + ((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*((-1).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp((-1).*((2.*pi().*k2.*d2)./y)).*(((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2)).*cos(((2.*pi().*n2.*d2)./y)) + ((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2)).*sin(((2.*pi().*n2.*d2)./y)))) + ((exp(((2.*pi().*V(2).*d1)./y)).*(((V(1).^2 - n2.^2 + V(2).^2 - k2.^2)./((V(1) + n2).^2 + (V(2) + k2).^2)).*cos(((2.*pi().*V(1).*d1)./y)) - ((2.*(V(1).*k2 - n2.*V(2)))./((V(1) + n2).^2 + (V(2) + k2).^2)).*sin(((2.*pi().*V(1).*d1)./y)))) + ((n0.^2 - V(1).^2 - V(2).^2)./((n0 + V(1)).^2 + V(2).^2)).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*cos(((2.*pi().*V(1).*d1)./y))) - ((2.*n0.*V(2))./((n0 + V(1)).^2 + V(2).^2)).*((-1).*(exp((-1).*((2.*pi().*V(2).*d1)./y)).*sin(((2.*pi().*V(1).*d1)./y))))).*(exp((-1).*((2.*pi().*k2.*d2)./y)).*(((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2)).*cos(((2.*pi().*n2.*d2)./y)) - ((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2)).*sin(((2.*pi().*n2.*d2)./y))))).^2)) - R2;
  1 commentaire
sabeeh ullah
sabeeh ullah le 14 Oct 2021
VERY GOOD SIR

Connectez-vous pour commenter.

Réponse acceptée

Andrew Newell
Andrew Newell le 6 Mar 2012
I tried it, and it doesn't complain if you provide values for all the constants and make sure that V has two elements, for example:
d1 = rand; y = rand; n2 = rand; k2 = rand; n0 = rand; d2 = rand; n3 = rand; k3 = rand; R2 = rand;
F = @(V) ...
F(rand(2,1))
  2 commentaires
Alex
Alex le 6 Mar 2012
The actual function itself should "work" as written, the problem arises when it is passed to fsolve. This is the code:
data = csvread('data.csv');
assert (mod(size(data, 1), 2) == 0, ...
'Input data must have an integer multiple of 2 rows');
assert (size(data, 2) == 7, ...
'Input data must have exactly seven columns.');
nsys = size(data, 1) / 2;
soln = zeros(nsys, 2);
options = optimset('MaxFunEvals', 1e10, 'MaxIter', 15000);
for k = 1 : nsys,
F = two_layer_nk_1d_gen(data(2*(k-1) + (1:2), 1:end));
Guess = [];
soln(k, :) = fsolve(F, Guess, options);
end
fid = fopen('results_2layer_1d.csv','w');
fprintf(fid, '%5.5f\n', soln);
fclose(fid);
And this is the function file:
function F = two_layer_nk_1d_gen(p)
assert(ndims(p) == 2, ...
'System parameters ''p'' must be 2D matrix.');
assert(all(size(p) == [2,7]), ...
'System parameters must be 2-by-7 matrix.');
y = p(:,1);
n0 = p(:,2);
n2 = p(:,3);
n3 = p(:,4);
k2 = p(:,5);
k3 = p(:,6);
R2 = p(:,7);
d1 = .34;
d2 = 300;
F = @(V) ...
end
Alex
Alex le 6 Mar 2012
Oh, after posting this I've found the problem, I left the guess field blank. Hahaha, the things we miss =)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by