# Writing complicated equations for problem-based optimization

22 views (last 30 days)
Brian Green on 10 Apr 2021 at 16:52
Answered: Alan Weiss on 13 Apr 2021 at 12:20
I'm trying to solve a system of 16 non-linear equations, but don't know how to properly setup the optimization problem in Matlab.
While there are 16 variables that I am optimizing (a vector T, with entries i and scalar p), there are also intermediate parameters that are functions of the choice variables and model parameters (which are given).    variables and w are already model parameters. The actual equations to optimize are:   The code I have so far is
%Initial Variables
w = [0 2000 4000 6000 8000 10000 12500 15000 17500 20000 25000 30000 50000 100000];
w_dis_init = [.142 .033 .027 .028 .03 .048 .052 .065 .047 .082 .098 .164 .145 .039];
t_init = .4;
nit = 6000;
H = 5000;
v = 1;
%Elasticities
int_elast = .25*ones(1,length(w)); %zeta
ext_elast = zeros(1,length(w)); %eta
for(i=1:length(w))
if(w(i)<= 20000)
ext_elast(i) = .5
end
end
%Constructed Variables
T_init = (1-t_init)*w - nit;
c_init = w - T_init;
%Optimization Variables
T = optimvar('T',1,length(w));
p = optimvar('p',1);
c = zeros(1,length(w));
g = zeros(1,length(w));
h = w_dis_init;
%Optimization Equations
eq_c = c == (w-T);
eq_g = g == (p*c).^(-1);
eq_h = h == w_dis_init.*(c-c_init)/(c_init-c_init(1));
eq_hg = g.*h == 1;
eq_ht = h.*T == H;
I don't know if equations c,g, and h are how to properly code these laws of motion in MATLAB; I also don't know how to code the 14 equations governing T - my current best guess is to use a for loop to write out the 14 equations "manually" but I'm not sure.

Alan Weiss on 13 Apr 2021 at 12:20
I think that you need to keep clear in your mind (and code) what is an optimization variable and what is an optimization expression. I am not sure that I understand all of your equations, but your code does not seem to match the equations. I'd change the code like this:
%Initial Variables
w = [0 2000 4000 6000 8000 10000 12500 15000 17500 20000 25000 30000 50000 100000];
w_dis_init = [.142 .033 .027 .028 .03 .048 .052 .065 .047 .082 .098 .164 .145 .039];
t_init = .4;
nit = 6000;
H = 5000;
v = 1;
%Elasticities
int_elast = .25*ones(1,length(w)); %zeta
ext_elast = zeros(1,length(w)); %eta
ext_elast(w <= 20000) = 0.5; % no loop needed
%Constructed Variables
T_init = (1-t_init)*w - nit;
c_init = w - T_init;
%Optimization Variables
T = optimvar('T',1,length(w));
p = optimvar('p',1);
% Optimization expressions
% c = zeros(1,length(w));
% g = zeros(1,length(w));
% h = w_dis_init;
%Optimization Expressions and equations
c = (w-T);
g = 1./(p*c);
h = w_dis_init.*(c-c_init)./(c_init-c_init(1));
eq_hg = dot(g,h) == 1;
eq_ht = dot(h,T) == H;
eq_TT = % You might need a loop to specify the T equations
% Equation problem and constraints
prob = eqnproblem;
prob.Equations.eq_hg = eq_hg;
prob.Equations.eq_ht = eq_ht;
% Also need T equations
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

R2021a

### Community Treasure Hunt

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

Start Hunting!