Page 363 - Applied Numerical Methods Using MATLAB
P. 363
352 OPTIMIZATION
the minimization problem
N
2
Min f (x) (7.3.2)
n
n=1
The routine needs at least the vector or matrix function f(x) and the initial guess
x 0 as its first and second input arguments, where the components of f(x) =
T
[f 1 (x) ·· · f N (x)] are squared, summed, and then minimized over x.Inorder to
learn the usage and function of this routine, we made the MATLAB program
“nm731_2.m”, which uses it to find a second-degree polynomial approximating
the function
1
y = f(x) = (7.3.3)
1 + 8x 2
For verification, the result of using the NLLS routine “lsqnonlin()” is compared
with that obtained from directly applying the routine “polyfits()” introduced
in Section 3.8.2.
>> nm731_2
ao_lsq = [-0.1631 -0.0000 0.4653], ao_fit = [-0.1631 -0.0000 0.4653]
%nm731_2 try using lsqnonlin() for a vector-valued objective ftn F(x)
clear, clf
N = 3; a0 = zeros(1,N); %the initial guess of polynomial coefficient vector
ao_lsq = lsqnonlin(’f731_2’,a0) %parameter estimate by lsqnonlin()
xx = -2+[0:400]/50; fx = 1./(1+8*xx.*xx);
ao_fit = polyfits(xx,fx,N - 1) %parameter estimate by polyfits()
function F = f731_2(a)
%error between the polynomial a(x) and f(x) = 1/(1+8x^2)
xx = -2 +[0:200]/50; F = polyval(a,xx) - 1./(1+8*xx.*xx);
7.3.2 Constrained Optimization
Generally, constrained optimization is very complicated and difficult to deal with.
So we will not cover the topic in details here and instead, will just introduce the
powerful MATLAB built-in routine “fmincon()”, which makes us relieved from
a big headache.
This routine is well-designed for attacking the optimization problems subject
to some constraints:
function [c,ceq] = f722c(x)
c = [-x(1); -x(2); 3*x(1) - x(1)*x(2) + 4*x(2)- 7;
2*x(1)+ x(2)- 3; 3*x(1)- 4*x(2)^2 - 4*x(2)]; %inequality constraints
ceq = []; %equality constraints