Defining variables for fsolve

2 views (last 30 days)
Alex on 24 Feb 2012
Hey all, quick question:
I've been trying to pass along data to an fsolve command all morning, but I keep running into errors every time I run the program. I've been looking up information on resolving these errors all morning, and I've had some success with fixing the issues, only to have new ones replace them. I can tell from the types of errors that I am getting that the error stems in the values that I'm using as variables in the equations in fsolve, but I don't know how to remedy the issue. I've read all the literature I could find, and I've modeled the program after several examples, but to no avail. Someone with more experience than me should be able to spot the problem easily enough. Can I get a recommendation? Here's the code:
% This program aims to back-solve for reflectance (R) using given values
% input by hand.
syms x y z
y_1 = 309;
n0_1 = 1;
%n1_1 = 1.580086;
n1_1 = x;
%k1_1 = 0;
k1_1 = y;
n2_1 = 5.07;
k2_1 = 3.62;
%d1_1 = 25;
d1_1 = z;
R_1 = .4335;
y_2 = 310;
n0_2 = 1;
%n1_2 = 1.579925;
n1_2 = x;
%k1_2 = 0;
k1_2 = y;
n2_2 = 5.07;
k2_2 = 3.56;
%d1_2 = 25;
d1_2 = z;
R_2 = .4294;
y_3 = 311;
n0_3 = 1;
%n1_3 = 1.579764;
n1_3 = x;
%k1_3 = 0;
k1_3 = y;
n2_3 = 5.08;
k2_3 = 3.53;
%d1_3 = 25;
d1_3 = z;
R_3 = .4277;
g1_1 = (n0_1.^2 - n1_1.^2 - k1_1.^2)./((n1_1 + n2_1).^2 + k1_1.^2);
g2_1 = (n1_1.^2 - n2_1.^2 + k1_1.^2 - k2_1.^2)./((n1_1 + n2_1).^2 + (k1_1 + k2_1).^2);
h1_1 = (2.*n0_1.*k1_1)./((n0_1 + n1_1).^2 + k1_1.^2);
h2_1 = (2.*(n1_1.*k2_1 - n2_1.*k1_1))./((n1_1 + n2_1).^2 + (k1_1 + k2_1).^2);
a_1 = (2.*pi().*k1_1.*d1_1)./y_1;
b_1 = (2.*pi().*n1_1.*d1_1)./y_1;
A_1 = 2.*(g1_1.*g2_1 + h1_1.*h2_1);
B_1 = 2.*(g1_1.*h2_1 - g2_1.*h1_1);
C_1 = 2.*(g1_1.*g2_1 - h1_1.*h2_1);
D_1 = 2.*(g1_1.*h2_1 + g2_1.*h1_1);
g1_2 = (n0_2.^2 - n1_2.^2 - k1_2.^2)./((n1_2 + n2_2).^2 + k1_2.^2);
g2_2 = (n1_2.^2 - n2_2.^2 + k1_2.^2 - k2_2.^2)./((n1_2 + n2_2).^2 + (k1_2 + k2_2).^2);
h1_2 = (2.*n0_2.*k1_2)./((n0_2 + n1_2).^2 + k1_2.^2);
h2_2 = (2.*(n1_2.*k2_2 - n2_2.*k1_2))./((n1_2 + n2_2).^2 + (k1_2 + k2_2).^2);
a_2 = (2.*pi().*k1_2.*d1_2)./y_2;
b_2 = (2.*pi().*n1_2.*d1_2)./y_2;
A_2 = 2.*(g1_2.*g2_2 + h1_2.*h2_2);
B_2 = 2.*(g1_2.*h2_2 - g2_2.*h1_2);
C_2 = 2.*(g1_2.*g2_2 - h1_2.*h2_2);
D_2 = 2.*(g1_2.*h2_2 + g2_2.*h1_2);
g1_3 = (n0_3.^2 - n1_3.^2 - k1_3.^2)./((n1_3 + n2_3).^2 + k1_3.^2);
g2_3 = (n1_3.^2 - n2_3.^2 + k1_3.^2 - k2_3.^2)./((n1_3 + n2_3).^2 + (k1_3 + k2_3).^2);
h1_3 = (2.*n0_3.*k1_3)./((n0_3 + n1_3).^2 + k1_3.^2);
h2_3 = (2.*(n1_3.*k2_3 - n2_3.*k1_3))./((n1_3 + n2_3).^2 + (k1_3 + k2_3).^2);
a_3 = (2.*pi().*k1_3.*d1_3)./y_3;
b_3 = (2.*pi().*n1_3.*d1_3)./y_3;
A_3 = 2.*(g1_3.*g2_3 + h1_3.*h2_3);
B_3 = 2.*(g1_3.*h2_3 - g2_3.*h1_3);
C_3 = 2.*(g1_3.*g2_3 - h1_3.*h2_3);
D_3 = 2.*(g1_3.*h2_3 + g2_3.*h1_3);
Guess = [1.58,0,25];
F = @(x,y,z) [((g1_1.^2 + h1_1.^2).*(exp(2.*a_1)) + (g2_1.^2 + h2_1.^2).*(exp(-2.*a_1)) + A_1.*(cos(2.*b_1)) + B_1.*(sin(2.*b_1)))./((exp(2.*a_1)) + (g1_1.^2 + h1_1.^2).*(g2_1.^2 + h2_1.^2).*(exp(-2.*a_1)) + C_1.*(cos(2.*b_1)) + D_1.*(sin(2.*b_1))) - R_1; ...
((g1_2.^2 + h1_2.^2).*(exp(2.*a_2)) + (g2_2.^2 + h2_2.^2).*(exp(-2.*a_2)) + A_2.*(cos(2.*b_2)) + B_2.*(sin(2.*b_2)))./((exp(2.*a_2)) + (g1_2.^2 + h1_2.^2).*(g2_2.^2 + h2_2.^2).*(exp(-2.*a_2)) + C_2.*(cos(2.*b_2)) + D_2.*(sin(2.*b_2))) - R_2; ...
((g1_3.^2 + h1_3.^2).*(exp(2.*a_3)) + (g2_3.^2 + h2_3.^2).*(exp(-2.*a_3)) + A_3.*(cos(2.*b_3)) + B_3.*(sin(2.*b_3)))./((exp(2.*a_3)) + (g1_3.^2 + h1_3.^2).*(g2_3.^2 + h2_3.^2).*(exp(-2.*a_3)) + C_3.*(cos(2.*b_3)) + D_3.*(sin(2.*b_3))) - R_3];
nkd = fsolve(F, Guess);
Thanks everyone!

