首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >用Matlab求解方程组

用Matlab求解方程组
EN

Stack Overflow用户
提问于 2014-05-10 04:04:42
回答 2查看 938关注 0票数 1

我想在Matlab中求解一个线性方程组。问题是这个系统一般都有一个非唯一的解(所以Nullspace是非平凡的),并且这个系统依赖于一个参数β(非零!)。因此,我希望有这个参数的解决方案。MATLAB能做到这一点吗?我需要以什么样的方式输入方程和参数,我需要使用哪个命令,这样Matlab就能给出所有的解。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-10 06:30:27

希望这能有所帮助。这不是最理想的。它的测试在八度,有一些稍微不同的解析规则,matlab,我一般很好地保持在共享语法的八度和matlab,但提供公平的警告。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    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

和简单的测试用例

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    % 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
EN

Stack Overflow用户

发布于 2014-05-11 07:46:03

Maxima 1可以求解包含符号变量的方程(如您所提到的beta ),如果解不是唯一的,它将引入虚拟变量,这样该解对所有虚拟值都有效。例如:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
(%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中解决这个问题。

1http://sourceforge.net/p/maxima

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23580840

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文