Subject: Cannot use "diff" in a function assignment.
From: Stavros Macrakis
Date: Wed, 23 Nov 2011 17:15:52 -0500
This is a difficult point for many new users.
It appears that you want to define a mathematical function f and then
define a mathematical function df which is df/dx. The simplest and most
Maxima-like way of doing this is NOT to use use function-definition (which
is really subroutine-definition). You'd do it something like this:
f: x^3 + 2*x^2 + x - 1;
df: diff(f,x)$
Now to get the value of f at a given value, you'd do subst([x=3],f); for
df, subst([x=3],df). Another way is ev(f,x=3) and ev(df,x=3), but I don't
recommend that (long discussion).
Another approach, if you really want to define subroutines, is:
f(x) := x^3 + 2*x^2 + x - 1;
define(df(x), diff(f(x),x))$
Now you can write f(3) and df(3).
Another way to write the "define" is
df(x) := ''(diff(f(x),x))$
but I don't recommend that. In particular, it won't work as you expect
within programs.
Note that in BOTH approaches above, if you redefine f, it will not redefine
df!
Well, if I go on, I think I will make things more rather than less
confusing....
-s
On Wed, Nov 23, 2011 at 16:53, Daniel Carrera <dcarrera at gmail.com> wrote:
> Hello,
>
> I'm pretty new to Maxima and I wonder why the following doesn't work:
>
> (%i1) f(x) := x^3 + 2*x^2 + x - 1;
> (%o1) ...
>
> (%i2) df(x) := diff( f(x), x )
> (%o2) ...
>
> (%i3) df(2);
> diff: second argument must be a variable; found 2
>
>
>
> I don't understand why this is a problem. It seems like a fairly natural
> thing to want to do - differentiate a function and use the result as a new
> function. Strange, because I seem to be able to use diff(f(x),x) in other
> similar contexts:
>
> (%i4) linsolve( [diff( f(x),x) = 2], [x] );
> (%o4) [ x = 1/4]
>
> (%i5) plot2d( diff(f(x),x), [x,-1,1] );
> (%o5)
>
>
> Any ideas?
>
> Cheers,
> Daniel.
> --
> I'm not overweight, I'm undertall.
> ______________________________**_________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/**mailman/listinfo/maxima<http://www.math.utexas.edu/mailman/listinfo/maxima>
>