# MATLAB 50-50/Introduction

### for Summer URA Program, July/09.

#### In theory there is no difference between theory and practice. In practice, there is. Yogi Berra

• You can get the file pctar.exe here and use it on the tarfile for this directory. (pctar xvf tarfile)
• This interactive seminar will provide an introduction to the powerful computing environment of MATLAB and demonstrate the tools needed for conducting research in combinatorics and optimization using MATLAB.
• The first 50 minutes will focus on some of the basics of MATLAB, from entering matrices, solving linear systems, and calculating eigenvalues, to using the graphical features of MATLAB for plotting functions and even graphs.
• The second 50 minutes will look at writing your own MATLAB programs (M-files), using symbolic MATLAB, and solving both discrete and continuous optimization problems.
• References:

1. # First 50

1. ## First 25 of First 50: ( Whirlwind Tour)

1. The Origins of MATLAB, with a (enjoy) video;
2. #### Preliminary Notes

• MATLAB is case sensitive (X=1;x=2;x-X % observe that workspace entries appear)
• typing a variable displays its value; cell arrays, structures
• ; suppresses screen output
• various parentheses [],(),{} are used, not interchangeable: x(1); [1 2]; C={[1 2;3 4] , 'I love MATLAB'}
• up/down arrows to scroll through commands; completion of a command; tab for completion of file/variable names
• windows: directory, command history, variables
• start (bottom left) for e.g. start->matlab->demos
• directory commands: what, which, addpath, editpath
• command line; graphical interface, e.g. try rightclick on history, highlight and create m-file
• f_x function browser
• MATLAB editor debugging, line-by-line, M-lint code checker, mlint(filename) (or rt-click filename), profiler, depfun command, saving and exporting figures, anonymous functions
• help/doc topic; demo; bench; intro; whatsnew; exit/quit

2. ## Second 25 of First 50:

1. Solve a system of linear equations: x=A\b (demo with roundoff error example) Try the test example file Asolve.m. (Or use runAs.m.)
Now try the sample file Aoneseps.m. (Or use runeps.m.) Can you explain the large error?! (Exercise)
2. M-files, debugger: product help: matlab: desktop tools...: editing and debugging; create M-files from command window and history;
script file ddom.m (create A diagonally dominant), ddomalt.m
change script file to function file ddomaltf.m
3. #### More on Functions

• function handles: f=@sqrt; f(2), sqrt(2)
f=str2func('ddomaltf'); A=randn(4), f(A)
• anonymous functions: g= @(x) x^2-3*x+4-9*sin(3*x), g(2)
• (addpath timfiles.d) bisect.m (type bisect) uses function handle or string as input.
Try: bisect(@sin, [3 4]);
a=1;b=2; g=@(x) sin(x) - b*cos(a*x); bisect(g,[1 2])
h{1}=@bisect; h{2}=g; h{1}(h{2},[1 2]) % using cell array
4. #### Performance Measures

• tic statements toc; cputime; clear; n=16000; x=randn(n,1); y=randn(1,n); z=randn(n,1);
whos; a1=(y*z); whos; a2=x*a1; whos whos; b1=(x*y); whos; b2=b1*z; whos
5. #### Sparse Matrices

• A=sparse(veci,vecj,entries); full(A); nnz(A); spalloc(m,n,nzmax); A=spdiags(B,d,m,n);
try: n=1e4;density=.00001;A=sprandn(n,n,density)+rand*speye(n);spy(A); b=randn(n,1); tic; x=A\b; toc; disp(['residual is ',num2str(norm(A*x-b))]),
2. # Second 50

1. #### First 25 of Second 50:

1. Porfolio Analysis (demo)
2. Optimization (login to one of the cpu... machines, e.g. cpu105, 107, 109, 111, 113, 117 general mfcf machine info and student machine info )
• Go to Demos (e.g. start at BL) --> toolboxes --> optimization --> minimizing an expensive .... using parallel computing ... Cut and paste from the demo window.
• The tutorial contains info on various packages, for small and large scale problems:
• fminunc (unconstrained optimization)
• linprog (Linear programming)
• lsqlin (Constrained linear least squares)
• fmincon (Constrained programming)
• optimtool (Optimization Toolbox Graphical User Interface)
2. #### Second 25 of Second 50:

1. Parallel Computing Toolbox
2. Symbolic Math Toolbox
• syms x y t; r=x+x+y, r=x^2+y^2, theta=atan(y/x), e = exp(i*pi*t),
• diff(x^3), int(x^3), int(exp(-t^2)), int(x^t), % uses vrble closest to x
• f = 1/(5+4*cos(x)), ezplot(f), g=log(x), pretty(diff(f*g)),
• latex(g), ccode(g), fortran(g),
• s=simple(sym('13/7 + 17/23')), vpa('pi * log(2)'), vpa('pi * log(2)',50),
• pretty(vpa('pi',10000))
• subs(sin(x),x,pi/3), subs(sin(x),x,sym(pi/3)), double(ans)
• S=x^y, subs(S, {x y}, {3 x+1} ) % using cells

Back to Main Course Page  Start of 's Home Page (more ambigrams)  Users' Home Pages on orion.math,