Linear and Quadratic Approximations in Maple
Math 2130 Spring 2011
This worksheet contains code for several functions which compute
multivariate Taylor polynomials. Alternatively you can just use the
built in command "TaylorApproximation" (shown below).
Maple worksheet: math2130-spring2011-quadratic_approximations.mw
Below the code are the solutions for (most of) the problems found
in our "Quadratic Approximations" supplement.
"Edit → Execute → Worksheet" to make Maple show the command outputs.
> | restart;
with(VectorCalculus): evalSF := proc(f,X,A) # Computes f with X = A (a scalar valued function version of evalVF). subs([seq(convert(X,list)[i]=convert(A,list)[i],i=1..nops(convert(X,list)))],f); end proc: LinearApproximation := proc(f,X,A) # Computes the linear approximation (1st order Taylor polynomial) # of f based at X = A. # # f is an expression (not a function) with variables X. # X is a vector which lists the variables on which f depends. # A is a vector. local f_A, delf_A; # computes f(A) f_A := evalSF(f,X,A); # the gradient of f evaluated at X = A. delf_A := evalVF(Gradient(f,convert(X,list)),A); # Return the Linear Approximation of f at X = A. simplify(f_A + delf_A.(X-A)); end proc: QuadraticApproximation := proc(f,X,A) # Computes the quadratic approximation (2nd order Taylor polynomial) # of f based at X = A. # # f is an expression (not a function) with variables X. # X is a vector which lists the variables on which f depends. # A is a vector. local f_A, delf_A, Hf_A; # computes f(A) f_A := evalSF(f,X,A); # the gradient of f evaluated at X = A. delf_A := evalVF(Gradient(f,convert(X,list)),A); # the Hessian of f evaluated at X = A. Hf_A := evalSF(Hessian(f,convert(X,list)),X,A); # Return the Quadratic Approximation of f at X = A. simplify(f_A + delf_A.(X-A)+((1/2)*LinearAlgebra[Transpose](convert(X-A,Matrix)).Hf_A.convert(X-A,Matrix))[1,1]); end proc: |
Exercise #1: at
> | X := <x,y>;
A := <0,0>; f := exp(x+2*y); L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(1) |
Exercise #2: at
> | X := <x,y>;
A := <2,3>; f := x^2+x*y^2; L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(2) |
Exercise #3: at
> | X := <x,y>;
A := <-1,0>; f := x^4*y^2+x*y-2*y; L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(3) |
Exercise #4: at
> | X := <x,y>;
A := <2,0>; f := 2*x^2+3*y^4+exp(x*y^2); L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(4) |
Exercise #5: at
> | X := <x,y>;
A := <3,Pi>; f := sin(x*y); L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(5) |
Exercise #6: at
> | X := <x,y>;
A := <1,0>; f := ln(x^2+y); L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(6) |
Exercise #7: at
> | X := <x,y>;
A := <sqrt(Pi),sqrt(Pi)>; f := cos(x*y); L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(7) |
Exercise #8: at
> | X := <x,y,z>;
A := <1,2,3>; f := x*y*z; L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(8) |
Exercise #9: at
> | X := <x,y,z>;
A := <-1,0,1>; f := x^4+y^4+z^4; L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(9) |
Exercise #10: at
> | X := <x,y,z>;
A := <2,1,-2>; f := x^2*z+x*y^2+y^3*z^2+exp(x*y+z); L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(10) |
Exercise #11: at
> | X := <w,x,y,z>;
A := <0,0,0,0>; f := w+x^2+y^3+z^4+w^2*cos(x*y^2); L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() ![]() ![]() ![]() ![]() |
(11) |
Exercise #12: Let have linear and quadratic approximations
(based at ) called
and
. Show that in
this case . What can be said when
?
A linear approximation of a linear function is just the function itself and since the
linear approximation is already perfect, the quadratic approximation will be no
different. Now if is a quadratic function, we can expect (and in fact prove)
that although it probably won't match its linear approximation
anymore.
> | X := <x,y>;
A := <a,b>; # I'm avoiding the name "A" which is already in use. f := cA*x+cB*y+cC; L := LinearApproximation(f,X,A); Q := QuadraticApproximation(f,X,A); H := Hessian(f,convert(X,list)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(12) |
Exercise #13: Write down the -order terms for the Taylor polynomial expansion
of at
.
Exercise #14: Write down the 4-order terms for the Taylor polynomial expansion
of at
.
I'm not sure why, but...
Here is exercise #5 reworked without using any fancy commands (minus printing
our the Hessian). Each partial is computed and then evaluated at the point (just like
we'd do by hand).
> | f := (x,y) -> sin(x*y);
'f(x,y)' = f(x,y); a := 3; b := Pi; |
![]() |
|
![]() |
|
![]() |
|
![]() |
(13) |
> | fx := diff(f(x,y),x);
fy := diff(f(x,y),y); fxx := diff(f(x,y),x,x); fxy := diff(f(x,y),x,y); fyy := diff(f(x,y),y,y); fxS := simplify(subs(x=a,y=b,fx)); fyS := simplify(subs(x=a,y=b,fy)); fxxS := simplify(subs(x=a,y=b,fxx)); fxyS := simplify(subs(x=a,y=b,fxy)); fyyS := simplify(subs(x=a,y=b,fyy)); L := simplify(f(a,b) + fxS*(x-a) + fyS*(y-b)); Q := simplify(L + (1/2)*(fxxS*(x-a)^2+2*fxyS*(x-a)*(y-b)+fyyS*(y-b)^2)); |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
(14) |
Maple has a nice built in command to compute multivariate Taylor polynomials.
So my "LinearApproximation" and "QuadraticApproximation" functions aren't
needed. We can just compute the Taylor polynomials of orders 1 and 2.
Let's redo exercise #5 (yet again and again no Hessian) using the
"TaylorApproximation" command.
> | with(Student[MultivariateCalculus]): |
> | simplify(TaylorApproximation(sin(x*y),[x,y]=[3,Pi],1));
simplify(TaylorApproximation(sin(x*y),[x,y]=[3,Pi],2)); |
![]() |
|
![]() |
(15) |
Now we can also compute 3rd, 4th, ... order Taylor polynomials as well.
Here are the 3rd, 4th, and 5th order Taylor polynomials (continuing our previous
example).
> | simplify(TaylorApproximation(sin(x*y),[x,y]=[3,Pi],3));
simplify(TaylorApproximation(sin(x*y),[x,y]=[3,Pi],4)); simplify(TaylorApproximation(sin(x*y),[x,y]=[3,Pi],5)); |
![]() ![]() |
|
![]() ![]() |
|
![]() ![]() ![]() ![]() ![]() |
(16) |
This command can also produce plots and animations. Although, you need to fight with them
a bit to get them to show what you want.
> | TaylorApproximation(sin(x*y),[x,y]=[3,Pi],2,x=2.2..3.8,y=2.3..3.9,output=plot); |
![]() |
> | TaylorApproximation(sin(x*y),[x,y]=[3,Pi],10,output=animation); |
![]() |