Bill Allombert on Thu, 02 Jun 2005 13:23:43 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: User functions and their derivatives


On Thu, Jun 02, 2005 at 12:35:29PM +0200, Sascha Rissel wrote:
> Hello,
> 
> Let's say I define a function (let's choose a simple one)
> > f(x) = x^2
> 
> I can compute values of this function, like
> > f(3)
> and get 9 in this case as a result.
> 
> Now I would like to derive this function, so I define a function f1 for the derivative of f:
> > f1(x) = f(x)' (or f1(x) = deriv(f(x))
> 
> The problem that occurs now is, that I am not able to use the derivative f1(x) for computation.
> Let's say I want to compute some values of f1:
> > f1(3)
> The result I get is always 0 and not the expected value 6 (in this example), 'cause the derivative is 2*x of course.
> Also plotting of f1(x) does not work. I think this is all the same problem.
> 
> So what am I doing wrong?

You mix f'(x) with f(x)'. Derivation is an operator that act on
function, not numbers. f1(3) compute f(3)' which is in PARI view, just
like 9' which is 0.

What do you want to do ? 3 solutions:

1) Use the numerical derivative facility:
? f1(x)=f'(x)
? f1(3)
%1 = 6.000000000000000000000000000


2) Handle your function as a rational function:
Evaluate it on X, derive it and the evaluate it in x:
? f1(x)=subst(f(X)',X,x)
? f1(3)
%4 = 6
This only work because f is a rational function.

3) Use the standard formula for derivative:
? f1(x)=truncate((f(x+h+O(h^2))-f(x))/h)
? f1(3)
%6 = 6

This work for all functions that PARI can compute the Taylor expansion,

(the formula below can be written more efficiently as
f1(x)=polcoeff(f(x+h+O(h^2)),1,h)
)
> I have another small question:
> If I define variables or functions (like in the case above), how can I undefine these variables and functions, to be able to use the names for other concerns?

If you want to reset a variable, do
X='X

('X is the monomial of degre 1 and coeff 1 in the variable X).

If you want to replace a function by a variable or vice-versa you need
to use kill:
? f(x)=x^2
? kill(f)
? f
%7 = f

If you want to reset a function, don't!

Cheers,
Bill.