Walter Roberson on 24 Feb 2012
Your Guess has 2 elements, suggesting you are wanting to work with two variables, but your F is @(x,y,z) suggesting you want to work with three variables.
fsolve() is going to pass one variable to the function handle. That one variable will be a vector the same length as Guess.
It appears to me that your Guess should have three elements, and it appears to me that your function handle should be:
F = @(X) double( subs( [((g1_1.^2 + h1_1.^2).*(exp(2.*a_1)) + (g2_1.^2 + h2_1.^2).*(exp(-2.*a_1)) + A_1.*(cos(2.*b_1)) + B_1.*(sin(2.*b_1)))./((exp(2.*a_1)) + (g1_1.^2 + h1_1.^2).*(g2_1.^2 + h2_1.^2).*(exp(-2.*a_1)) + C_1.*(cos(2.*b_1)) + D_1.*(sin(2.*b_1))) - R_1; ...
((g1_2.^2 + h1_2.^2).*(exp(2.*a_2)) + (g2_2.^2 + h2_2.^2).*(exp(-2.*a_2)) + A_2.*(cos(2.*b_2)) + B_2.*(sin(2.*b_2)))./((exp(2.*a_2)) + (g1_2.^2 + h1_2.^2).*(g2_2.^2 + h2_2.^2).*(exp(-2.*a_2)) + C_2.*(cos(2.*b_2)) + D_2.*(sin(2.*b_2))) - R_2; ...
((g1_3.^2 + h1_3.^2).*(exp(2.*a_3)) + (g2_3.^2 + h2_3.^2).*(exp(-2.*a_3)) + A_3.*(cos(2.*b_3)) + B_3.*(sin(2.*b_3)))./((exp(2.*a_3)) + (g1_3.^2 + h1_3.^2).*(g2_3.^2 + h2_3.^2).*(exp(-2.*a_3)) + C_3.*(cos(2.*b_3)) + D_3.*(sin(2.*b_3))) - R_3], {x y z}, {X(1) X(2) X(3)}) );
It would not be typical to use symbolic expressions with fsolve() !
Walter Roberson on 24 Feb 2012
Ah, with your spacing I read the 0,25 as 0.25 . 0 is fine as a guess, but it would be easier to read as 0, 25