Previous Page Next Page Contents

funcenv -- create a function environment

Introduction

funcenv creates a function environment. A function environment behaves like an ordinary function with the additional possibility to define function attributes. These are used to overload standard system functions such as diff, float etc.

Call(s)

funcenv(f1 <, f2> <, slotTable>)

Parameters

f1 - an arbitrary MuPAD object. Typically, a procedure. It handles the evaluation of a function call to the function environment.
f2 - a procedure handling the screen output of symbolic function calls
slotTable - a table of function attributes (slots)

Returns

a function environment of type DOM_FUNC_ENV.

Further Documentation

Chapter ``Function Environments'' of the Tutorial.

Related Functions

slot

Details

Example 1

We want to introduce a function f that represents a solution of the differential equation f'(x) = x + sin(x)*f(x). First, we define a function f, which returns any call f(x) symbolically:

>> f := proc(x) begin procname(args()) end_proc: f(x), f(3 + y)
                              f(x), f(y + 3)

Because of the differential equation f'(x) = x + sin(x)*f(x), derivatives of f can be rewritten in terms of f. How can we tell the MuPAD system to differentiate symbolic functions calls such as f(x) accordingly? For this, we first have to embed the procedure f into a function environment:

>> f := funcenv(f):

The function environment behaves like the original procedure:

>> f(x), f(3 + y)
                              f(x), f(y + 3)

System functions such as diff still treat symbolic calls of f as calls to unknown functions:

>> diff(f(x + 3), x) 
                                D(f)(x + 3)

However, as a function environment, f can receive attributes that overload the system functions. The following slot call attaches a dummy "diff" attribute to f:

>> f := slot(f, "diff", mydiff): diff(2*f(x^2) + x, x) 
                                      2
                          2 mydiff(f(x ), x) + 1

We attach a more meaningful "diff" attribute to f that is based on f'(x) = x + sin(x)*f(x). Note, that arbitrary calls diff(f(y), x1, x2, ..) have to be handled by this slot:

>> fdiff := proc(fcall) local y; begin
       y:= op(fcall, 1);
       (y + sin(y)*f(y))*diff(y, args(2..args(0)))
   end_proc:
   f := slot(f, "diff", fdiff):

Now, as far as differentiation is concerned, the function f is fully integrated into MuPAD:

>> diff(f(x), x), diff(f(x), x, x)
        x + f(x) sin(x), f(x) cos(x) + sin(x) (x + f(x) sin(x)) + 1
>> diff(sin(x)*f(x^2), x)
                        2                 2      2       2
              cos(x) f(x ) + 2 x sin(x) (x  + f(x ) sin(x ))

Since Taylor expansion around finite points only needs to evaluate derivatives, also Taylor expansions of f can be computed:

>> taylor(f(x^2), x = 0, 9)
                   4 / f(0)       \    8 / f(0)       \      9
           f(0) + x  | ---- + 1/2 | + x  | ---- + 1/8 | + O(x )
                     \  2         /      \  12        /
>> delete f, fdiff:

Background

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000