This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Lecture 1. Matlab as a Calculator Welcome to the first lecture of Matlab. This lecture will focus on introducing the basic functionalities of Matlab to the students as well as the fundamentals of programming. The following topics will be covered in this lecture:
scalar operations
variable assignment
order of operations
function calls
saving and loading data to Matlab
saving and loading scripts to Matlab
how to look for help
useful commands Getting started: When you open up Matlab, you will see the following window: Matlab is divided into 4 main sections: 1) Current Folder Window: Shows you your current directory and allows you to load files into Matlab by drag
and
clicking the file 2) Command Window: You interact with Matlab by entering commands and observing the results here 3) Workspace: Shows you a list of variables that are currently stored in Matlab 4) Command History: A list of all the commands you have entered Most of your time will be spend typing up commands and expressions in the Command Window. The purpose of this lecture is to demonstrate Matlab as a powerful calculator with the ability to perform many advanced functions. To enter a command, type a command after the “>>” and hit Enter/Return. Exercise Enter “3 + 2” into the command window >> 3 + 2 ans = 5 As you can see, after entering the command and hit Enter, the command will be immediately executed and the result is output to the screen. Scalar Operations: Matlab supports all of the basic arithmetic operations ( +,
, *, /, ^). The “^” operator is used to denote exponentiation. (2^3 = 8) For example, to calculate 9
5, enter the expression “9
5” in the command window: >> 9 – 5 ans = 4 Exercise Enter the following expressions and observe their result: >> 101 + 49 >> 76 – 32 >> 3 * 5 >> 100 / 25 >> 3 ^ 2 Variable Assignment: Matlab allows you to save the result of your calculations into variables for future use. To declare your own variables, use the following format: “variable_name = expression” Example: To save the value of “6 – 3” into a variable called “a”, enter the following into the command window: >> a = 6 – 3 a = 3 To see the value of a variable, enter the variable into the Command Window: >> a a = 3 Once you assign a variable a value, it maybe used as a number in calculations. >> a * 6 ans = 18 Notice that if you don’t assign the result to to a variable, Matlab automatically saves it in to a variable called “ans” which can then be used in your calculations. In the Workspace, you can locate “a” and “ans” as well as their values. All the variables and their values will be shown here. You can even click and drag a variable into the Command Window. Note: You must be careful when naming your variables. The followings rule apply when naming a variable:
the variable name must begin with an letter
any digit, letter, and underscore my be used after the first letter
variable names are case sensitive (“a” and “A” are not the same) Order of Operations: Just like a TI
83 calculator, Matlab follows the orders of operations and evaluates the expression from left to right. To brush up on your order of operations, recall the following acronym: Parentheses Exponents Multiplication/Division Addition/Subtraction Exercise: What is the result of the following expression? >>3 + (4
2)3 * 4 + 8 / 2 Function Calls: How do we use special functions like finding the square root of a number or the sine of an angle? Luckily for us, Matlab provides many of these functions for us to use. To execute a function call, we use the following command: “function(value)” Example: To find the square root of a number, enter the following expression in the command window: >> b = sqrt(9) b = 3 To find the sine of an angle, we would call the sin() function: >> x = sin(pi/2) x = 1 >> y = cos(pi) y =
1 Note: “pi” is a special variable that is equal to 3.1415… For the trigonometry functions, the input must in radians, not in degrees. To convert from degrees to radian, multiply the angle by pi/180. Here is a list of the most commonly used functions that Matlab provides:
sqrt(x)
> finding the square root of x
sin(x)
cos(x)
tan(x)
log(x)
> natural log, ln(x)
exp(x)
> ex
abs(x)
> absolute value of x Exercises: Computer the value of pi/6 Compute the value of √(sin2(2*pi/3) + cos2(2*pi/3)) Saving and loading data to Matlab Matlab allows you to save the variable and the values in your Workspace for future use. This is useful if you want to take a break and work on the project later. You can save the variables by entering the following into the Command Window: >>save filename where “filename” is for you to decide. The data will be saved into a “filename.mat” file, and it will it appear in the Current Folder Window. To load the data back into Matlab, either drag the “filename.mat” file into the Command Window, or by typing: >>load filename Example: Enter the following Commands: >> a = 30 a = 30 >> b =25 b = 25 >> save data Now close Matlab and open it again, and enter the following command: >>load data Observe that the Workspace will be filled with data from the previous session. Saving and loading scripts to Matlab When working with a project, it often becomes very tedious to enter the same commands again and again. Matlab provides the ability to save commands into a “script” file and executing it by simply calling its name. To create your own script, enter: >> edit script_name And click “Yes” on the popup dialog. You will see that a script_name.m file will be created in the Current Window Folder, and a new window will appear where you can enter your commands. Enter your commands and save the file. Now you can run the script by simply dragging the script_name.m file in to the command window or type: >> script_name and your commands will be executed and the result of the commands will be saved to the Workspace. Note: Executing a script is equivalent to copying
and
pasting the commands directly in to the Command Window. Therefore, the script will operate on the current Workspace and manipulate whatever data currently exist there. Do not confuse a .m file with a .mat file! A .m file is a script that contains the commands to be executed. A .mat file contains only information on variables and their values. Adding Comments To add a comment in Matlab, add a “%” at the front of a line. Matlab will ignore everything after the “%” on the same line. Comments are extremely useful to the user. Example: % This is a comment! How to Look for Help: Matlab provides a rich set of documentations to help you out if you get lost. To view how to use a command or function, type the following into the Command Window: >>help command Example: >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of X. Complex results are produced if X is not positive. See also sqrtm, realsqrt, hypot. Reference page in Help browser doc sqrt To look for a particular functionality, enter “lookfor description” in the command window: Example: >> lookfor natural log
Natural logarithm. damp
Natural frequency and damping of linear system dynamics. ddamp
Natural frequency and damping factor for discrete systems. cscvn
`Natural' or periodic interpolating cubic spline curve. To view a list of files format that can be imported into Matlab, type the following command: >>help fileformats Useful Commands: There are several commands that are useful to know.
clear : remove all of the variables that are stored in the Workspace
clc: clear the command window
the “up” arrow key: allows you to cycle through previously entered comands
disp(x): display the values of x, can also be used to display a message To display a message, use the disp(‘insert message here’) function: >> disp('Hello World!') Hello World! The message must be enclosed in SINGLE quotes ‘. To suppress output and not display the result of the expression, add a semicolon(;) after the expression. Example: Compare the output of >> a = 5 + 3 a = 8 and >> b = 9
2; Notice how the second command does not show any output. However, both a and b are in the Workspace. To see what a Matlab error looks like, enter the following expression: >> sqt(9) A useful function to save the command window is to use the diary function: >>diary(‘filename’) When you wish to stop recording the command window, enter: >> diary off To read more on each topic, click on the following links
scalar operations (+,
, *, /, ^) o http://www.imc.tue.nl/IMC
main/IMC
main
node5.html
variable assignments (x = 10) o (http://math.gmu.edu/~memelian/teaching/Spring10/notes/Matlab
lec1.pdf , pages 1 to 5)
order of operations o (http://math.gmu.edu/~memelian/teaching/Spring10/notes/Matlab
lec1.pdf , page 5 to page 8)
function calls (x = sqrt(9)) o (http://math.gmu.edu/~memelian/teaching/Spring10/notes/Matlab
lec1.pdf , pages 8 to 12)
the “help” command (help function_name)
the “lookfor” command (lookfor functionalities) o (http://www.imc.tue.nl/IMC
main/IMC
main
node6.html)
“help fileformats”
save and load m files o http://www.imc.tue.nl/IMC
main/IMC
main
node9.html#Ch1.7 o http://www.imc.tue.nl/IMC
main/IMC
main
node10.html#Ch1.7a
save and load mat files o http://www.imc.tue.nl/IMC
main/IMC
main
node11.html#Ch1.8 ...
View Full
Document
 Spring '08
 greenwood
 Calculus, matlab

Click to edit the document details