You are on page 1of 4

MATLAB basics 205B, Winter 2008 Carl Walsh This document provides a very basic introduction to commands in MATLAB.

Launch the MATLAB program. In the Command Window, MATLAB commands can be entered at the command prompt >> or can be collected in a simple text file with extension m (e.g. myprogam.m) and then executed at the command prompt by typing >> myprogram followed by the enter key. The m file must be in the current path for MATLAB to find it. IMPORTANT: Conventions: MATLAB distinguishes between upper case and lower case letters, so A and a are difference variable to MATLAB. Basic functions and operations: Define a scaler: >>a = 2 Compare the results of the following: >>a = 2 >>a = 2; If the command ends with a semi colon :, MATLAB does not return any output to the window. NOTE: To add a comment to the command line (very useful when using batch files), precede the comment with %. Add, subtract, multiple, divide, and raise to an exponent: >> a = 2; >> b = 3: >> a + b >> a b >> a*b >> a/b >> a^b Define a row vector: >> A = [1 2 3 4] Define a column vector: >>A = [1; 2; 3; 4] Define a 2x10 matrix of zeros: >>C= zeros(2,10) Define a 3 x 4 matrix of ones: >>B = ones(3,4) Check its dimension: >>size(B)

or >>A = [ 1 2 3 4]

Try the following: >>[nr nc] = size(B); >>nr >>nc >>nnc = size(B,2); >>nnc Assign an individual element of a matrix: >>B(2,3) = 2; >>B Display a row or column of B together with a heading: >>disp(This is the third row of B) >>B(3,:) >>disp(This is the 4th column) >>B(:,4) Matrix multiplication >>A = [1 2 ; 4 8; 6 12; 8 16] >>B*A See what happens if you try >>A*B Matrix multiplication versus element by element operations: >>F = 2*ones(4,4) Now compare >>F*F >>F^2 >>F.^2 .^ raises each element of F to the power 2, F^2 squares the matrix. Use the random number generator to create some matrices: >>C = rand(4,4) >>D = rand(4,4) Invert C >>inv(C) >>C*inv(C) Raise each element of C to the power of the corresponding element of D (i.e., C(i,j)^D(I,j): >>C.^D Getting help on functions examples: >>help zeros >>help ones >>help eig Controlling the flow: >>for i = 1:10

>> disp(i) >>end >>x=0 >>for i = 1:10 >> x(i) = 2*i; >>end >>x >>A = zero(5,5); >>for i=1:5; >> for j = 1:5; >> A(i,j) = i^j; >> end >>end >>A Creating a function file; Save the following in a simple text file with name stat.m (Use the built in text editor in MATLAB.) function [mx, sx] = stat(x) % function [mx, sx] = stat(x) % computes mean and standard deviation of vector x mx = mean(x); sx = std(x); Save the function as stat.m, then back in the MATLAB command window, type >> help stat (See how this returns anything you have entered preceded by an % at the beginning of an *.m file.) Now use this new function: >>z = rand(25,1) ; >>[mz sz] = stat(z) Convergence and equation solving: The following provides an example of iterating to find the solution to an equation as a fixed point. This example is very simple since the equation being solved (d = 10 + 0.5*d) is linear with solution d = 10/(1-.5) = 20. The code starts with a guess (d0 = 10), calculates a new value for d (d1 = 10 + 0.5*d0), checks whether this new d is close enough to the initial d0 (Vdiff = (d1-d0)^2 and stops if Vdiff < 1E-8. If not, it sets d0 equal to the new value of d (d1) and repeats until convergence or Nmax iterations have occurred. If it reaches Nmax before converging, then flag1 is set equal to 1 and Vdiff, d0, d1, and flga1 are displayed. Enter this code in a text file and name it simplesol.m. (MATLAB has a built in text editor.) >>Nmax = 30; >>Vdiff = 100; >>j = 1; >>d0 = 10; >>while Vdiff > 1E-8;

>> d1 = 10 + 0.5*d0; >> Vdiff = (d1 d0)*2; >> if j > Nmax; >> flag1 = 1; >> disp(Nmax iterations reached without convergence) >> disp(Vdiff, do, d1, flag1) >> [Vdiff [d0 d1 flag1] >> end; >> d0 = d1; >> j = j+1; >>end >>disp(No. of iterations to convergence, Vdiff, and solution) >>[j-1 Vdiff d1] To run the program, at the MATLAB prompt, type >>simplesol Note that the solution is given as 19.9999. Try running the program after first entering the command >>format long (To return to the previous format, enter >>format short) Now try adding the following command: >>d1 = round(1000*d0)/1000;

Now consider something slightly more complicated. Create a file fq.m consisting of the following: function fv = fq(x) global a b c fv = a*x^2 + b*x + c; Now run the following global a b c a = 2; b = 1; c = -5; fqs = FZERO(@fq,1) Verify that fqs returns the solution to a*X^2 + b*X + c = 0 by calculating a*(fqs)^2 + b*fqs + c

You might also like