Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
假设我们有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命令似乎没有提供任何方式来将所有不同形式的符号表达式作为输出参数返回。它只返回最简单的一个,其余的显示在命令窗口中。 如果要避免从“命令窗口”中剪切和粘贴,可以使用我编写的此函数,该函数利用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 更多&回答... |
![]() |
![]() |