## MATLAB : A SHORT TUTORIAL INTRODUCTION

MATLAB : A SHORT TUTORIAL INTRODUCTION MATLAB is a convenient program to graph functions or perform computations. Its strength is its simple format an...
Author: Arron Kelley
MATLAB : A SHORT TUTORIAL INTRODUCTION MATLAB is a convenient program to graph functions or perform computations. Its strength is its simple format and its speed in performing vector and matrix operations. We introduce you to it in Math 162/163 to give you an option for graphing and performing tedious computations easily. MATLAB is now available for free for all UNM students (usually a \$1000 value). It is installed in all pod computers. You can download it onto your laptop or home computer. Follow the instructions on http : //it.unm.edu/download. You are allowed to install it on two devices. Once installed on your computer, you run it by clicking on it (Mac,Windows) or typing matlab (Linux.) Below are some simple MATLAB commands with examples. These commands can be typed directly into MATLABs Command Window, at the prompt given by the double arrows, >>. Alternatively, you can write all of these commands in a file called a script, whose name must end in a .m (thus it is also called an m-file), and then execute the commands in MATLAB by typing in the name of the file (without the .m). For example, if you have a file called plotsin.m containing the 3 lines: x=linspace(0,pi,100); y=sin(x); plot(x,y) then you can execute it by typing >> plotsin at the prompt. On our course website there are some simple example scripts posted. Run them to see what they do. The best way to learn MATLAB is by seeing examples and trying to modify them. Out of the information below, what is most useful for work in this class are really sections 1-4. Section 5 and 6, and the expanded tutorial posted on the web, are included only for your information.

1. Creating vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2. Functions, the inline command, manipulating vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3. Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 4. Miscellaneous commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5. The for statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 6. The if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1

1. Creating Vectors Row vectors: there are many ways of creating a vector Explicit list >> x=[0 1 2 3 4 5];

% What happens if you skip the semicolon?

>> x=[0,1,2,3,4,5];

% Inserting commas doesnt change anything

Using a:increment:b >> x= 0:0.2:1; >> x= 3:-1:0; >> x= 1:3; >> x= a:∆x:b;

% same as x=[0 0.2 0.4 0.6 0.8 1.0], % same as x=[3 2 1 0]; % same as x=1:1:3 same as x=[1 2 3]; (that is, default increment is 1) % x=[a,a+∆x, a+2∆x, a+3∆x, . . . , b] % that is, vector from a to b in increments of size ∆x % What happens if ∆x is not a integer divisor of b-a?

Using linspace(a,b,n) >> x= linspace(0,1,6);

% vector containing 6 points on interval [0,1]

>> a=0;b=1;n=6; >> x= linspace(a,b,n);

% Set variables % vector containing n points on interval [a,b] % Note: spacing is ∆x = 1/(n − 1)!

Column vectors Explicit list >> x=[0;1;2;3;4] Transposing a row vector >> x=[0 1 2 3 4]’

% Vectors are matrices.

A’=transpose(A)

2. Functions, the inline command, manipulating vectors Example >> x=0:0.1:1; >> y=sin(pi*x);

% Type help elfun to see a list of predefined functions

Alternative, using the inline command to define function >> f=inline(’sin(pi*x)’); >> x=0:0.1:1; >> y=f(x); Vectors >> >> >> >>

are matrices y=x*x; x2=0:0.2:1; y=x+x2; y=x’*x y=x*x’

Componentwise operation >> y=x.*x >> y=x.^3 >> y=2*x >> y=1./x

% % % %

% % % %

What What What What

happens? happens? is y ? is y ?

Why? Why?

The dot denotes multiplication of components The carat denotes exponentiation Here you dont need a dot Here you do 2

3. Plotting Plot command. Examples and Exercises: >> x=0:.1:1; y =sin(2*pi*x); >> plot(x,y);

% the two vectors have to have same dimensions

