Untitled
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Use the following first guess values x0 = [1; 1; 0; 1; 1]; % [x y z lambda1 lambda2] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Use the following options for the solver options = optimoptions("fsolve", "MaxFunctionEvaluations", 10000, "MaxIterations", 10000); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% fun=fsolve(@kkt_system,x0,options); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Test if the function is written % correctly by saving its output inside % the following variables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% F_test = kkt_system(x0); x_test =fun(1); y_test =fun(2) ; z_test =fun(3); lambda1_test =(4); lambda2_test =(5); fprintf('x= %.1f \n', fun(1)); fprintf('y= %.1f \n', fun(2)); fprintf('z= %.1f \n', fun(3)); fprintf('lambda1= %.1f \n', fun(4)); fprintf('lambda2= %.1f \n', fun(5)); % Save the solution inside the following variables x_opt = ; y_opt = ; z_opt = ; lambda1_opt = ; lambda2_opt = ; function F = kkt_system(var) x=var(1); y=var(2); z=var(3); lambda1=var(4); lambda2=var(5); F=(var(1))^2+2*(var(2))^2+3*(var(3))^2-var(4)*(var(1)+var(2)+var(3)-2)-var(5)*(var(1)*var(2)+var(2)*var(3)-1); end function I=equazioni_Lagrange(w) x=w(1); y=w(2); z=w(3); lambda=w(4); lambda=w(5);
Leave a Comment