Thursday, December 30, 2010

Lesson 2

Today's Blog is a collection of snippets from the following MATLAB Tutorial:

http://www.staff.ncl.ac.uk/damian.giaouris/pdf/Matlab/MatlabTutorial%20%207th%20edition.pdf

Your assignment is to make an example from each snippet and post it in the comments. Fabricate or construct the data you are operating on as well.

In some regards MATLAB is just a glorified calculator some of the common math functions are listed below:


A challenge in writing a large piece of code is to have logical variable names. There are several names which you cannot use and several which you should (but can) use:


Use one of the following Relational Operators in an example in the comments below:


Use one of the following Logical Operators in an example in the comments below:


I rarely use these but may come in useful sometimes. Additional Logical Operators:


Are you familiar with any of the following Linear Algebra Functions? If so make an example in the comments below.


For each example make sure to include the result from the MATLAB command prompt.

-Richard

Tuesday, December 28, 2010

Lesson 1

%Lesson One - 12/24/2010
% Plotting, labeling, concatenation of strings and output to command prompt

%Getting help: You can open the MATLAB documentation by typing 'doc' at
%the command propt. If you want to jump directly to a specifc page of the
%documentation you can type 'doc <function>' where <function> is a valid
%MATLAB function, for example 'doc plot'. Also you can get help at the
%command line by typing 'help plot'.


%clear the variables in the work space. I want to make sure that I can start fresh
clear;

%I am always curious about how long it takes to run a script. 'tic' starts
%the timer, and 'toc' at the end of the file will report out the elapsed
%time.
tic

% There are always many ways of doing any one operation. Knowing your
% options will help you know what is best for each particular application.
% Below we will explore several different ways of doing some actions.

%Creating the time vector with 'linspace'. This function will create a
%linearly space vector from -2 to 3 in 101 steps. see 'doc linspace' for
%more details
t1 = linspace(-2,3,101);

%Create the time vector with the colon operator. see 'help colon' for more
%information. The colon operator is one of the most useful and powerful
%aspects of MATLAB. Learning how to use the colon operatior is essential
%to being a good MATLAB programmer.
%Below creates a vector, starting at -2, incrementing in steps of 0.05 to
%3.
t2 = -2:0.05:3;

%We want to compare the vectors 't1' and 't2' and see if they are identical
%or not.

%First lets look at the size of the vectors. The 'size' command returns
%the length of of each dimension of the vector. Below we will see that the
%vector 't1' is a vector composed of one row and 101 columns

t1_size = size(t1) %A semicolon suppresses output to the command prompt.
t2_size = size(t2);

%The vector 't2_size' vector with two entries. Below we will use indexing
%to select what parts of the vector we want. Indexing is as important as
%the colon operator and we will use it extensively.
t2_size_dim1 = t2_size(1); %first entry in the vector 't2_size'
t2_size_dim2 = t2_size(2); %second entry

%Below is a bit of advanced code. We use the '[' and ']' brackets to
%concatenate a series of strings together and then use the 'disp' or
%display function to output this string to the command prompt.
%Concatenation means to squash things together and is something I use
%everyday in my MATLAB coding. I also used the function 'num2str' or
%number to string to convert the two numbers to strings. Elements must be
%of the same 'type' to be able to be concatenated together.
disp(['t2_size = ',num2str(t2_size_dim1),' x ',num2str(t2_size_dim2)])

%Below is an alternative way of seeing the properties of both t1 and t2
%using the 'whos' command.
whos t1 t2

%type 'whos' at the command prompt now to see the information of all the
%variables in your workspace.


%Ok, we see that the two vectors are the same dimension which is necessary
%for them to be identical. I will show two ways of comparing the contents
%of the two vectors 't1' and t2'

%subtract one vector from the other to find the other to find the
%'distance' between the two vectors at each of the 101 entries.
t_difference = t1-t2;

%Use a logical comparitor '==' to check for equilvalence. A '1' in the
%'t_compare' vector means that the two respective entries are identical and
%a '0' means that they are different.
t_compare = t1==t2;

%Let's visualize these two comparison approaches.

figure(1), %create a figure window with handle of '1'
        stem(t_compare) %create a 'stem' plot of the vector
        xlabel('index number') %add the xlabel to the axis
        ylabel('0= different, 1= identical') %add the y label to the axis
        title('Logical comparison of two ways of creating a time vector') %add a title to the axes



figure(2), %create a figure window with a handle of '2'
        stem(t_difference)
        xlabel('index number')
        ylabel('Distance between the two vectors')
        title('Difference comparison of two ways of creating a time vector')

%Ok, the difference plot is much more informative than the equilvalence
%plot. But note the magnitude of the difference, it's really small.

%Use the 'max' function to find the maximum difference. Note that I first
%take the absolute value of the vector so I can obtain hte true max
%difference.
max_difference = max(abs(t_difference))

%This difference is on the order of magnitude of the precision of your
%computer. Your computer does not have infinite precision in representing
%numbers but must round them off at some point. To understand the floating
%point accuracy of your computer you can use the following command.

float_point_accuracy = eps

%You can see that the max difference of the time vectors is near to the
%machines floating point accuracy so for all but a few advanced application, the
%vectors 't1' and 't2' should be considered identical.

%So what did you learn? Please take a minute to comment on the blog on
%some of the things you learned, what you already knew and any questions or
%topics you would like to discuss in the future.

%-Richard

toc