我想在Matlab中求解一个线性方程组。问题是这个系统一般都有一个非唯一的解(所以Nullspace是非平凡的),并且这个系统依赖于一个参数β(非零!)。因此,我希望有这个参数的解决方案。MATLAB能做到这一点吗?我需要以什么样的方式输入方程和参数,我需要使用哪个命令,这样Matlab就能给出所有的解。
发布于 2014-05-10 06:30:27
希望这能有所帮助。这不是最理想的。它的测试在八度,有一些稍微不同的解析规则,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
发布于 2014-05-11 07:46:03
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中解决这个问题。
https://stackoverflow.com/questions/23580840
复制相似问题