作为一个例子,我想尝试找到
的偏导数f(x) = \sum_{i=1}^n x_i^2
千里马中的 (预期的输出将是\frac{\partial f}{\partial x_k} = 2x_k
)我试过跟随,但似乎索引变量没有像我预期的那样处理,有人能解释我做错了吗?
如果您将n
和k
替换为实际数字,则相同的命令有效,但不是以这种形式:
f(x) := 1/2 * sum( x[i]^2, i, 1, n);
print(diff(f(x),x[k]));
答案 0 :(得分:2)
Maxima can't handle derivative with respect to a indexed variable by default. I wrote a couple of small packages to handle these problems. Perhaps this is useful to you.
See: https://pastebin.com/MmYJRqCq (sum_kron_delta, summation of Kronecker delta) and: https://pastebin.com/UGCPgvAn (diff_sum, derivative of summation wrt indexed variable)
Here's an example applied to your problem. I'll assume you have downloaded the code above to your computer.
(%i1) load ("sum_kron_delta.mac");
(%o1) sum_kron_delta.mac
(%i2) load ("diff_sum.mac");
(%o2) diff_sum.mac
(%i3) 'diff ('sum (x[i]^2, i, 1, n), x[j]);
n
====
\
(%o3) 2 > x kron_delta(i, j)
/ i
====
i = 1
Note that you have to write 'diff('sum(...
that is, with the quote mark '
to indicate that diff
and sum
are nouns (formal expressions) instead of verbs (functions which are called). This is necessary in the implementation of diff_sum
and sum_kron_delta
because they work with simplification rules. (It's a long story, which I can explain if there's interest.)
I see we got the kron_delta summation, but we need to cause the simplification rules to be applied. We could also write expand(%, 0, 0)
here instead of ''%
.
(%i4) ''%;
(%o4) 2 (if (1 <= j) and (j <= n) and %elementp(j, integers) then x else 0)
j
At this point we have the final result, which we can simplify further with additional data.
(%i5) assume (j >= 1, j <= n);
(%o5) [j >= 1, n >= j]
(%i6) ''%o4;
(%o6) 2 (if %elementp(j, integers) then x else 0)
j
(%i7) declare (j, integer);
(%o7) done
(%i8) ''%o6;
(%o8) 2 x
j
If this seems fruitful to you, I'll be happy to go into details.