Saturday, January 15, 2011

Lesson 3: 3D Plotting Part 1

clear; tic

% Title: 3D plotting of x*exp(-x^2-y^2)
% Question: What is necessary to represent a surface in three dimensions?

% Step 1) Create the matrix you would like to visualize.

Nx = 18;
Ny = 20;

x = linspace(-3,3,Nx);
y = linspace(-2,2,Ny);

% Method 1 of creating surface matrix
Z1 = zeros(Nx,Ny);
for ii = 1:length(x)
        for jj = 1:Ny
               Z1(ii,jj) = x(ii)*exp(-x(ii)^2-y(jj)^2);
        end
end

% Method 2 of creating surface matrix
[X1, Y1] = ndgrid(x,y); % We will discuss 'ndgrid' and 'meshgrid' in the future
Z2 = X1.*exp(-X1.^2-Y1.^2); %Question: What are the '.' for? See note 1 below.

% Question: What do you think of method 1 and method 2 to create matrix Z?
% What are the merits of each method? What are the downsides of each
% method?

% Note 1:
% MATLAB stands for Matrix Labratory so basic functions like *, / and ^ assume
% that they are operating on matricies. Above we don't want a matrix
% multiplication but a point by point multiplication. Same with the square
% function, we want to do that point by point.
%
% I typed 'doc *' and it brought me the list of arithmetic operators.
% plus - Plus +
% uplus - Unary plus +
% minus - Minus -
% uminus - Unary minus -
% mtimes - Matrix multiply *
% times - Array multiply .*
% mpower - Matrix power ^
% power - Array power .^
% mldivide - Backslash or left matrix divide \
% mrdivide - Slash or right matrix divide /
% ldivide - Left array divide .\
% rdivide - Right array divide ./

zdiff = Z1-Z2;
maxZdiff = max(zdiff(:)) % We will discuss the max functions in a future lesson.

% Plot the surfaces


% Here is how you get a quick view of a surface but you aren't always sure
% of the axis.
figure(22),
        surf(Z1)
        zlabel('z')
        title('x*e^{-x^2-y^2}')

%Make sure you play with all the figure window buttons. Zoom, pan, rotate, etc

% Below are equilvalent ways of plotting the surface:
figure(23),
surf(y,x,Z1)
xlabel('y'),ylabel('x'),zlabel('z')
title('x*e^{-x^2-y^2}')

figure(24),
        surf(x,y,Z1.')
        xlabel('x'),ylabel('y'),zlabel('z')
        title('x*e^{-x^2-y^2}')


figure(25),
        surf(X1,Y1,Z2)
        xlabel('x'),ylabel('y'),zlabel('z')
        title('surf plot with matrix inputs x*e^{-x^2-y^2}')

% Below are some other plotting options which we will go into more details
% in the future.

figure(26)
        surfc(X1,Y1,Z2)
        xlabel('x'),ylabel('y'),zlabel('z')
        title('surf plot with contours x*e^{-x^2-y^2}')

figure(27)
        surf(X1,Y1,Z2,'edgecolor','none')
        xlabel('x'),ylabel('y'),zlabel('z')
        title('surf plot with no mesh x*e^{-x^2-y^2}')

figure(28)
        mesh(X1,Y1,Z2)
        xlabel('x'),ylabel('y'),zlabel('z')
        title('mesh plot of x*e^{-x^2-y^2}')

figure(29)
        surfl(X1,Y1,Z2)
        xlabel('x'),ylabel('y'),zlabel('z')
        title('surfl plot of x*e^{-x^2-y^2}')
        shading interp
        colormap('copper')

% Question: What did you like about this lesson? What would you like to
% learn more about?


toc

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