MATLAB assignment Math 222, Fall 2011 ROOT FINDING AND OPTIMIZATION!!! Part I: The Mathematical Prelude So the goal of this part of the assignment is to approximate solutions to nonlinear systems of equations and approximate solutions to optimization problems. We start with an example that we can solve by hand. Find the absolute maximum and the absolute minimum of the function subject to the constraint f(x, y) = 3x + 4y g(x, y) = x 2 + y 2 = 25. Attacking this problem with Lagrange multipliers leads to something like this: { 3 = λ2x f = λ g 4 = λ2y. Together with our constraint equation (x 2 +y 2 = 25), we have three equations for our three unknowns: x, y, and λ. Now proceeding without the use of MATLAB, we observe that neither x nor y can equal zero, so we can divide the first equation above by x and the second by y to get to: 3 x = 2λ = 4 y. Cross-multiplying and ignoring λ henceforth we arrive at the equations: or if you prefer 3y = 4x x 2 + y 2 = 25, 3y 4x = 0 x 2 + y 2 25 = 0.
Now we can crank through these equations without too much difficulty (I hope), to get two possibilities: Either x = 3 & y = 4, or x = 3 & y = 4. Plugging into our original function f(x, y) we get f(3, 4) = 25 and f( 3, 4) = 25. The first case gives us our absolute maximum, and the second case gives us our absolute minimum. OK, that is fine, but it was a somewhat contrived example. Let s try a very similar but slightly less contrived example. Find the absolute maximum and the absolute minimum of the function subject to the constraint f(x, y) = 3x + 4y + 0.001e x g(x, y) = x 2 + y 2 = 25. Now intuitively, since we have taken a teeny tiny function (namely 0.001e x ), and added to what we were optimizing before, we expect that the new max and min will be close to the old max and min. On the other hand, if we wish to find the exact solutions the algebra is now intractable. Ignoring my pessimism, let us see what happens. Lagrange multipliers leads to something like this: { 3 + 0.001e x = λ2x f = λ g 4 = λ2y. So, by solving for 2λ in each equation and equating the result, we get to: 3 + 0.001e x = 4 x y.
A little bit of algebraic manipulation (multiply by xy and subtract 4x from both sides), together with the constraint equation and now we have the system of equations: 3y + 0.001ye x 4x = 0 x 2 + y 2 25 = 0. Now in order to solve this system, we need something with some computing power. Part II: The Rootfinding Approach Using fsolve We turn to our first and most straightforward approach to solving the system above. In my first attack on this problem, I made two m-files. My first m-file was called myfun.m and its contents were: function F = myfun(w) F = [3 w(2) +.001 w(2) exp(w(1))-4 w(1), w(1) 2 + w(2) 2-25]; Now as I stated above, this file was named myfun.m and you will notice that on the first line I wrote [blah blah] myfun(w). Now if I had named the file cheeseburger.m, then on the first line where it says myfun(w) I would need it to say cheeseburger(w). In terms of what this m-file does, I guess it may help to figure out what is going on by typing in the main window: myfun([3,4]) Now hopefully you will see something like this: ans =
0.080342147692750 0 (If you saw an error message like this: Undefined function myfun for input arguments of type double, then you need to make sure that the window that has the current folder is set to the place where your myfun.m file is located.) So... What is going on? Well, you are feeding myfun the vector [3, 4], and so within the function it is setting w = [3,4], and more specifically, it is letting w(1) = 3, and w(2) = 4. After that it computes the two pieces of what it is calling F, and it is then spitting these out for you. In particular, you will notice that w(1) 2 + w(2) 2 25 = 3 2 + 4 2 25 = 9 + 16 25 = 0, and that is indeed exactly what MATLAB gave you for the second component of F. Now if you compare myfun with the system of equations that you found at the end of Part I, then you will hopefully recognize that solving that system, amounts to finding a vector (probably close to [3,4] or [-3, -4]) which makes myfun spit out 0, 0. Well, now it is time to get MATLAB to do some root finding for us. I wrote a file named examplewfsolve.m and it contents are: w0 = [3,4]; [w,fval] = fsolve(@myfun,w0); w w0 = [-3,-4]; [w,fval] = fsolve(@myfun,w0); w So... what is going on is MATLAB looks for a point near w0 which makes myfun equal to zero, and, indeed, it finds a point very close to [3,4] and then it finds a point very close to [-3,-4] which do indeed make myfun = [0, 0].
Assignment! Problem 1: Change the function f(x, y) by altering the tiny function 0.001e x to a different (but also tiny) nonpolynomial function of x and y. Use MATLAB to solve the corresponding Lagrange Multiplier problem by altering my function myfun above. Submit the code and the output. Part III: The Optimization Approach Using fminsearch Another way to try to get a solution to the system: 3y + 0.001ye x 4x = 0 x 2 + y 2 25 = 0 which has the benefit of forcing you to learn about minimization is to try to find minimums of the function created by adding up the squares of the left hand sides. In other words, let Θ(x, y) := (3y + 0.001ye x 4x) 2 + (x 2 + y 2 25) 2 and then let MATLAB hunt for a minimum of Θ using fminsearch. To this end I wrote the following code: I wrote a file named Cartman.m with the contents: function F = Cartman(y) F = ((3 y(2) +.001 y(2) exp(y(1)) - 4 y(1)) 2 + (y(1) 2 + y(2) 2-25) 2); and then I wrote a file named examplewfminsearch.m with the contents: y0 = [3,4]; [y,fval] = fminsearch(@cartman,y0,optimset( TolX,1e-15)); y Cartman(y)
y0 = [-3,-4]; [y,fval] = fminsearch(@cartman,y0,optimset( TolX,1e-15)); y Cartman(y) I guess it is worth mentioning that the part involving optimset is just used to tell MATLAB to work harder to get really close to the minimum, and by plugging the values I find into the function Cartman I can see that I do indeed get a number which is incredibly close to zero. Assignment! Problem 2: Alter the definition of Cartman above so that fminsearch can be used to find the roots of the same Lagrange Multiplier problem that you solved in Problem 1. Submit the code and the output.