【问题标题】:can we use one expression(our choice) after using simple command?使用简单命令后我们可以使用一个表达式(我们的选择)吗?
【发布时间】:2010-07-17 01:12:11
【问题描述】:

假设我们有像eq1=sin(t)*cos(t)^2-sin(t)*cos(t)^4这样的表达式。我们想通过matlab的简单命令来简化这个表达式。我们得到不同的形式在 matlab 提示符下。 我们如何在不剪切和粘贴的情况下使用其中一种所需的形式,例如“1/16*sin(3*t)+1/8*sin(t)-1/16*sin(5*t)”?

提前致谢

蚂蚁

【问题讨论】:

    标签: matlab


    【解决方案1】:

    SIMPLE 命令似乎没有提供任何方法来返回所有符号表达式的不同形式作为输出参数。它只返回最简单的一个,并在命令行窗口中显示其余的。

    如果你想避免从命令行窗口剪切和粘贴,你可以使用我写的这个函数,它利用EVALC 命令(如Andrew Janke 所建议)来捕获显示在命令窗口:

    function allEquations = simple_forms(S)
    
      output = evalc('simple(S);');             %# Capture the output from the
                                                %#   Command Window
      newlineIndex = find(output == char(10));  %# Get the indices of newlines
      lineSizes = diff([0 newlineIndex]);       %# Get the sizes of each line
      output = mat2cell(output,1,lineSizes);    %# Put the lines in a cell array
      output = deblank(output);                 %# Remove blank spaces
      emptyIndex = cellfun(@isempty,output);    %# Find the indices of empty lines
      output(emptyIndex) = [];                  %# Remove the empty lines
      allEquations = output(2:2:end);           %# Get the even lines (where the
                                                %#   formulae are)
      allEquations = cellfun(@sym,allEquations,...    %# Convert the formulae to
                             'UniformOutput',false);  %#   symbolic expressions
    
    end
    

    此函数将返回一个元胞数组,其中包含SIMPLE 生成的所有方程的符号形式。你只需要选择你想要的,就像这样:

    >> eq1 = sym('sin(t)*cos(t)^2-sin(t)*cos(t)^4');  %# Create a symbolic equation
    >> eqs = simple_forms(eq1);                       %# Get the different forms
    >> eqs{1}                                         %# Pick the first formula
    
    ans =
    
    sin(3*t)/16 - sin(5*t)/16 + sin(t)/8
    

    【讨论】:

    • 你能用 evalc() 避免弄乱临时文件吗?也可以让它在已经在 diary() 下运行的代码中工作。
    • @Andrew:好点子。我忽略了EVALC。我会尽快发布更新的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2018-07-10
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2012-01-24
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多