【发布时间】: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
假设我们有像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
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
【讨论】: