# Question about writing if statement on function nlcon()

2 views (last 30 days)
Hyunmin Jung on 4 Jan 2021
Answered: Anmol Dhiman on 7 Jan 2021
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!

Anmol Dhiman on 7 Jan 2021
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