>> x=[0,1]; y=sin(2*pi*x); >> plot(x,y);

% What is going on??

Options Line type options: -,:,--,-. >> plot(x,y,’-’); >> plot(x,y,’:’); >> plot(x,y,’--’); >> plot(x,y,’-.’); Color options: y,m,c,r,g,b,w,k >> plot(x,y,’g’); >> plot(x,y,’r’)

% green line (line is default)

Marker options: .,o,x,+,*,s,d,v,^,,p,h (type help plot) >> plot(x,y,’x’); % blue star (blue is default) Using several options together >> plot(x,y,’*-r’);

% red line with star markers

Plotting several curves >> x=0:0.05:1; y1=sin(2*pi*x); y2=cos(2*pi*x); >> plot(x,y1,x,y2) >> plot(x,y1,’-b’,x,y2,’--r’) >> x=0:0.05:2; y1=x; y2=x.^2; y3=x.^3; y4=x.^4; >> plot(x,y1,’-b’,x,y2,’--r’,x,y3,’*g’,x,y4,’-c’) Alternative, using hold command >> x=0:0.05:1; y1=sin(2*pi*x); y2=cos(2*pi*x); >> plot(x,y1,’-b’) >> hold on >> plot(x,y2,’--r’) >> hold off The axis command >> axis([0,2,0,4]) >> axis equal >> axis square

% Use ’help axis’ to see what other options there are

Labelling >> xlabel(’time t’) >> ylabel(’position s(t)’) >> ylabel(’position s(t)’,’FontSize’,16) >> title(’Its important to label your graphs!’) >> text(0.6, 2,’some text’,’FontSize’,16) >> set(gca,’FontSize’,16) >> legend(’x’,’x^2’) 3

4. Miscellaneous commands Comments >> % This is a comment The help and lookfor commands >> >> >> >>

help zeros help for help lookfor factorial

The print command >> print >> print -deps >> print -depsc >> print -dps

% you need to know exact command name % lists topics for which there is help % if you do not know the exact command name

% % % %

prints prints prints prints

current current current current

figure figure figure figure

to to to to

current printer .eps file color .eps file .ps file

The figure command >> figure >> figure(2) The pause command >> pause >> pause(2) The continuation symbol >> x=[0 1 2 3 4 5 ... >> 6 7 8 9 10]

% opens new figure % makes figure 2 the current figure

% What does this do? % What does this do?

% To continue the current command % to the next line, use ...

The clear command >> clear >> clear x y ...

% clears all variables from memory % clears listed variables from memory

The clf command >> clf

% clears current figure

5. The for statement >> >> >> >> >> >> >> >> >>

% The command for repeats statements for a specific number of times. % The general form of the while statement is FOR variable=expr statements END % expr is often of the form i0:j0 or i0:l:j0. % Negative steps l are allowed.

Example 1: What does this code do? >> n = 10; >> for i=1:n 4

>> for j=1:n >> a(i,j) = 1/(i+j-1); >> end >> end 6. The if statement >> >> >> >> >> >> >> >> >> >> >> >> >> >>

% The general form of the if statement is IF expression statement ELSEIF expression statement ELSE expression statement END % where the ELSE and ELSEIF % The expression is usually % a oper % where oper is == (equal),

parts are optional. of the form b , =, or ~= (not equal).

Example 1: What does this code do? >> n=10; >> for i=1:n >> for j=1:n >> if i == j >> A(i,j) = 2; >> elseif abs(i-j) == 1 >> A(i,j) = -1; >> else >> A(i,j) = 0; >> end >> end >> end >> >> >> >> >> >>

% You can also combine two expressions % with the and, or, and not operations. % % expression oper2 expression % % where oper2 is & (and), | (or), ~ (not).

Example 3: What does this code do? >> for i=1:10 >> if (i > 5) & (rem(i,2)==0) >> x(i)=1; >> else >> x(i)=0; >> end >> end 5