Mathematica differentiates a function expression and sets it as a new custom function

custom function

Custom functions need to pay attention to the highlights
1. It is better to use :=instead of =
2. When defining, the function variable at the left end of the equation is underlined, which is called "blank"

Perhaps the most powerful aspect of Wolfram System transformation rules is that they can be used not only with expressions, but also with patterns. A pattern is an expression such as f[t_] that contains an underscore. An underscore can represent any expression . Thus, a transformation rule for f[t_] will transform a function f with any arguments. Note that, by contrast, a transformation rule for f[x] without an underscore transforms only the expression f[x], while for Expressions such as f[y] have no effect.
When you define a function such as f[t_]:=t^2, what you are doing is telling the Wolfram System to automatically use the transformation rule f whenever possible [t_]->t^2.
(Quoted from the MMA tutorial "Function Transformation Rules")

After defining the function, you can assign values ​​to variables, and you can also draw graphs

f2[t_] := t^3
f2[3]
f2[a]
Plot[f2[t], {
    
    t, 0, 3}]

Differentiate a function expression

This operation is relatively simple, directly use D[f,x]or D[f,{x,n}]to represent partial derivatives and higher order derivatives respectively.
An example is given below

f[x_] := x^5 + 6 x
D[x^5 + 6 x, x]
D[f[x], x]
D[f[t], t]

The result of the operation is
insert image description here

save the result of the differentiation as a new custom function

Sometimes it is hoped that the result of the derivation can be saved as a new custom function for later use.
An intuitive but wrong approach is to
insert image description here

As can be seen from the figure above, the result of g[t] seems to be fine, but the result of g[3] is not quite right.
The reason here is that x in the original function f is also assigned when x is assigned .
Looking at f and g, it can be found that g is not an independent function, but the variable must be substituted into f first, and then derived. So for g[2], it is equivalent to seeking D[f(2),2], which is unreasonable.
Four options are provided below, of which the first two options feel the best.

Solution 1

It is generally customary to use it when customizing functions :=, and it is just to use =here to solve the problem. Right now

f[x_] := x^5 + 6 x
g[x_] = D[f[x], x]
g[t]
g[2]

The result is as follows
insert image description here

Solution 2

Use Block (block)

Block[{x,y,...},expr], meaning to specify that expr is computed with local values ​​of the symbols x, y, ….

f[x_] := x^5 + 6 x
g[x_] = Block[{
    
    t}, t = x; D[f[t], t]];   (*注意这里用的是=*)
g[t]
g[2]

The result is as follows
insert image description here

Solution 3

Similar to the previous solution, note that the:=

f[x_] := x^5 + 6 x
g[x_] := Block[{
    
    result}, result = D[f[t], t] /. {
    
    t -> x}; 
   Return[result]];
g[t]
g[2]

insert image description here

Solution 4

Since the cause of the problem lies in the assignment of x, then change the variable name.
But the disadvantage of this is that there are more functions and more variable names.

f[x_] := x^5 + 6 x
g[xx_] := D[f[x], x] /. x -> xx
g[t]
g[2]

The result is as follows
insert image description here

Guess you like

Origin blog.csdn.net/gsgbgxp/article/details/127412355