【问题标题】:Solve system of equations in Matlab在 Matlab 中求解方程组
【发布时间】:2014-05-10 12:04:42
【问题描述】:

我想在 Matlab 中求解一个线性方程组。问题是这个系统通常会有一个非唯一的解决方案(所以 Nullspace 是不平凡的)并且这个系统依赖于一个参数 beta(非零!)。因此,我想根据这个参数得到解决方案。 MATLAB 能做到这一点吗?我需要以什么方式输入方程和参数以及我需要使用哪个命令以便 Matlab 为我提供所有解决方案?

【问题讨论】:

    标签: matlab math equation equation-solving


    【解决方案1】:

    希望这会有所帮助。这并不意味着是最佳的。它在八度音阶中进行了测试,有一些 matlab 的解析规则略有不同,我通常很好地保持在共享范围内 octave 和 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
    

    【讨论】:

      【解决方案2】:

      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.nethttp://sourceforge.net/p/maxima

      【讨论】:

        猜你喜欢
        • 2018-05-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多