我想在Matlab中求解一个线性方程组。问题是这个系统通常会有一个非唯一的解决方案(所以Nullspace是非平凡的),这个系统依赖于参数beta(非零!)。因此,我想根据这个参数得到解决方案。 MATLAB能够做到这一点吗?我需要以什么方式输入方程式和参数以及我需要使用哪个命令,以便Matlab为我提供所有解决方案?
答案 0 :(得分:1)
希望这会有所帮助。它并不意味着是最优的。它在八度音阶中进行了测试 与matlab稍微不同的解析规则,我通常很好地保持共享 八度和matlab的语法,但提供公平的警告。
function x=solver(A,y,freeVars)
%
% x=solver(A,y,freeVars)
%
% Solve system of equations Ax=y for x.
% Use elements of freeVars to fill undetermined ranks and produce
% a unique solution.
%
% Typically this is of form
%
% f_1( t_1 ) * x_1 + f_2( t_1 ) * x_2 ... + f_n( t_1 ) * x_n = y_1
%
% f_1( t_2 ) * x_1 + f_2( t_2 ) * x_2 ... + f_n( t_2 ) * x_n = y_2
% .
% .
% .
% f_1( t_m ) * x_1 + f_2( t_m ) * x_2 ... + f_n( t_m ) * x_n = y_m
%
% A= [ f_1( t_1 ) , f_2( t_1 ) , ... f_n( t_1 ) ;
% f_1( t_2 ) , f_2( t_2 ) , ... f_n( t_2 ) ;
% ...
% f_1( t_m ) , f_2( t_m ) , ... f_n( t_m ) ];
%
% For example a first order linear fit would be
% f_1(t) = 1
% f_2(t) = t
%
%
% If the problem is overdetermined this would be a least squares problem
% that is not going to be addressed here.
%
% Assuming fully determined, one solution would be
% Given:Ax=y
% [U,S,V]= svd(a)
% such that U*S*V'*x = y
% S*V'*x = U'*y
% for fully determined case S is invertable.
% for less than fully determined case rank(S) < n,
% Let [ S_r | 0 ] represent the non-zero and zero columns of S.
% and [ V_r | 0 ] represent the columns of V that are used vs.
% ones multiplied by zeros of S.
% [ S_r | 0 ] * [ V_r |0 ]' * x = [ U_r | 0 ]' * y
%
% V_r is in some sense a projection of your x coordinates into rank(S)
% subspace that is fully determined. That portion can be solved
% but requires additional parameters to fully determine X.
%
% x = V * [ inv(S_r) U_r' * y ; alpha ]
%
% where alpha's are free parameters filling the extra degrees or freedom.
%
% The columns of V that aren't included in V_r are (were temporarily
% temporarily replace by zeros determine which of the x parameters are
% impacted by each of the free parameters.
%
% Rather than use freevariables as I do here I presume one could set
% some x's that were influenced by those freevars to desired values
% and backsolve what values of free vars would produce those x's and
% then obtain values for the remaining undetermined x's from the computed
% free vars.
%
%
[U,S,V]=svd(A)
s=diag(S);
%
% Default rank tolerance taken from help page on rank.
%
r=sum(s>max(size(A)) * max(s)* eps )
%
%
U_r=U(:,1:r)
S_r=S(1:r,1:r)
%
alpha = freeVars(1:(size(y,1)-r) ,1)
%
invS_r = diag(diag(S_r).^-1)
x = V * [ invS_r * U_r' * y ; alpha ];
%
% aka:
% x = V_r * S_r^(-1) * U_r' *y + V_n * alpha
简单的测试用例
% Fully determined case:
% mt+b = y x=[b;m]=[1;2] evaluated at t=0, t=1
%
t=[ 0 ; 1]
%
% A = [ 1 , t ]
%
A=[ ones(2,1) , t]
%
%
xd=[ 1 ; 2 ]
y = xd(1) + xd(2)* t
x=solver(A,y,[1;2;3;4;5])
xerr=xd-x
yerr=A*x-y
% under determined case:
% mt+b = y w/ x=[b;m]=[1;2] evaluated at t=0, t=0
%
t=[ 0 ; 0]
%
% A = [ 1 , t ]
%
A=[ ones(2,1) , t]
%
%
xd=[ 1 ; 2 ]
y = xd(1) + xd(2)* t
x=solver(A,y,[1;2;3;4;5])
xerr=xd-x
yerr=A*x-y
答案 1 :(得分:0)
Maxima [1]可以求解包含符号变量的方程式(如你所提到的beta
),如果解决方案不是唯一的,它将引入虚拟变量,使得解决方案对所有虚拟变量值都有效。例如:
(%i5) solve ([3 * x + beta * y = 5, -6 * x - 2*beta * y = -10], [x, y]);
solve: dependent equations eliminated: (2)
%r2 beta - 5
(%o5) [[x = - ------------, y = %r2]]
3
此处%r2
是虚拟变量,解决方案适用于%r2
的任何值。
然而,Maxima的符号解算器使用了大量内存,这可能会对它可以处理的问题的大小设置相对较低的限制。你有多少个方程式和多少个变量?也许你可以在这里发布方程组。
很抱歉,我不知道如何在Matlab中解决这个问题。
[1] http://maxima.sourceforge.net,http://sourceforge.net/p/maxima