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

Matlab 在使用简单命令之后,我们可以使用一个表达式(我们的选择)吗?,matlab,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)”,而不进行剪切和粘贴 提前谢谢 Anant该命令似乎没有提供任何方法来返回所有不同形式的符号表达式作为输出参数。它只返回最简单的一个,其余的显示在命令窗口中 如果希望避免从命令窗口剪切和粘贴,可以

假设我们有像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)”,而不进行剪切和粘贴

提前谢谢

Anant

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

如果希望避免从命令窗口剪切和粘贴,可以使用我编写的此函数,它利用命令(如所建议)捕获命令窗口中显示的输出:

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
此函数将返回一个单元格数组,其中包含由生成的所有方程式的符号形式。你只要选一个你想要的,就像这样:

>> 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
该命令似乎没有提供任何方法将所有不同形式的符号表达式作为输出参数返回。它只返回最简单的一个,其余的显示在命令窗口中

如果希望避免从命令窗口剪切和粘贴,可以使用我编写的此函数,它利用命令(如所建议)捕获命令窗口中显示的输出:

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
此函数将返回一个单元格数组,其中包含由生成的所有方程式的符号形式。你只要选一个你想要的,就像这样:

>> 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()来避免弄乱临时文件吗?可能会使它在已经在日记()下运行的代码中工作。@Andrew:说得好。我忽略了评估。我将很快发布一个更新的解决方案。您能使用evalc()避免弄乱临时文件吗?可能会使它在已经在日记()下运行的代码中工作。@Andrew:说得好。我忽略了评估。我将很快发布更新的解决方案。