Re: Evaulate an arithmetic expression

From: David Sletten (david_at_slytobias.com)
Date: 02/09/05


Date: Wed, 09 Feb 2005 07:05:22 GMT

fix wrote:

>>>> This could be even simpler:
>>>> (defun deriv-eval (expr var val)
>>>> (setf (symbol-value var) val)
>>>> (eval (deriv expr val)))
>>>>

>> In the terser example, we simply use the derivative directly rather
>> than storing it temporarily. The fact that we've already assigned a
>> value to X is irrelevant. Your derivative code isn't evaluating
>> variables, is it? It's simply looking for the symbol with respect to
>> which you are differentiating right? In other words, differentiating
>> 3x^2 - 4x wrt x is 6x - 4 regardless of the value of x. Even if you
>> know ahead of time that x = 3 you can't evaluate 3(3^2) - 4(3) and
>> then take the derivative.
>
>
> Do you mean "3" will be the symbol if you set x=3?
> It is just messing up my mind, I can't decide when it is a variable and
> when it is a number.
> What if I deriv (* 3 x) and substitute x=3? The "3" in (* 3 x) would not
> be regarded as a variable and be differentiated?
>

The process of differentiation of polynomials is simply a formal
transformation. Regardless of whether the variable is x or y or z, we
take the pattern <var>^2 and produce 2*<var>. This is what your code is
doing in DERIV. It takes the expression (* x x) and produces (+ x x). It
would transform the expression (* z z) into (+ z z), right? This process
does not involve evaluating variables--it simply transforms one
expression into another. It's only when we ask Lisp to EVAL the
derivative expression that we need concern ourselves with what X (or Z)
represents. So in the shorter version above, the assignment of a value
to X is independent of the production of the derivative expression. As
long as we do the SETF _sometime_ before we call EVAL we're OK. Because
EVAL sees the derivative expression, (+ x x), for instance and has to
determine "what is the value of X?".

>> In any case, take a look at Alan's example. He has a better approach.
>
>
> Alan's?
Take a look at the example Alan Crowe presented elsewhere in this
thread. It uses PROGV to limit the scope of the variables you evaluate
in your derivatives.

David Sletten



Relevant Pages

  • Re: Simple Question on Derivatives
    ... derivatives, and moreover you haven't specified what variable you are ... you are differentiating with respect to x, you will be dividing by zero. ... If, on the other hand, x and y are discrete numerical arrays, it still ... In this case the requirement of even spacing for the third ...
    (comp.soft-sys.matlab)
  • Re: integration + Levenberg-Marquardt (Numerical Recipes)
    ... I am having problems getting my fitting function>derivatives correct ... The integration is correct when I test the resulting>data but I don't ... This follows by differentiating the equation ...
    (sci.math.num-analysis)
  • Re: Linear operators 3
    ... Why are you differentiating? ... You are given TWO FUNCTIONS f and g, with domain, and codomain ... you are going to use derivatives; to check that your proposed h also ... would recommend you quit giving me a hard time and quit your position ...
    (sci.math)
  • Re: Linear operators 3
    ... Do you know any way by which, given a CONTINUOUS function f, you can ... Why are you differentiating? ... You are given TWO FUNCTIONS f and g, with domain, and codomain ... you are going to use derivatives; to check that your proposed h also ...
    (sci.math)
  • Re: Looking for suggestions on a root search strategy
    ... possible to avoid evaluating f or its derivatives at points ... Let's assume that both f'and f''have an upper bound. ... without an illegal function evaluation. ...
    (sci.math.num-analysis)