Title: A Mathematicians Introduction to MATLAB
 1A Mathematicians Introduction to MATLAB
- Robert LangerUWEC (retired)July 2007
2Thanks
- Professor Elgindi for giving me this opportunity. 
- Students from Math 351, Fall 2005, and Math 352, 
 Spring 2007, especially
- Dave Kincaid and Steve Henke
3Plan
- Basics 
- Linear algebra 
- Ordinary differential equations 
- Partial differential equations
 I cant teach you how to use Matlab effectively, 
but I hope that I can show you enough so that 
when you need Matlab you will know how to start.   
 4Session 1
- Basics 
- Graphing yf(x) 
- User defined functions (M-file) 
- Numerical integration  
5MATLAB Environment
Command Window
 CommandHistory 
 6Some Important Details
- Everything is a matrix. 
- Comma or space separates row elements.Semicolon 
 separates rows.
- Elements are referred to by indices in 
 parentheses (not square brackets as in Maple).
- Assignment uses  (not ) 
- Terminal semicolon () suppresses output. 
- Variable names are Case sensitive. 
- Two sets of arithmetic operations  vector, 
 element-wise.
- Previous inputs cant be edited directly.
7Practice Entering Some Expressions
- gtgt43sqrt(2) 
- gtgtsin(pi/6) 
- gtgti2 
- gtgtj2 (For electrical engineers) 
- gtgtxlog(10) 
- gtgtyexp(x)
8Recalculation with MATLAB A frustration for 
Maple users
Use the up arrow key
- In Maple you can select a previous input and 
 press Enter to recalculate it.
- With MATLAB, this will just send you back to the 
 current input prompt.
- To edit and/or recalculate a previous line, press 
 the up arrow to scroll back through the worksheet
 until you find what you want to recalculate.
- Then press Enter
9Entering a matrix and assigning it to a variable 
name
 Remember that row elements are separated by 
comma or space and rows by semicolon or enter 
 10Try the following.
 XB , BX , piX , B2 , X2,X.2 , YXX , 
Y2, Y.2,B.B , BB , B.B ,sqrt(X) , exp(X) 
, cos(X) 
 11Two Kinds of Arithmetic Operations
- Matrix operations  and  
- Element-wise operations . and . 
12Saving your work
- If you open a saved Maple worksheet it must be 
 recalculated. If you exit MATLAB and then start
 it again, the history of your previous work will
 appear, but none of your variables will be known
 in the workspace.
- You can get the variables back either by 
 recalculating them or by Saving the workspace.
13Help on how to save workspace 
 14Warning MATLAB Workspace
-  If you are working on a computer that is not 
 your own, and if you are using MATLABs default
 Work folder, be sure to save a copy of your saved
 workspace in your own file space.
15The Graph of y  f(x)
- plot(X,Y) where X is a vector of domain elements 
 and Y is the vector of corresponding values.
- First example y  cos2(x)  1 
- Using X  2,-3 previously defined, 
 letY(cos(X)).21
-  
16How to make a domain vector
- A slick way to make the vector X  a, ah, 
 a2h,  , b-h, bis gtgt Xahb
- Lets plot y  cos2(x)1 on -p,p with 
 hp/18(or make one of your own)
- gtgt X-pipi/18pigtgt Y(cos(X)).21gtgt 
 plot(X,Y)
17Creating an M-file for a function
- User defined functions are created in M-files. 
- FilegtNewgtM-File 
- function yf(x) y(cos(x)).21end 
- Since the function is named f it must be saved 
 with the filename f.m
18Numerical Integration
- Lets compute where f is our previously 
 defined function.
- Search Help for integration. 
- gtgt quad(_at_f,0,pi) 
19Final Thoughts
- Nobody could or would want to know everything 
 about MATLAB.
- Start with a particular project or problem. 
- Use the Help files. 
- Be willing to try something. 
- Be prepared for frustration. 
20Final Thoughts
- Nobody could or would want to know everything 
 about MATLAB.
- Start with a particular project or problem. 
- Use the Help files. 
- Be willing to try something. 
- Be prepared for frustration, but if you need 
 MATLAB it is worth the frustration.