登录论坛

查看完整版本 : 使用简单命令后,我们可以使用一个表达式(我们的选择)吗?


poster
2019-12-10, 20:48
假设我们有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)”?

提前致谢

一只蚂蚁



回答:

SIMPLE (http://www.mathworks.fr/access/helpdesk/help/toolbox/symbolic/simple.html)命令似乎没有提供任何方式来将所有不同形式的符号表达式作为输出参数返回。它只返回最简单的一个,其余的显示在命令窗口中。

如果要避免从“命令窗口”中剪切和粘贴,可以使用我编写的此函数,该函数利用EVALC (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/evalc.html)命令(如Andrew Janke的 (https://stackoverflow.com/users/105904/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 (http://www.mathworks.fr/access/helpdesk/help/toolbox/symbolic/simple.html)生成的所有方程式的符号形式。您只需要选择想要的那个,就像这样:

>> 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

更多&回答... (https://stackoverflow.com/questions/3263737)