Adding input parameters to a function

So I have a function that uses fsolve to solve for two variables, lmb_1 and lmb_2. There are two other variables that I'd like to plug into the function. something like root2d(x, phi_0,k) so that I don't have to keep changing the phi_0 and k values in the root2d function.
function F = root2d(x)
phi_0=0.2;
k=0.1;
lmb_1=x(1);
lmb_2=x(2);
s_i = x(1);
s_ii = x(2);
expression_i = phi_0*(k - s_i) + k^2/2 - s_i^2/2 - lmb_1*lmb_2*(log(k) - log(s_i));
expression_ii = phi_0*s_ii - phi_0 - s_ii^2/2 + lmb_1*lmb_2*log(s_ii) + 1/2;
F(1) = expression_i-expression_ii;
F(2) = x(1)-x(2)+phi_0;
end
x0 = [1,1];
answer = fsolve(@root2d,x0)

回答(1 个)

2 个评论

So I attempted this but, when I call the function:
answer=Lambda1and2Solver_n_1(0.2,0.2,[1 1])
I get a exceeds matrix error.
function F = Lambda1and2Solver_n_1(phi_0,k,x0)
F = fzero(@Solver,x0);
function F = Solver(x)
phi_0=0.2;
k=0.1;
lmb_1=x(1);
lmb_2=x(2);
s_i = x(1);
s_ii = x(2);
expression_i = phi_0*(k - s_i) + k^2/2 - s_i^2/2 - lmb_1*lmb_2*(log(k) - log(s_i));
expression_ii = phi_0*s_ii - phi_0 - s_ii^2/2 + lmb_1*lmb_2*log(s_ii) + 1/2;
F(1) = expression_i-expression_ii;
F(2) = x(1)-x(2)+phi_0;
end
end
phi_0 = 0.2;
k = 0.1;
x0 = [1 1];
F = fzero(@(x) Lambda1and2Solver_n_1(x, phi_0, k), x0);
function F = Lambda1and2Solver_n_1(x, phi_0, k)
lmb_1=x(1);
lmb_2=x(2);
s_i = x(1);
s_ii = x(2);
expression_i = phi_0*(k - s_i) + k^2/2 - s_i^2/2 - lmb_1*lmb_2*(log(k) - log(s_i));
expression_ii = phi_0*s_ii - phi_0 - s_ii^2/2 + lmb_1*lmb_2*log(s_ii) + 1/2;
F(1) = expression_i-expression_ii;
F(2) = x(1)-x(2)+phi_0;
end

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by