Está en la página 1de 6

gradte.

% Este programa sirve para resolver un sistema integrado con n


% ecuaciones simultaneas, que tienen n incognitas, con el metodo
% de Gradiente Conjugado. Para esto, el programa utiliza la herramienta
% fminunc del Matlab. Las ecuaciones simultaneas que se van a resolver
% y el vector del gradiente se incluyen en la subrutina funcion.
x0(1)=input('Valor inicial de x(1) =>')
x0(2)=input('Valor inicial de x(2) =>')
x0(3)=input('Valor inicial de x(3) =>')
options=optimset('GradObj', 'on', 'TolFun', 1.0e-05, 'TolX', 1.0e-05,
'Diagnostics', 'on', 'Algorithm','trust-region')
[x,fval]=fminunc(@funcion, x0, options)

funcion.m

function [fg,grd]=funcion(x)
% EN ESTA SUBRUTINA SE PONEN LA FUNCION QUE SE VA A OPTIMZAR, fg,
% Y EL GRADIENTE, grd. EL CASO QUE ESTA MONTADO CORRESPONDE AL
% PROBLEMA 2-A, DEL JUEGO DE EJERCICIOS 10.2, DEL LIBRO ANALISIS
% NUMERICO DE BURDEN Y FAIRES, EDICION N0. 9.
%
f1=3.0*x(1)-cos(x(2)*x(3))-1.0/2.0;
f2=4.0*x(1)^2-625.0*x(2)^2+2.0*x(2)-1.0;
f3=exp(-x(1)*x(2))+20.0*x(3)+(10.0*pi-3.0)/3.0;
fg=f1^2+f2^2+f3^2;
grd(1)=2.0*f1*3.0+2.0*f2*8.0*x(1)+2.0*f3*(-x(2)*exp(-x(1)*x(2)));
grd(2)=2.0*f1*x(3)*sin(x(2)*x(3))+2.0*f2*(-1250.0*x(2)+2.0)+2.0* ...
f3*(-x(1)*exp(-x(1)*x(2)));
grd(3)=2.0*f1*x(2)*sin(x(2)*x(3))+2.0*f3*20.0;
end

32
Funciones a las que se les desea sacar sus raices:

1
f1 x1 , x 2 , x 3   3x1  cosx 2 x 3   0
2
f 2 x1 , x 2 , x 3   4x12  625x 2 2  2x 2  1  0
10π  3
f 3 x1 , x 2 , x 3   e  x 1 x 2  20x3  0
3

Función objetivo que se desea optimizar:

 
2
 1
gx1 , x 2 , x 3    3x1  cosx 2 x 3     4x12  625x 2 2  2x 2  1 
2

 2
2
  x1x 2 10π  3 
e  20x3  
 3 

quedando finalmente:

gx1, x 2 , x 3   f1 x1, x 2 , x 3 2  f 2 x1, x 2 , x 3 2  f 3 x1, x 2 , x 3 2

33
gradiente de g(x1,x2,x3):

  1 
2
  13x  cos  x x
2 3    3   
2
 

 2 2

gx1, x 2 , x 3   2 4x1  625x 2  2x 2  1 8x1   ax1 


2 e  x 1 x 2  20x 
  3
10π 
3 
3



x x 
  x 2e 1 2 
 
  1 
2 
  1 3x  cos  x x
2 3     x 3sen  x x
2 3   
2
 

 2 2

2 4x1  625x 2  2x 2  1  1250x2  2   ax 2 

 

2 e  x1 x 2  20x 
 3
10π 
3 
3

 
 x1e  x1 x 2 

 
  1 
2 
  1 3x  cos  x x
2 3     x 2 sen  x x
2 3    
2
  ax 3
   x1x 2 10π  3  
2
   e  20x 3   20  
 3  

34
Quedando finalmente:

2f1 x1 , x 2 , x 3 3  2f 2 x1 , x 2 , x 3 8x1   


gx1 , x 2 , x 3     ax1 

2f 3 x1 , x 2 , x 3   x 2e 
 x1x 2

2f1 x1 , x 2 , x 3 x 3sen x 2 x 3   
 
2f 
 2 1 2 3 x , x , x   1250x 2  2    ax 2 

2f 
 3 1 2 3x , x , x   x1e 
 x1x 2 

2f1 x1 , x 2 , x 3 x 2sen x 2 x 3   
2 f x , x , x 20  ax 3
 3 1 2 3 

>> clear
>> gradte
Valor inicial de x(1) =>0.5

x0 =

0.5000

Valor inicial de x(2) =>0.05

x0 =

0.5000 0.0500

Valor inicial de x(3) =>-0.05

x0 =

0.5000 0.0500 -0.0500

options =

Display: []
MaxFunEvals: []
MaxIter: []
TolFun: 1.0000e-005
TolX: 1.0000e-005
FunValCheck: []
OutputFcn: []
PlotFcns: []

35
ActiveConstrTol: []
Algorithm: []
AlwaysHonorConstraints: []
BranchStrategy: []
DerivativeCheck: []
Diagnostics: 'on'
DiffMaxChange: []
DiffMinChange: []
FinDiffType: []
GoalsExactAchieve: []
GradConstr: []
GradObj: 'on'
HessFcn: []
Hessian: []
HessMult: []
HessPattern: []
HessUpdate: []
InitialHessType: []
InitialHessMatrix: []
InitBarrierParam: []
InitTrustRegionRadius: []
Jacobian: []
JacobMult: []
JacobPattern: []
LargeScale: []
LevenbergMarquardt: []
LineSearchType: []
MaxNodes: []
MaxPCGIter: []
MaxProjCGIter: []
MaxRLPIter: []
MaxSQPIter: []
MaxTime: []
MeritFunction: []
MinAbsMax: []
NodeDisplayInterval: []
NodeSearchStrategy: []
NonlEqnAlgorithm: []
NoStopIfFlatInfeas: []
ObjectiveLimit: []
PhaseOneTotalScaling: []
Preconditioner: []
PrecondBandWidth: []
RelLineSrchBnd: []
RelLineSrchBndDuration: []
ScaleProblem: []
ShowStatusWindow: []
Simplex: []
SubproblemAlgorithm: []
TolCon: []
TolConSQP: []
TolGradCon: []
TolPCG: []
TolProjCG: []
TolProjCGAbs: []
TolRLPFun: []
TolXInteger: []

36
TypicalX: []
UseParallel: []

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Diagnostic Information

Number of variables: 3

Functions
Objective and gradient: funcion
Hessian: finite-differencing (or Quasi-Newton)

Algorithm selected
large-scale: trust-region Newton

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
End diagnostic information

Optimization terminated: relative function value changing by less than


OPTIONS.TolFun.

x =

0.5004 0.0042 -0.5235

fval =

3.1682e-006

37

También podría gustarte