MATLAB

MATLAB for MATH241H - Section 0301 - Fall 2017


MATLAB Project 2 Due: W 10/25

The following material has been adapted and copied with permission from material created by Justin Wyss-Gallifent.

The basic format of this guide is the same as the first one in the sense that the Guide/Project has various Tasks interspersed within. The project involves creating a script m-file and put the tasks in order into that file, then publishing that file using MATLAB's publish to HTML feature and printing the resulting document. As in the last assignment, you should separate each task into cells with the title of the task preceded by a %%.


Partial Derivatives

Since we can tell Matlab which variable to take the derivative with respect to, finding partial derivatives is as easy as regular derivatives. Here’s an example of a partial derivative with respect to $y$:

syms x y
diff(x^2*y+y^2/x,y)
ans =

(2*y)/x + x^2
Task 1: Find $\frac{\partial}{\partial y}\left[\frac{\sin({x^2y})}{y^2}\right]$

Here’s an example of a second partial derivative: First finding the derivative with respect to $y$ and then $x$:

diff(diff(x^2*exp(x*y^2),y),x)
ans =

2*x^3*y^3*exp(x*y^2) + 6*x^2*y*exp(x*y^2)
Task 2: Find $\frac{\partial^2}{\partial y^2}\left[\frac{x^3 + y^2}{x-y}\right]$

Matlab does have a gradient command but it gives numerical approximations, not what we want. Instead we want a specific manifestation of the Matlab Jacobian command. Specifically for a function of two variables we do the following. What this really does is take a bunch of derivatives with respect to the letters given and product the output we desire. This is how we find $\nabla f$ for $f(x,y) = x^2 y^3$:

jacobian(x^2*y^3,[x y])
ans =

[ 2*x*y^3, 3*x^2*y^2]
Task 3: Find $\nabla f$ for $f(x,y) = \frac{xy}{x^2y + y}$.

Here’s the same gradient with values plugged in for x and y. Note the use of the familiar subs command but with {x,y} to substitute for both values:

subs(jacobian(x^2*y^3,[x y]),{x,y},{-1,3})
ans =

[ -54, 27]
Task 4: Find $\nabla f(-1,2)$ for $f(x,y) = 5x^3y^2 - \frac{x}{y}$.

Here’s one with three variables, I only syms z because x and y are still active from earlier in the guide.

syms z;
jacobian(x*exp(x*y)/z,[x y z])
ans =

[ exp(x*y)/z + (x*y*exp(x*y))/z, (x^2*exp(x*y))/z, -(x*exp(x*y))/z^2]

Directional Derivatives

We learned in class that the directional derivative is the dot product of the unit vector with the gradient. Consider the following compound command. Really think about what it all does:

a=[1 2];
dot((a/norm(a)),subs(jacobian(x*y^2-1/y,[x y]),{x,y},{-2,3}))
ans =

-(133*5^(1/2))/45

You (hopefully!) guessed it! This is the directional derivative of $f(x,y) = xy^2 − \frac{1}{y}$ in the direction of $\mathbf{a} = 1\mathbf{i} + 2\mathbf{j}$ at the point $(−2, 3)$. We make the vector $\mathbf{a}$ a unit vector and dot it with the gradient.

Task 5: Find the directional derivative of $g(x,y) = x^2 + y^3$ at $(1, −2)$ in the direction of $\mathbf{a} = 3\mathbf{i} − 7\mathbf{j}$.

Critical Points

We know how to solve a single equation equal to zero in Matlab:

syms x
solve(x^2+2*x==0)
ans =

-2
 0

We can solve a system of equations by simply stuffing the two equations into a vector. Since the output is a vector we need to assign it to make it legible. If that confuses you don’t worry, just do it. Here is how to simultaneously solve $xy − 3 = 0$ and $x − y − 2 = 0$:

[xsoln,ysoln]=solve([x*y-3==0,x-y-2==0])
xsoln =

-1
 3

ysoln =

-3
 1

Be aware that the first vector xsoln is giving you the two possible $x$ values. These pair up with the two possible y values. Thus in friendlier terms the two solutions are $(3, 1)$ and $(−1, −3)$. Also note that the solve command returns the solution in alphabetical order, meaning it returns $x$ and then $y$. This is why we assign the solution to [xsoln,ysoln]=.

Now then when we look for critical points we’re setting both partial derivatives equal to $0$. This is the same as setting the gradient equal to the $0$ vector. Here’s example 4 from page 878 in the book:

f(x)=x^2-2*x*y+1/3*y^3-3*y;
[xsoln,ysoln]=solve(jacobian(f,[x y]))
xsoln =

-1
 3

ysoln =

-1
 3
Task 6: Find all critical points for $f(x,y) = (y − 2) \ln(xy)$. Remember that $\ln$ in Matlab is log. On your printout write the points as coordinate pairs next to the output.
Task 7: Find all critical points for $f(x,y) = x^3 + y^3 − 6xy$. On your printout write the points as coordinate pairs next to the output.

Lagrange Multipliers

When we solve a problem using Lagrange multipliers what we’re doing is solving $\nabla f = L\nabla g$ along with the constraint. The first of these is the same as solving $\nabla f − L\nabla g = 0$ and the constraint can be rewritten to equal $0$ too. The tricky thing is solving them all at once. This is how it’s done for finding the extreme values for $f(x, y) = 3x^2 + 2y^2 − 4y + 1$ subject to $x^2 + y^2 = 16$. Well first here we'll just solve the equations:

clear all;
syms x y L;
f(x,y)=3*x^2+2*y^2-4*y+1;
g(x,y)=x^2+y^2-16;
firstpart=jacobian(f,[x y])-L*jacobian(g,[x y]);
[Lsoln,xsoln,ysoln]=solve([firstpart,g])
Lsoln =

  3
  3
3/2
5/2

xsoln =

-2*3^(1/2)
 2*3^(1/2)
         0
         0

ysoln =

-2
-2
 4
-4

Again note that the values group together. For example $L = 3/2$ corresponds to the point $(0, 4)$ and so on through all four. Also again note the order [Lsoln,xsoln,ysoln] because solve returns the solutions to $L$, $x$ and $y$ in alphabetical order. Even better we can plug xsoln and ysoln into f at the end:

subs(f(x,y),{x,y},{xsoln,ysoln})
ans =

53
53
17
49

and simply pick off the largest and smallest. In this case the maximum is $53$ occuring at both $(2(3^{1/2}), −2)$ and $(−2(3^{1/2}), −2)$ and the minimum is $17$ occuring at $(0, 4)$.

Task 8: Use Lagrange multipliers to find the maximum and minimum values of $f(x,y) = xy^2$ subject to the constraint $x^2 + y^2 = 16$. On your printout write a neat summary next to the output.