Purpose: This exercise is a brief introduction to MATLAB for students who want to use MATLAB for research projects. You will familiarize yourself with the MATLAB command line and editor. You will learn the difference between a script and a function. Some programming elements such as loops and conditional statements are introduced as well as how to generate plots and obtain help within MATLAB.
The Command Line
Perform the follow tasks and note the output in the Command Window.
1] Assign values to variables and perform basic arithmetic by typing the following in the Command Window.
>> a = 5
>> b = -3
>> c = a*b – (a+b)
What happens if you include semicolons at the end of each command?
2] Create two vectors and calculate the inner product.
>> a = [1 2 3 4]
>> b = [1; 2; 3; 4]
>> c = a*b
What is the purpose of the semicolons inside of the vector definition?
3] Create two matrices and find the matrix product and matrix inverse.
>> A = [1 2; 3 4]
>> B = [5 6; 7 8]
>> C = A*B
>> D = A.*B
>> E = inv(A)
What is the difference between how C and D are calculated? What does the .* notation indicate should be done?
The Editor and Writing a Script
1] Open the MATLAB editor by typing the following in the Command Window.
2] Type the following into a blank file and save it as myfirstscript.m
A = [1 2 3; 2 2 4; 5 4 6]
B = [1; 4; 2]
x = inv(A)*B
3] Click the button that looks like a green play button on the editor toolbar and note the output in the Command Window.
4] Type myfirstscript in the Command Window and press return and note the output. Make sure that the Current Directory drop down box is pointing to the directory where myfirstscript.m is saved.
Writing and Calling a Function
1] Open the MATLAB editor.
2] Type the following into a blank file and save it as calcPi.m
function [x, error] = calcPi(n)
% The percent sign is used to add comments to a script of function.
% This function takes one input argument, n, and produces two outputs. All three % scalars.
% The function estimates the value of pi using the random number generator.
% Two random numbers uniformly distributed from zero to one are generated.
% These numbers represent the coordinates of a point in quadrant one of a
% Cartesian axis. If this point falls within the unit circle then iterate n1 and n2. If
% not then iterate only n2. The ratio n1/n2 converges to A1/A2 for large n where
% A1 is the area of the quarter unit circle in quadrant one and A2 is the area of a
% 1-by-1 square in quadrant one. This ratio is proportional to pi. The
% convergence gets better as n, the number of iteration used to estimate
% A1/A2, gets large. The percent error (error) between the estimate and the
% actual value of pi is calculated too.
n1 = 0;
n2 = 0;
for ii = 1:n
a = rand(1);
b = rand(1);
if sqrt(a^2 + b^2) < 1 n1 = n1+1; n2 = n2+1; else n2 = n2+1; end end x = 4*n1/n2; % estimate of pi error = (pi – x)/(pi); return
3] Type the following in the Command Window and press return and note the output. Make sure that the Current Directory drop down box is pointing to the directory where calcPi.m is saved.
>> [x, y] = calcPi(100);
Does x converge to the actual value of pi as the input argument n gets large?
Using Plotting Commands
1] Type the following at the command prompt.
>> x = -5:0.1:5;
>> y = exp(x);
What does the ‘k’, ‘o’, and ‘:’ options do in the plot command?
What does the colon notation in the definition of x do?
>> y2 = sin(x);
>> hold on;
What does the figure command do?
What does the hold on command do?
1] MATLAB contains extensive help files. At the command prompt type:
>> help while
What is the syntax to write a while loop? Write a script that uses a while loop to spit out in the Command Window all of the elements of the Fibonacci sequence less then 1,000,000. The Fibonacci sequence is
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …,
where each element (after the first two) is the sum of the two preceding elements.
There is also an HTML based repository of help documentation in MATLAB. This can be searched using keywords.
2] Write a script that uses the calcPi function your wrote to estimate pi for increasing values of n. The script should also plot on the same axes the estimated values of pi and the percent errors as a function of n. Add a title, axis labels, and a legend to your plot.