Intro Programming Using Matlab
Intro Programming Using Matlab
I developed this tutorial to emphasize some essential programming skills. I have written a more
comprehensive self-guided tutorial that also demonstrates more of the graphics (on the REU website).
b. You can define variables that will last until you clear them.
<<You Try>> Type m = 7 and press enter.
m+2
m
m^2
m = m^2
clear m
This should give you some idea of how m is stored and how it can be used or changed.
c. You can also define variables that are matrices (called 'arrays'). (You may copy and paste.)
<<You Try>> Type A = [7, 8, 9; 10, 11, 12] and press enter.
A(2, 3)
B = [0:2:10]
C = transpose(1:2:11)
dot(B, C)
D = rand(6, 6)
E = linspace(0, 5, 6)
E * E'
E' * E
F = exp(E)
zeros(5, 9)
ones(11, 5)
This shows just a few ways to make and use arrays (matrix variables).
PRACTICE 1: Find the natural logarithm for all the numbers between 0 and 100 using arrays.
Show your answer to a mentor.
d. MatLab has a powerful graphics tool.
There is much more about graphing in the other tutorial, but this will be all you need for
today.
----------------------------------------------------------------------------------------------------------------------------
<<You Try>> - Create a folder on your desktop or thumb drive called something like
'MyMatLab' or whatever.
- Now set the MatLab's 'Current Folder' to your newly made folder by
clicking on the ellipses button '…' in the middle of the control toolbar
on the top-middle of screen.
- Create a new m-file by clicking 'File > New > Blank M-File'
- Save this (initially blank) file as 'PracticeScript.m' in your new folder.
Note that MatLab program names being with a letter and end in '.m'.
- Now copy and paste the following text into your m-file:
<<You Try>> - Replace the previous m-file content with the following:
close all;
clear all;
user_input = input('Are you happy? (''1'' for Y, ''2'' for N.) \n');
% If you want to single quote to appear as text, you
% have to type two of them!
if user_input == 1,
disp('Oh brother!');
elseif user_input == 2,
disp('Boo hoo.');
else,
disp('Wrong answer!');
end;
% We would need to do something fancier to prevent errors
% when something other than a number is typed.
- Press F5.
- Replace the previous m-file content with the following:
% Tossing a coin.
close all;
clear all;
clc; % Clears the current screen.
x = 2*rand(1,1); % Random number between 0 and 2.
if x >= 1,
disp('Heads!')
else,
disp('Tails!');
end;
- Press F5.
PRACTICE 2: Write a MatLab program that asks the user for their favorite number and then
insults them if you don't like their choice for some reason. Save this file as 'Practice2.m' in your
MatLab folder. Test it on your neighbor! Then show an REU mentor.
c. Numbered loops are useful when you have a specific number of repetitive commands for the
computer to do.
<<You Try>> - Replace the previous m-file content with the following:
close all;
clear all;
clc;
sum_integers = 0;
for i_loop = 1:100,
sum_integers = sum_integers + i_loop;
end;
disp(['The sum of 1 through 100 is ',num2str(sum_integers)]);
% 'num2str' converts a number to text (string) for printing.
- Press F5.
- Replace the previous m-file content with the following:
- Press F5.
PRACTICE 3: Write a MatLab program that calculates the first 50 integers of the Fibonacci
sequence. Save this program as 'Practice3.m' in your MatLab folder. Write down the 50th integer
in the Fibonacci sequence here to check with your neighbors and mentor. Note: 1, 1, 2, 3, 5, …
<<You Try>> - Replace the previous m-file content with the following:
- Press F5.
PRACTICE 4: Write a MatLab program that asks the user to guess your favorite number between
1 and 20, and does not stop until they get it. Test it on your neighbor to see anyone is psychic.
Don't forget to berate them for their ineptitude when they guess incorrectly! Save this file as
'Practice4.m' in your MatLab folder.