Question about writing if statement on function nlcon()

function [c, ceq] = nlcon(x)
c = [];
Pi_ss_nl = 1.005;
beta_d = 0.9963;
beta_l = 0.9901;
i_l_ss_nl = Pi_ss_nl/beta_l - 1;
i_d_ss_nl = Pi_ss_nl/beta_d - 1;
i_r_ss_nl = i_d_ss_nl;
iota = 0.88;
nu = 6;
gamma = 0.0001;
R_bar = 0.07;
m_bar = 0.01;
% if x(2) < R_bar || x(3) < m_bar
% ceq = x(4) - ((i_l_ss_nl - i_d_ss_nl)/(1+i_d_ss_nl))*x(1) + ((i_d_ss_nl - i_r_ss_nl)/(1+i_d_ss_nl))*x(2) +...
% ((i_d_ss_nl + gamma)/(1+i_d_ss_nl))*x(3) + (x(1)^nu)*(x(4)^-iota) + 1/2*(x(2) - R_bar)^2 + 1/2*(x(3) - m_bar)^2;
% elseif x(2) >= R_bar || x(3) < m_bar
% ceq = x(4) - ((i_l_ss_nl - i_d_ss_nl)/(1+i_d_ss_nl))*x(1) + ((i_d_ss_nl - i_r_ss_nl)/(1+i_d_ss_nl))*x(2) +...
% ((i_d_ss_nl + gamma)/(1+i_d_ss_nl))*x(3) + (x(1)^nu)*(x(4)^-iota) + 1/2*(x(3) - m_bar)^2;
% elseif x(2) < R_bar || x(3) >= m_bar
% ceq = x(4) - ((i_l_ss_nl - i_d_ss_nl)/(1+i_d_ss_nl))*x(1) + ((i_d_ss_nl - i_r_ss_nl)/(1+i_d_ss_nl))*x(2) +...
% ((i_d_ss_nl + gamma)/(1+i_d_ss_nl))*x(3) + (x(1)^nu)*(x(4)^-iota) + 1/2*(x(2) - R_bar)^2;
% elseif x(2) >= R_bar || x(3) >= m_bar
% ceq = x(4) - ((i_l_ss_nl - i_d_ss_nl)/(1+i_d_ss_nl))*x(1) + ((i_d_ss_nl - i_r_ss_nl)/(1+i_d_ss_nl))*x(2) +...
% ((i_d_ss_nl + gamma)/(1+i_d_ss_nl))*x(3) + (x(1)^nu)*(x(4)^-iota);
% end
end
What I want is ceq to be different for 4 scenarios.
Is there a way I can do this?
How do I write if statement on nlcon()?
Any help would really be appreciated!

답변 (1개)

Anmol Dhiman
Anmol Dhiman 2021년 1월 7일
Hi Hyunmin,
In my opinion the problem with the above if conditions is the fact that you are using OR(||) instead of AND(&&) in the condition of the if statement.
for eg, A||B is true if any of A , B is true. In your case, both the statements will come as true if "x(3) < m_bar" is statisfied. Hence the first if condtion is nver executed.
x(2) < R_bar || x(3) < m_bar
x(2) >= R_bar || x(3) < m_bar
Try using && instead of ||.
Hope it Helps

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2021년 1월 4일

답변:

2021년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by