我正在尝试用最大值计算线性回归的方程,但最大值不能解决线性系统:
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无法解决与其他变量相加的变量。我该如何解决?我是否必须使用其他功能,还是必须重新排序方程式?
答案 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