Taylor Expansion with 2 variables
12-30-2019, 01:27 AM
Post: #1
 hamorabi Junior Member Posts: 13 Joined: Dec 2019
Taylor Expansion with 2 variables
I'm sharing a little function I wrote that returns the Taylor expansion of functions of two variables. The Taylor function included in the prime only works for single variable functions.

Function definition:
PHP Code:
taylor2d(ff,vars,exppt,ordr)

For example, to return the second order polynomial that approximates the function sin(2x)+cos(y) at the point x=0, y=0:
PHP Code:
taylor2d(sin(2x)+cos(y),[x,y],[0,0],2)

You can also use this function to find the equation of the tangential plane to a surface. Since the tangential plane is a first order approximation, just set the parameter ordr to 1:
PHP Code:
taylor2d(ln(2*x+y),[x,y],[-1,3],1)
PHP Code:
2x+y-1
Equate the result to 0 and you have the equation of the tangential plane.

PHP Code:
#casdef taylor2d(ff,vars,exppt,ordr):  """  taylor2d(sin(2x)+cos(y),[x,y],[0,0],2)  taylor2d(x*e^y+1,[x,y],[1,0],2)  """  if (type(vars) != DOM_LIST) and (type(exppt) != DOM_LIST):    return taylor(ff,equal(vars,exppt),ordr,polynom)  elif (len(vars) == 2 and len(exppt) == 2):    pp = seq(equal(vars[ii],exppt[ii]),ii,0,len(vars)-1)    ii, nn, bb, dd = 0, 0, 0, 0    while (ii<=ordr):      bb = binomial(ii,nn)/(ii!)*((vars[0]-exppt[0])**nn)*((vars[1]-exppt[1])**(ii-nn))      dd += subst(diff(diff(ff,vars[0],nn),vars[1],ii-nn),pp)*bb      if (nn==0):        ii += 1        nn = ii + 1      nn -= 1    return dd  else:    return "Error!"#end

Attached File(s)