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)
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 X, Y, 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 X, Y, 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.
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)
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.
The bar command draws a two dimensional bar chart. Let us take up an example to demonstrate the idea.
Example
Let us have an imaginary classroom with 10 students. We know the percent of marks obtained by these students are 75, 58, 90, 87, 50, 85, 92, 75, 60 and 95. We will draw the bar chart for this data.
MATLAB provides eight basic color options for drawing graphs. The following table shows the colors and their codes:
w- White k - Black b - Blue r - Red c - Cyan g - Green m - Magenta y - Yellow
Example
Plot three sine curves with a small phase shift between each line. Use a green line with no markers for the first sine curve. Use a blue dashed line with circle markers for the second sine curve. Use only cyan star markers for the third sine curve.
Create a script file and type the following code into it:
str1 = 'This is test' str2 = 'This is text' if (strcmp(str1, str2)) sprintf('%s and %s are equal', str1, str2) else sprintf('%s and %s are not equal', str1, str2) end
When you run the file, it displays the following result:
str1 = This is test str2 = This is text ans = This is test and This is text are not equal
Create a script file and type the following code into it:
x = [7.1, 3.4, 7.2, 28/4, 3.6, 17, 9.4, 8.9]; length(x) % length of x vector y = rand(3, 4, 5, 2); ndims(y) % no of dimensions in array y s = ['Zara', 'Nuha', 'Shamim', 'Riz', 'Shadab']; numel(s) % no of elements in s
When you run the file, it displays the following result:
ans = 8 ans = 4 ans = 23
Circular Shifting of the Array Elements:
Create a script file and type the following code into it:
a = [1 2 3; 4 5 6; 7 8 9] % the original array a b = circshift(a,1) % circular shift first dimension values down by 1. c = circshift(a,[1 -1]) % circular shift first dimension values % down by 1 % and second dimension values to the left % by 1.
When you run the file, it displays the following result:
Create a script file and type the following code in it:
A = [1 2 3 4; 4 5 6 7; 7 8 9 10] A(:,2) % second column of A A(:,2:3) % second and third column of A A(2:3,2:3) % second and third rows and second and third columns
When you run the file, it displays the following result:
A = 1 2 3 4 4 5 6 7 7 8 9 10 ans = 2 5 8 ans = 2 3 5 6 8 9 ans = 5 6 8 9
You can use the colon operator to create a vector of indices to select rows, columns or elements of arrays.
The following table describes its use for this purpose (let us have a matrix A):
A(:,j) - is the jth column of A.
A(i,:) - is the ith row of A.
A(:,:) - is the equivalent two-dimensional array. For matrices this is the same as A.
A(j:k) - is A(j), A(j+1),...,A(k).
A(:,j:k) - is A(:,j), A(:,j+1),...,A(:,k).
A(:,:,k) - is the kth page of three-dimensional array A.
A(i,j,k,:) - is a vector in four-dimensional array A. The vector includes A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.
A(:) - is all the elements of A, regarded as a single column. On the left side of an assignment statement, A(:) fills A, preserving its shape from before. In this case, the right side must contain the same number of elements as A.
Create a script file and type the following code into it:
v = [ 23 45 12 9 5 0 19 17] % horizonal vector sort(v) %sorting v m = [2 6 4; 5 3 9; 2 0 1] % two dimensional array sort(m, 1) % sorting m along the row sort(m, 2) % sorting m along the column
When you run the file, it displays the following result: