clear; clc; close all; f=@fun2C; %Funcion a optimizar %Geometrico opt=optimset('Display','iter','MaxIter',40); %Trust-Region opt1 = optimoptions('fminunc','Display','iter','MaxIter',40,'Algorithm','trust-region','SpecifyObjectiveGradient',true); %Quasi-Newton opt2 = optimoptions('fminunc','Display','iter','MaxIter',40,'Algorithm','quasi-newton','SpecifyObjectiveGradient',true); %Optimizacion x_unc = fminunc(f,x0,opt1); disp(x_unc); %--Geometrico x_search = fminsearch(f,x0,opt); disp(x_search); function [y,yd]=fun2C(x) %Funcion de la practica 2C para 3 variables x1 = x(1); x2 = x(2); x3 = x(3); y = 2500*(sqrt(4+x1.^2))+1200*(sqrt(1+(x2-x1).^2))+2000*(sqrt(0.5^2+(x3-x2).^2))+1000*(10-x3); if nargout>1 yd(1,1) =(600.*(2.*x1 - 2.*x2))./((x1 - x2).^2 + 1).^(1/2) + (2500.*x1)./(x1.^2 + 4).^(1/2); yd(2,1) = (1000.*(2.*x2 - 2.*x3))./((x2 - x3).^2 + 1/4).^(1/2) - (600.*(2.*x1 - 2.*x2))./((x1 - x2).^2 + 1).^(1/2); yd(3,1) = -(1000.*(2.*x2 - 2.*x3))./((x2 - x3).^2 + 1/4).^(1/2) - 1000; end end function [y,yd]=fun2C_1(x) %Funcion de la practica 2C para 2 variables x1 = x(1); x2 = x(2); y= 3000*(sqrt(4+x1.^2))+2000*(sqrt(1+(x2-x1).^2))+1000*(10-x2); if nargout>1 yd(1,1)=(1000.*(2.*x1 - 2.*x2))./((x1 - x2).^2 + 1).^(1/2) + (3000.*x1)./(x1.^2 + 4).^(1/2); yd(2,1)= -(1000.*(2.*x1 - 2.*x2))./((x1 - x2).^2 + 1).^(1/2) - 1000; end end