Adding input parameters to a function

9 views (last 30 days)
Noor Ahmad
Noor Ahmad on 14 Mar 2017
Commented: Walter Roberson on 15 Mar 2017
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)

Answers (1)

Walter Roberson
Walter Roberson on 14 Mar 2017
  2 Comments
Noor Ahmad
Noor Ahmad on 15 Mar 2017
Edited: Walter Roberson on 15 Mar 2017
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
Walter Roberson
Walter Roberson on 15 Mar 2017
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

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!