fsolve inputs
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hey all,
I'm trying to pass some equations to fsolve, but I'm getting nothing but errors. I have 3 complex equations, and to simplify input, I broke down the equations into component parts like so:
y_1 = 309;
n0_1 = 1;
n1_1 = x(1);
k1_1 = x(2);
n2_1 = 5.07;
k2_1 = 3.62;
d1_1 = x(3);
R_1 = .4335;
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_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
This equations repeats itself two more times, with different values as constants. The problem I seem to be having is that Matlab doesn't like my definitions of the variables. I don't know how to express this information in a way that Matlab is able to understand, and despite all of the help I've had from the Mathworks community (thanks so much), I can't seem to get this to work. I've tried syms, I've troubleshot syms, I've tried setting up a seperate function file with no results, and I'm stuck at this point. How would you pass this through? I'm sure it would be easy enough to just create the entire system of equations without all of the substitution, and then pass that on to fsolve, but this is much user friendly, and I would like to learn how to make this work.
Thanks!
2 comentarios
Walter Roberson
el 28 de Feb. de 2012
"Matlab doesn't like my definitions of the variables" is not very specific, makes it difficult for us to help you.
Walter Roberson
el 28 de Feb. de 2012
Duplicate is at http://www.mathworks.com/matlabcentral/answers/30540-passing-data-to-fsolve
Respuesta aceptada
Walter Roberson
el 28 de Feb. de 2012
I cannot test this as I do not have the toolbox with fsolve in it.
This is the structure I told you to adapt several threads ago.
function nkd = g3driver
Guess = [1.58, 0, 25];
nkd = fsolve(@g3, Guess);
end
function F = g3(X)
% This program aims to back-solve for reflectance (R) using given values
% input by hand.
x = X(1); y = X(2); z = X(3);
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);
F = [((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];
end
0 comentarios
Más respuestas (1)
Walter Roberson
el 28 de Feb. de 2012
Your line that starts g1_1.^2 is missing an opening '(', and has an extra ')' before the './'
4 comentarios
Walter Roberson
el 28 de Feb. de 2012
That still leaves the extra ')' before the './' .
When I put in the needed '(' and remove the unneeded ')', I have no problems with the code when I initialize x = rand(3,1)
Your code shown is not doing any substitution.
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!