Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 20:48   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 使用简单命令后,我们可以使用一个表达式(我们的选择)吗?

假设我们有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

更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 01:34


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.