Expanding and Collecting Equations in MATLAB
The expand and the collect commands expands and collects an equation respectively. The following example demonstrates the concepts:
When you work with many symbolic functions, you should declare that your variables are symbolic.
Create a script file and type the following code:
syms x %symbolic variable x
syms y %symbolic variable x
% expanding equations
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(sin(2*x))
expand(cos(x+y))
% collecting equations
collect(x^3 *(x-7))
collect(x^4*(x-3)*(x-5))
When you run the file, it displays the following result:
ans =
x^2 + 4*x - 45
ans =
x^4 + x^3 - 43*x^2 + 23*x + 210
ans =
2*cos(x)*sin(x)
ans =
cos(x)*cos(y) - sin(x)*sin(y)
ans =
x^4 - 7*x^3
ans =
x^6 - 8*x^5 + 15*x^4
No comments:
Post a Comment