设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1301|回复: 0

solve - 符号方程解

[复制链接]

13

主题

152

金钱

235

积分

入门用户

发表于 2018-9-20 14:50:06 | 显示全部楼层 |阅读模式
  1. function varargout = solve(varargin)
  2. %求各种类型方程组的解析解,
  3. %当找不到解析解时,将自动寻求原点附近的一个近似解, 并可达任意精度.
  4. %用法
  5. %   solve('方程1','方程2',...,'方程N','变量1','变量2',...,'变量M')
  6. %SOLVE  Symbolic solution of algebraic equations.
  7. %   SOLVE('eqn1','eqn2',...,'eqnN')
  8. %   SOLVE('eqn1','eqn2',...,'eqnN','var1,var2,...,varN')
  9. %   SOLVE('eqn1','eqn2',...,'eqnN','var1','var2',...'varN')
  10. %
  11. %   The eqns are symbolic expressions or strings specifying equations.  The
  12. %   vars are symbolic variables or strings specifying the unknown variables.
  13. %   SOLVE seeks zeros of the expressions or solutions of the equations.
  14. %   If not specified, the unknowns in the system are determined by FINDSYM.
  15. %   If no analytical solution is found and the number of equations equals
  16. %   the number of dependent variables, a numeric solution is attempted.
  17. %
  18. %   Three different types of output are possible.  For one equation and one
  19. %   output, the resulting solution is returned, with multiple solutions to
  20. %   a nonlinear equation in a symbolic vector.  For several equations and
  21. %   an equal number of outputs, the results are sorted in lexicographic
  22. %   order and assigned to the outputs.  For several equations and a single
  23. %   output, a structure containing the solutions is returned.
  24. %
  25. %   Examples:
  26. %
  27. %      solve('p*sin(x) = r') chooses 'x' as the unknown and returns
  28. %
  29. %        ans =
  30. %        asin(r/p)
  31. %
  32. %      [x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0') returns
  33. %
  34. %        x =
  35. %        [ 1]
  36. %        [ 3]
  37. %
  38. %        y =
  39. %        [    1]
  40. %        [ -3/2]
  41. %
  42. %      S = solve('x^2*y^2 - 2*x - 1 = 0','x^2 - y^2 - 1 = 0') returns
  43. %      the solutions in a structure.
  44. %
  45. %        S =
  46. %          x: [8x1 sym]
  47. %          y: [8x1 sym]
  48. %
  49. %      [u,v] = solve('a*u^2 + v^2 = 0','u - v = 1') regards 'a' as a
  50. %      parameter and solves the two equations for u and v.
  51. %
  52. %      S = solve('a*u^2 + v^2','u - v = 1','a,u') regards 'v' as a
  53. %      parameter, solves the two equations, and returns S.a and S.u.
  54. %
  55. %      [a,u,v] = solve('a*u^2 + v^2','u - v = 1','a^2 - 5*a + 6') solves
  56. %      the three equations for a, u and v.
  57. %
  58. %      [x,y] = solve('sin(x+y)-exp(x)*y = 0','x^2-y = 2') cannot find
  59. %      an analytic solution, so returns a numeric solution.
  60. %
  61. %   See also DSOLVE.

  62. %   Copyright (c) 1993-98 by The MathWorks, Inc.
  63. %   $Revision: 1.13 $  $Date: 1997/11/29 01:06:35 $

  64. % Set the Explicit solution (for equations of order 4 or less)
  65. % option on in the Maple Workspace.

  66. maple('_EnvExplicit := true;');

  67. % Collect input arguments together in either equation or variable lists.

  68. eqns = [];
  69. vars = [];
  70. for k = 1:nargin
  71.    v = varargin{k};
  72.    if all(isletter(v) | ('0'<=v & v<='9') | v == '_' | v == ',')
  73.       vars = [vars ',' v];
  74.    else
  75.       [t,stat] = maple(v);
  76.       if stat
  77.          error(['''' v ''' is not a valid expression or equation.'])
  78.       end
  79.       eqns = [eqns ',' v];   
  80.    end
  81. end

  82. if isempty(eqns)
  83.    warning('List of equations is empty.')
  84.    varargout = cell(1,nargout);
  85.    varargout{1} = sym([]);
  86.    return
  87. else
  88.    eqns(1) = '{'; eqns(end+1) = '}';
  89. end
  90. neqns = sum(commas(eqns)) + 1;

  91. % Determine default variables and sort variable list.

  92. if isempty(vars)
  93.    vars = ['[' findsym(sym(eqns),neqns) ']'];
  94. else
  95.    vars(1) = '['; vars(end+1) = ']';
  96. end
  97. v = vars;
  98. [vars,stat] = maple('sort',v);
  99. if stat
  100.    error(['''' v ''' is not a valid variable list.'])
  101. end
  102. vars(1) = '{'; vars(end) = '}';
  103. nvars = sum(commas(vars)) + 1;

  104. if neqns ~= nvars
  105.    warning([int2str(neqns) ' equations in ' int2str(nvars) ' variables.'])
  106. end

  107. % Seek analytic solution.

  108. [R,stat] = maple('solve',eqns,vars);

  109. % If no analytic solution, seek numerical solution.

  110. if (isempty(R) & (nvars == neqns))
  111.    [R,stat] = maple('fsolve',eqns,vars);
  112. end

  113. if stat
  114.    error(R)
  115. end

  116. % If still no solution, give up.

  117. if isempty(R) | findstr(R,'fsolve')
  118.    warning('Explicit solution could not be found.');
  119.    varargout = cell(1,nargout);
  120.    varargout{1} = sym([]);
  121.    return
  122. end

  123. % Expand any RootOf.

  124. while ~isempty(findstr(R,'RootOf'))
  125.    k = min(findstr(R,'RootOf'));
  126.    p = findstr(R,'{'); p = max(p(p<k));
  127.    q = findstr(R,'}'); q = min(q(q>k));
  128.    s = R(p:q);
  129.    t = s(min(findstr(s,'RootOf'))+6:end);
  130.    e = min(find(cumsum((t=='(')-(t==')'))==0));
  131.    if isempty(find(commas(t(2:e-1))))
  132.       % RootOf with one argument, possibly an imbedded RootOf.
  133.       [s,stat] = maple('allvalues',s,'dependent');
  134.       if stat
  135.          error(s)
  136.       end
  137.    else
  138.       % RootOf with a good estimate as the second argument.
  139.       s = maple('evalf',s);
  140.    end
  141.    R = [R(1:p-1) s R(q+1:end)];
  142. end

  143. % Parse the result.

  144. if nvars == 1 & nargout <= 1

  145.    % One variable and at most one output.
  146.    % Return a single scalar or vector sym.

  147.    S = sym([]);
  148.    c = find(commas(R) | R == '}');
  149.    for p = find(R == '=')
  150.       q = min(c(c>p));
  151.       t = trim(R(p+1:q-1));  % The solution (xk)
  152.       S = [S; sym(t)];
  153.    end
  154.    varargout{1} = S;

  155. else

  156.    % Several variables.
  157.    % Create a skeleton structure.

  158.    c = [1 find(commas(vars)) length(vars)];
  159.    S = [];
  160.    for j = 1:nvars
  161.       v = trim(vars(c(j)+1:c(j+1)-1));
  162.       S = setfield(S,v,[]);
  163.    end

  164.    % Complete the structure.

  165.    c = [1 find(commas(R) | R == '{' | R == '}') length(R)];
  166.    for p = find(R == '=')
  167.       q = max(c(c<p));
  168.       v = trim(R(q+1:p-1));  % The variable (x)
  169.       q = min(c(c>p));
  170.       t = trim(R(p+1:q-1));  % The solution (xk)
  171.       S = setfield(S,v,[getfield(S,v); sym(t)]);
  172.    end
  173.    
  174.    if nargout <= 1

  175.       % At most one output, return the structure.
  176.       varargout{1} = S;

  177.    elseif nargout == nvars

  178.       % Same number of outputs as variables.
  179.       % Match results in lexicographic order to outputs.
  180.       v = fieldnames(S);
  181.       for j = 1:nvars
  182.          varargout{j} = getfield(S,v{j});
  183.       end

  184.    else
  185.       error([int2str(nvars) ' variables does not match ' ...
  186.              int2str(nargout) ' outputs.'])
  187.    end
  188. end

  189. %-------------------------

  190. function s = trim(s);
  191. %TRIM  TRIM(s) deletes any leading or trailing blanks.
  192. while s(1) == ' ', s(1) = []; end
  193. while s(end) == ' ', s(end) = []; end

  194. %-------------------------

  195. function c = commas(s)
  196. %COMMAS  COMMAS(s) is true for commas not inside parentheses.
  197. p = cumsum((s == '(') - (s == ')'));
  198. c = (s == ',') & (p == 0);
复制代码


SOLVE.rar

2.51 KB, 下载次数: 0, 下载积分: 贡献 -1

售价: 1 金钱  [记录]  [购买]

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表