How do I know, what simplifying steps MATLAB had taken?

4 次查看(过去 30 天)
I know that using "simplify" command I can simplify my trignometric matrices. But, can we take output on each simplification step that MATLAB takes to simplify the given expression? I am trying to simplify this:
if true
% clc
clear all
syms o l1 l2 l0;
A=[ cos(o) -sin(o) 1;
cos(o-(2*pi/3)) -sin(o-(2*pi/3)) 1;
cos(o-(4*pi/3)) -sin(o-(4*pi/3)) 1;]
invA= (2/3)*[cos(o) cos(o-2*pi/3) cos(o-4*pi/3);
-sin(o) -sin(o-2*pi/3) -sin(o-4*pi/3);
0.5 0.5 0.5]
Lss = [l0 + ((l2)*cos(2*o)) (-0.5*l1)+((l2)*cos((2*o)-2*pi/3)) (-0.5*l1)+((l2)*cos((2*o)+2*pi/3));
(-0.5*l1)+((l2)*cos((2*o)-2*pi/3)) l0 + ((l2)*cos(2*(o-2*pi/3))) (-0.5*l1)+((l2)*cos(2*o))
(-0.5*l1)+((l2)*cos((2*o)+2*pi/3)) (-0.5*l1)+((l2)*cos((2*o))) l0 + ((l2)*cos(2*(o-4*pi/3)))]
X=(invA)*(Lss)*(A)
W=simplify(X)
end
and the answer comes to be:
if true
% W =[ l0 + l1/2 + (3*l2)/2, 0, 0]
[ 0, l0 + l1/2 - (3*l2)/2, 0]
[ 0, 0, l0 - l1]
end
which is true. But how do MATLAB got there??

回答(1 个)

NVSL
NVSL 2025-1-28
Hey @Yasir
Unfortunately, I could not find any option in MATLAB that allows us to see each computation step while using “simplify”.
However, according to the MATLAB R2024b “simplify” documentation, name-value pairs “All” set to true, and “Steps” set to any positive number can be used to obtain alternative expressions in the simplification process with a limitation being the expression to be scalar. So, we can use this workaround on each element of the matrix ‘X’ to find the steps involved in simplifying expression on the element.
Since ‘X’ is less complex, “simplify” solves in single step. To visualize the above workaround, I have provided a code involving matrix of more complex equations.
% Define symbolic variables
syms x y z
% Define a 3x1 symbolic matrix with complex equations
matrix = [
(x^2 - 2*x + 1) / (x - 1) + (x^3 - x^2) / (x^2 - x);
sin(x)^2 + cos(x)^2 + tan(x)^2 * (1 - sin(x)^2);
exp(x) * log(exp(x)) + log(x^2) - log(x) + log(x^3);
];
simplified_matrix = simplify(matrix)
simplified_matrix = 
% Simplify each element with 'All' set to true and 'Steps' set to 4
disp('Simplification steps for each element:');
Simplification steps for each element:
for i = 1:3
disp(['Element ', num2str(i), ':']);
steps = simplify(matrix(i), 'All', true, 'Steps', 4);
disp(steps);
end
Element 1:
Element 2:
Element 3:
You can refer to “simplify” documentation for any queries.
https://www.mathworks.com/help/symbolic/sym.simplify.html#namevaluepairarguments
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by