Monday, 8 August 2016

Calculus using MATLAB 11 - expanding and collecting of expressions in Octave


Expanding and Collecting Equations in Octave


You need to have symbolic package, which provides expand and collect command to expand and collect an equation, respectively. The following example demonstrates the concepts:

When you work with many symbolic functions, you should declare that your variables are symbolic but Octave has different approach to define symbolic variables. Notice the use of Sin and Cos which are also defined in symbolic package.

Create a script file and type the following code:


% first of all load the package, make sure it’s installed.

pkg load symbolic

% make symbols module available

symbols

% define symbolic variables
x = sym ('x');
y = sym ('y');
z = sym ('z');

% 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), z)
collect(x^4*(x-3)*(x-5), z)

When you run the file, it displays the following result:


ans =
-45.0+x^2+(4.0)*x

ans =
210.0+x^4-(43.0)*x^2+x^3+(23.0)*x

ans =
sin((2.0)*x)

ans =
cos(y+x)

ans =
x^(3.0)*(-7.0+x)

ans =
(-3.0+x)*x^(4.0)*(-5.0+x)


No comments:

Post a Comment