The fsolve( ) takes function with parameters of the form x(1),x(2).. I have quite long equations involving unknowns x and y. How can I give my equations as input?
MATLAB: Fsolve input
fsolve
Related Solutions
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);endfunction 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
fun = @(x,y) [x+y^2-5, 6*x^2+13*y-5]
Since fsolve requires the function you pass into it to accept one input, a vector, you need an adapter.
g = @(xy) fun(xy(1), xy(2));
This has the interface fsolve requires and calls fun the way it expects to be called.
g([1 2]) % [0 27]
fun(1, 2) % [0 27]
Best Answer