%Load parameter structure
clear all
fig = get(0,'CurrentFigure');
if fig == 666
close(666)
end
%clc
load param.mat
load un0.mat

%Set the needed parameteres not already included in param.mat
param.scale_un = abs(un0) ;
param.output = 'Solve_equation' ;
param.specs = [355 11000 25 0.901 1.1 0.2]' ; %Tr, Mr, L,xB(2), FA, P/D - these are the specifications for initialization.

un0 = un0./param.scale_un ; %Scale the initial values of un, hopefully making 

%Try with fsolve
options_I = optimset('Algorithm','levenberg-marquardt','MaxIter',1e3,'MaxFunEvals',1e7,'Display','iter','TolX',1e-5,'TolFun',1e-5);
[un,fval,exitflag] = fsolve(@reaseprec,un0,options_I,param) ;
un = un.*param.scale_un ;
un0 = un ;
% save un0.mat un0


%Try using fmincon


%Set optimization-specific parameters
param.Trmax = 380 ;
param.Mrmax = 11000 ;
param.Vmax = 30 ;
param.Fmax = 5 ;
param.xfa = [1 0 0]' ;
param.prices = [1 0.01 -2 -0.01]' ;
param.FA = 1.1 ;
param.output = 'Solve_optimizer' ;

un0_opt = un0./param.scale_un ;

options_III = optimset('Algorithm','active-set','Display','iter','TolCon',1e-5,'MaxFunEvals',1e6) ;
[un,fval,exitflag,output,lambda,grad,hessian] = fmincon(@rsr_obj,un0_opt,[],[],[],[],zeros(size(un0_opt)),[],@reaseprec,options_III,param) ;
un = un.*param.scale_un ;
if exitflag > 0
make_output
end