千里马不能用求和求解线性方程

时间:2018-05-08 20:26:13

标签: maxima

我正在尝试用最大值计算线性回归的方程,但最大值不能解决线性系统:

Maxima 5.38.1 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.12
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) e: sum((y[i] - (a*x[i]+b))^2, i, 1, n);
                             n
                            ====
                            \                    2
(%o1)                        >    (y  - a x  - b)
                            /       i      i
                            ====
                            i = 1
(%i2) dea: diff(e, a, 1);
                              n
                             ====
                             \
(%o2)                    - 2  >    x  (y  - a x  - b)
                             /      i   i      i
                             ====
                             i = 1
(%i3) deb: diff(e, b, 1);
                                n
                               ====
                               \
(%o3)                      - 2  >    (y  - a x  - b)
                               /       i      i
                               ====
                               i = 1
(%i4) linsolve([dea, deb], [a, b]);
(%o4)                                 []

为什么空?据我所知,maxima无法解决与其他变量相加的变量。我该如何解决?我是否必须使用其他功能,还是必须重新排序方程式?

1 个答案:

答案 0 :(得分:2)

您可以声明sum线性。

declare(sum, linear);
e: sum((y[i] - (a*x[i]+b))^2, i, 1, n);
e: expand(e);

dea: diff(e, a);
deb: diff(e, b);

linsolve([dea, deb], [a, b]);

输出

            n                 n         n
            ====              ====      ====
            \                 \         \
           ( >    x  y ) n - ( >    x )  >    y
            /      i  i       /      i  /      i
            ====              ====      ====
            i = 1             i = 1     i = 1
(%o7) [a = -------------------------------------, 
                  n              n
                 ====           ====
                 \      2       \        2
                ( >    x ) n - ( >    x )
                 /      i       /      i
                 ====           ====
                 i = 1          i = 1
                                    n         n              n         n
                                   ====      ====           ====      ====
                                   \         \              \      2  \
                                  ( >    x )  >    x  y  - ( >    x )  >    y
                                   /      i  /      i  i    /      i  /      i
                                   ====      ====           ====      ====
                                   i = 1     i = 1          i = 1     i = 1
                            b = - --------------------------------------------]
                                             n              n
                                            ====           ====
                                            \      2       \        2
                                           ( >    x ) n - ( >    x )
                                            /      i       /      i
                                            ====           ====
                                            i = 1          i = 1

或者用和的表示法:

declare(sum, linear) $

tellsimpafter('sum(x[i],i,1,n), sx) $
tellsimpafter('sum(y[i],i,1,n), sy) $
tellsimpafter('sum(y[i]^2,i,1,n), sy2) $
tellsimpafter('sum(x[i]^2,i,1,n), sx2) $
tellsimpafter('sum(x[i]*y[i],i,1,n), sxy) $

e: sum((y[i] - (a*x[i]+b))^2, i, 1, n);
e: expand(e);

dea: diff(e, a);
deb: diff(e, b);

linsolve([dea, deb], [a, b]);

                       n sxy - sx sy        sx sxy - sx2 sy
(%o22)            [a = -------------, b = - ---------------]
                                  2                     2
                        n sx2 - sx            n sx2 - sx