Adding input parameters to a function

조회 수: 9 (최근 30일)
Noor Ahmad
Noor Ahmad 2017년 3월 14일
댓글: Walter Roberson 2017년 3월 15일
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개)

Walter Roberson
Walter Roberson 2017년 3월 14일
  댓글 수: 2
Noor Ahmad
Noor Ahmad 2017년 3월 15일
편집: Walter Roberson 2017년 3월 15일
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 2017년 3월 15일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by