【问题标题】:Why does the following expression result in error?为什么下面的表达式会导致错误?
【发布时间】:2013-01-30 02:44:44
【问题描述】:

免责声明。我熟悉 Mathematica 但不熟悉 Matlab,所以如果这是一个新手问题,我深表歉意。

在使用 Matlab 的求解命令时,我在 Matlab 上遇到了一个奇怪的错误:

solve(0.2 = (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)

错误是:

Error: The expression to the left of the equals sign is not a valid target for an assignment.

Mathematica 中等效的 Solve 命令(使用相同的表达式)完美运行,所以我不认为我的表达式本身是无效的。 此外,当我尝试使用文档站点上的示例时,我得到了同样的错误:http://www.mathworks.com/help/symbolic/mupad_ref/solve.html

是配置问题还是我误解的命令语法有什么问题?

编辑:我也尝试使用 == 而不是 =,但我得到一个不同的错误:

Undefined function or variable 'M'.

另外,请注意,我正在运行 Matlab R2011b (7.13.0.564) 64 位 (glnxa64)。

Edit2:我用 syms 尝试了第一个建议的解决方案:

>> syms M
>> solve(0.2 == (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
Error using char
Conversion to char from logical is not possible.

Error in solve>getEqns (line 245)
  vc = char(v);

Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});

Edit3:即使是最简单的方程式,我也能重现这个问题

>> syms x
>> solve(x^2 -4 == 0, x)
Error using char
Conversion to char from logical is not possible.

Error in solve>getEqns (line 245)
  vc = char(v);

Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});

此外,我也尝试了这里建议的解决方案:MATLAB examples are failing

【问题讨论】:

    标签: matlab


    【解决方案1】:

    Matlab 的 fsolve 命令假定表达式设置为零。如果用数字求解,你不会这样做:

    x=solve(2=x+1,x)
    

    而是:

    x=fsolve(@(x) x+1-2,0)
    

    方程已经设置为零,@(x) 是您要求解的,0 是初始猜测。您必须包括在内。

    象征性地使用solve,它看起来像这样:

    syms x
    val=solve(x+1-2)
    

    或者对于您的系统:

    syms M
    solve(-0.2+ (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))))
    
    ans =
                                         4.7161724968093348297842999805458
                                       0.029173662296926424763929809009225
     - 3.8716404782846254923900841980317 - 3.4984412176176158766571497649425*i
       1.4989673987314948651159693032542 + 5.5784387926679222829321168661041*i
       1.4989673987314948651159693032542 - 5.5784387926679222829321168661041*i
     - 3.8716404782846254923900841980317 + 3.4984412176176158766571497649425*i
    

    【讨论】:

    • 似乎使用符号然后没有任何形式的 == 符号符合我的要求。
    【解决方案2】:

    您应该将 M 定义为sym,并使用== 而不是=

    syms M
    solve(0.2 == (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
    
    ans =
                                             4.7161724968093348297842999805458
                                           0.029173662296926424763929809009225
     - 3.8716404782846254923900841980317 - 3.4984412176176158766571497649425*i
       1.4989673987314948651159693032542 + 5.5784387926679222829321168661041*i
       1.4989673987314948651159693032542 - 5.5784387926679222829321168661041*i
     - 3.8716404782846254923900841980317 + 3.4984412176176158766571497649425*i
    

    【讨论】:

    • 你运行的是什么版本的 Matlab?
    猜你喜欢
    • 2013-12-04
    • 1970-01-01
    • 2022-11-14
    • 2021-01-03
    • 2019-08-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    相关资源
    最近更新 更多