Friday, April 5, 2013

MATLAB


In this laboratory assignment, we were introduced to a programming language called MATLAB, which easily allows one to perform complex mathematical operations such as matrix and vector operations with relative ease. We used a variant of MATLAB called FREEMAT.

First we we instructed to do specific arithmetic operations involving addition, subtraction, multiplication, division.



Next we were instructed to perform specific operations with mathematical functions such with a square root and some trigonometric functions. We noted that the input values for the trigonometric functions must be given in radians.


Next we were instructed to purposely input an expression such as 3=4 that would give an error message on the output. Furthermore, we were told that the up arrow key would allow us to redo the expression. Thus, we edited 3=4 to 3+4.


Then we were instructed how to use the help menu. As an example, we typed in "help" followed by "sin", in order for us to receive an instructional output regarding the use of this trigonometric function.


Next, we were instructed to create a variable and assign a value to it. This is very similar to how it's done in programming languages such as in Java. It is simply done by typing in a variable, followed by an equal sign, and then the numerical value of the variable. We noted that variables are case sensitive and that variables of specific functions cannot be used. We were also shown that typing in a semicolon after assigning a value to a variable would not be repeated in the output screen.


Next, we were instructed to do intermediate level operations with vectors and matrices. First we were instructed how to create a matrix. Commas were used to separate entries in a specific row, whereas semicolons are used to create a new row. Here is the specific example that we were instructed to do.

Next, we were told to create automatic arrays using the general formula A=x:y:z, where x is the variable for the initial entry, z is the variable for the final entry, and y is the increment for the sequence of numbers. Here is the example that we were asked to work on.  Note that I was unable to fit the entire sequence on the screen.


Next, we were instructed to transpose a matrix. A single quotation mark (') is used to transpose matrices. In our first specific example for this part, we were asked to create a variable that was a row vector and then to create a second variable which was the transpose of the first matrix, which should output a column vector. Next, we were asked to create a variable "xx" and to assign an arbitrary 2X3 matrix to it, followed by creating the matrix "xx'', which outputted a 3X2 transpose matrix.


Next, we were asked to apply functions to a row vector. In other words, we were asked to compose  functions from another function. Here are the specific examples that we were asked to work on.

Now, for the fun part, we were asked to perform operations on vectors. We were also instructed that ".*", "./", and ".^" performs scalar multiplication, scalar division and scalar exponentiation, respectively. from the value "x" from the previous example, we were asked to define and compute CC=cos(x)*sin(x) and DD=sin(x).*cos(x). We received an error message for variable CC, because a period was not included before the multiplication symbol.



Next, we asked to enter commands that would give parts of a matrix, or submatrices. Specifically, we were asked to create a matrix, followed by creating a submatrix for the first row, first column, first two rows, and the last two columns, respectively.

Next, we were asked to create some simple plots. We were instructed to create a variable "degree", which was a row vector with the parameters 0:2*pi/100:2*pi. Then we were to create a second variable "output", which was a function of "degree". Then by typing in the command "plot(degree, output)", a graph of the function appeared on the screen.


Next we asked to create multiple graphs on the same screen by implementing the "hold on" function and adding a cosine function.

We were then asked to enter the "hold off" command an create the original sine function. Then we were asked again to enter the both the sine and cosine functions by entering them through a single command.
Next, we were required to create script files by using the text editor to perform commands. After following the list of instructions in doing so, I saved it and created a variable "a=2" in the regular command screen and clicked on the "Execute Current Buffer" icon on the text editor menu.

We then set a=6 and we received similar results.

Next, we were given instructions on how to solve a specific set of simultaneous equations.  Here are the results that we obtained.

Next, we were given a circuit with resistance values for some resistors and some voltage values for some voltage sources, and were asked to find the current through the middle resistors. This could be done by setting up some mesh equations and solving for the unknowns. Here is what we got:


Our next assignment was to plot two exponential functions of the form 2e^(-t/tau). Using the given instructions I entered the required variable assignments, I came up with these graphs. The variable assignments are listed underneath the graph.


Next we had to plot two more exponential functions of the form 2*(1-e^(-t/tau)). Here is the the graphs along with the additional assignment variable statements underneath the graph in the photo.



Our next assignment was to plot the trigonometric function 3sin(2t+10^0)+5cos(2t-30^0). Here is the assignment statements along with the corresponding graphs.



Our final assignment was to create a script file so that we could create the graphs using a desired frequency, such as f=10. Here is a photo of the script file and it's corresponding graph after freq=10 was entered in the regular prompt screen.




As a final thought, even though it was very time consuming and required an entire day to perform, I thought that this lab was the funnest lab assignment to work with all semester long. I learned how to solve problems and perform mathematical operations with MATLAB!

No comments:

Post a Comment