使用简单命令后我们可以使用一个表达式(我们的选择)吗?

时间:2010-07-16 10:03:45

标签: matlab

假设我们有像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)”而不切割和粘贴?< / p>

提前致谢

了Anant

1 个答案:

答案 0 :(得分:1)

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

如果你想避免必须从命令窗口剪切和粘贴,你可以使用我写的这个函数,它使用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