Tuesday, 9 August 2016

Calculus using MATLAB 12 - factorization of an expression


Factorization of Algebraic Expressions


The factor function factorizes an expression. The following example demonstrates the concept:

Example


Create a script file and type the following code:


syms x
syms y
factor(x^3 - y^3)
factor([x^2-y^2,x^3+y^3])

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


ans =
(x - y)*(x^2 + x*y + y^2)

ans =
[ (x - y)*(x + y), (x + y)*(x^2 - x*y + y^2)]


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)


Calculus using MATLAB 11 - expanding and collecting of expressions


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



Sunday, 7 August 2016

MATLAB Programming 52 - 3D plots of functions


Three-Dimensional Plots


Three-dimensional plots basically display a surface defined by a function in two variables, g = f (x,y).
As before, to define g, we first create a set of (x,y) points over the domain of the function using the meshgrid command. Next, we assign the function itself. Finally, we use the surf command to create a surface plot.

The following example demonstrates the concept:


Use the peaks function to define XY, and Z as 25-by-25 matrices. Then, create a surface plot.



[X,Y,Z] = peaks(25);

figure
surf(X,Y,Z);


surf creates the surface plot from corresponding values in XY, and Z. If you do not define the color data C, then surf uses Z to determine the color, so color is proportional to surface height.




Friday, 5 August 2016

MATLAB Programming 51 - how to draw contour


A contour line of a function of two variables is a curve along which the function has a constant value. Contour lines are used for creating contour maps by joining points of equal elevation above a given level, such as mean sea level.

MATLAB provides a contour function for drawing contour maps.

Example


Let us generate a contour map that shows the contour lines for a given function g = f(x, y). This function has two variables. So, we will have to generate two independent variables, i.e., two data sets x and y. This is done by calling the meshgrid command.

The meshgrid command is used for generating a matrix of elements that give the range over x and y along with the specification of increment in each case.


Use the meshgrid function to generate matrices X and Y. Create a third matrix, Z, and plot its contours.



x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);

figure
contour(X,Y,Z)




MATLAB Programming 50 - bar charts (Example)

Set the bar interior color and outline color using RGB triplets. Set the width of the bar outline



y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)


Create a bar graph with a three-column matrix input and return the three bar series objects. bar creates one bar series for each column in the matrix.


y = [2 4 6; 3 4 5];
b = bar(y);


Change properties for a specific bar series by indexing into the object array. For example, change properties of the bars representing the second column of y using b(2). Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.


b(2).LineWidth = 2;
b(2).EdgeColor = 'red';




MATLAB Programming 50 - bar charts (Example)

Display one bar for each row of the matrix. The height of each bar is the sum of the elements in the row.



y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'stacked')



Create a figure with two subplots. In the upper subplot, plot a bar graph. In the lower subplot, plot a stacked bar graph of the same data.


y = [1 2 3; 4 5 6];
ax1 = subplot(2,1,1);
bar(ax1,y)

ax2 = subplot(2,1,2);
bar(ax2,y,'stacked')


Create a bar graph using red bars


y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'r')