Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB

Similar presentations


Presentation on theme: "Introduction to MATLAB"— Presentation transcript:

1 Introduction to MATLAB
Instructor notes: These are a new set of slides which have been modified to make the teaching more interactive and example based. The number of slides in each lecture is between The idea is to involve the students as you go along. Let the students type all the commands in the lecture along with you. This will give them a good feel of MATLAB. When you see the right arrow at the end of the slide, any further click or keyboard press will lead you to the next slide. So, you should discuss all the relevant topics of that particular slide by the time the arrow appears.

2 Introduction to MATLAB
MATLAB is a powerful program for numerical computations, plotting and programming. Use it as a calculator. Define variables and use them in calculations. Use built-in functions. Plot graphs. Write and run computer programs. Perform symbolic mathematics. Instructor notes: These are a new set of slides which have been modified to make the teaching more interactive and example based. The number of slides in each lecture is between The idea is to involve the students as you go along. Let the students type all the commands in the lecture along with you. This will give them a good feel of MATLAB. When you see the right arrow at the end of the slide, any further click or keyboard press will lead you to the next slide. So, you should discuss all the relevant topics of that particular slide by the time the arrow appears.

3 Open MATLAB now: Click on the shortcut icon or select: start / All programs / MATLAB There will be a fairly long initialization process, then following prompt should appear in the command window: >> It is recommended that the instructor not use SchoolView to control student terminals. Please open MATLAB in exactly the same way as students have on their screen. Below are the steps to get a highly readable display on your computer: To display ONLY the command window, select Desktop / Desktop Layout / Command Window Only To enlarge MATLAB fonts, select File / Preferences / Fonts and change 18 to 36. The commands which the students need to type along with you during the lecture are shown in red after the ‘>>’.

4 Command Window (where commands are entered)
Workspace (variable values) Current Directory (lists file, etc.) Command prompt Command History (what has been typed) Modified by R.C.Busick 1/28/11, Hi 221, Instructor notes: The command window is the primary interface between the user and Matlab. It is the control center for using Matlab. Commands will be invoked here. User written programs will be run from here; this includes script files and function files which will be covered in detail later on. The command prompt is displayed in the command window. All commands must be entered at the command prompt. Matlab is an environment and a language all in one. From the command window various environment manipulation tasks may be done as well. For example the cd path_name command that is common in Unix and Dos can be used from the command window to change the current working directory. The ls (dir) command can be used to display the contents of the current working directory. Added 8/06/07 by Dick Busick to describe different MATLAB panes. Instructor should step through the various panes to describe their use. Note that it is possible to "toggle" between Current Directory and Workspace Command Window (where commands are entered)

5 MATLAB’s Working Directory
When you first start MATLAB it is a good practice to change the working directory to your Z: drive or USB device. Keep all of your files in the same folder so you do not have to constantly change your directory Modified 01/28/11 by R.C.Busick, HI 221 , Instructor note: If the establishment of the "new" operating directory is not made prior to running the editor, any saved files may be placed in a different directory from that of the command window, resulting in great confusion!

6 Order of Precedence Higher-precedence operations are executed before lower-precedence operations (like Excel or graphing calculators). If two operations have the same precedence, then the expression is executed from left to right. PRECEDENCE OPERATION First Parentheses (innermost pair first) Second Exponentiation. Third Multiplication and division Fourth Addition and subtraction Instructor notes: The order of precedence is very important and the kind of thing that students may not initially pay attention to since it doesn’t seem very exciting, especially those who have not had any programming before. But, it is important and very important in terms of programming. The order of precedence indicates the order of operations…that is the sequence of steps that commands or any of the various operations will be executed in. Even though Matlab (seemingly) processes an entire equation and then moves on to the next, it actually breaks equations down into their smallest components and executes the components in a particular order. Parentheses have the highest order of precedence. Consequently expressions enclosed within parentheses are evaluated first. If there are parentheses nested within other sets of parentheses, then the innermost ones will be evaluated first. It might be important to point out that if students want to make sure that an operation with a normally low order of precedence executes first or earlier, an easy way to accomplish that is to include the lower operation within parentheses. <If students use parentheses for everything, it increases readability, and eliminates confusion> The second highest order of operation is exponentiation. Be careful where parenthesis are placed. The results can be very different. For example: 64^(1/2) is significantly different than 64^1/2 After exponentiation in order of precedence comes multiplication and division. And finally, the lowest order of operation is addition and subtraction. It is also important to note that if there are operations of the same precedence effectively occurring at the same level, then they will be evaluated from left to right.

7 Precedence of Operations
Task : Find the average of 7 and 8 >> / 2 ans = 11 …not what we wanted  >> (7+8)/2 ans = 7.500 … correct! 

8 Precedence of Operations
Task: Add the cube root of 27 to the 5th root of 32 >> 27 ^ 1/ ^ 0.2 ans = 11 … not what we wanted  >> 27 ^ (1/3) + 32 ^ 0.2 5

9 Variable Definition >> x = 5 x = 5 >> x = x + 5 10
This statement does not make any mathematical sense but in programming language it changes the value of ‘x’ to a new value.

10 Calculations with Variables
We can use the variables defined previously to do calculations. Define the following: a=8, my_var =12 >> a + my_var ans = 20 >> a * my_var^2 1152

11 Rules About Variable Names
Can be up to 63 characters long. Must begin with a letter. Can contain letters, digits, and the underscore character (no spaces). MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. Avoid naming variables with names that are used by MATLAB: exp, sin, cos, sqrt, length, mean, max, min… Modified 01/28/11 by R.C.Busick, HI 221 , Instructor notes: Matlab is case sensitive. Matlab distinguishes between upper and lower case. So, even if the only difference between two variable names is the case of one (or more letters), then the variables are completely separate variables.

12 Examples of Variable Names
Acceptable: My_name MyName myname1 Unacceptable: 1Name starts with numeral My Name spaces not allowed My-Name special characters not allowed

13 Built-In Math Functions: Examples
exp(x) exponential (ex) log(x) natural logarithm (log base 'e') log10(x) base 10 logarithm (log) sqrt(x) square root abs(x) absolute value Instructor notes: Matlab has numerous built in arithmetic functions. Some commonly used ones appear on this slide, but there are too many to list. It’s important to remind people that the trig functions are in radians. It’s also important to point out that log(x) is the natural logarithm and log10(x) is the base 10 logarithm. There are functions for rounding values as listed on page 15 of the text that can be very useful. There are statistical functions. Functions for summing vectors (which will be discussed later on)

14 Built-In Math Functions
In MATLAB, trigonometric functions use radians. So for these examples, x is in radians: sin(x), cos(x), asin(x), acos(x), tan(x), cot(x) MATLAB also has equivalent functions where x is in degrees: sind(x), cosd(x), tand(x), etc Instructor notes: Matlab has numerous built in arithmetic functions. Some commonly used ones appear on this slide, but there are too many to list. It’s important to remind people that the trig functions are in radians. It’s also important to point out that log(x) is the natural logarithm and log10(x) is the base 10 logarithm. There are functions for rounding values as listed on page 15 of the text that can be very useful. There are statistical functions. Functions for summing vectors (which will be discussed later on)

15 Built-In Math Functions: Examples
Task: calculate sin(∏ / 2). Note: ∏ is defined by MATLAB as pi. >> sin(pi/2) ans = 1

16 Built-In Math Functions: Examples
Task: calculate e2 >> e ^ 2 ??? Undefined function or variable 'e' How do we fix this? >> exp(2) ans = 7.3891

17 Built-In Math Functions: Examples
Task: calculate sin2x, where x = ∏ /4 >> x = pi/4 x = 0.7854 >>sin(2*x) ans = 1

18 Built-In Math Functions: Examples
Task: Using (cos2xsin2x + tan(x/2)) / e3x , find the value for x = 30 degrees. >> x = 30 x = 30 >> x = x * pi / 180 0.5236

19 Built-In Math Functions: Examples
Task: Using (cos2xsin2x + tan(x/2)) / e3x , find the value for x = 30 degrees. >> (cos(2*x)* sin(x)^2 + tan(x/2)) / exp(3*x) ans = 0.0817

20 Saving a Script File Once the script file is completed, it must be saved. In our class use Save As to your Z:\ or your own USB drive. This should be the same as the working directory you specified when you started MATLAB. ALWAYS SAVE YOUR WORK. Back it up in multiple places for safety! To the cloud!!

21 Saving a Script File Valid File Names Prob1a.m problemone.m
Invalid File Names Prob-1a.m Special Characters not allowed Prob 1a.m Spaces not allowed 1aProb.m Cannot start with number Modified 01/26/11 by R.C.Busick, HI 221 , Instructor notes: The file should be saved to a floppy/zip disk or possibly the Temp/Tmp directories. Remind students that if they use the Temp/Tmp directories, they copy the file to their own floppy/zip disks before leaving. Work can also be saved on their N: drive.

22 Useful Programming Commands
To clear the command window: clc >> clc To clear all variables previously defined: clear >> x = 5 x = 5 >> clear >> x ??? Undefined function or variable 'x' Modified 01/28/11 by R.C.Busick, HI 221 ,

23 The Display Function: disp()
This function displays a message to the command window. It can be a string of text or variable contents. To display your message as text, use single quotes around your message: >> disp(‘Brutus Buckeye’) Brutus Buckeye

24 The Display Function: disp()
To display any variable contents, do not use quotes: >> x = 5 x = 5 >> disp(x)

25 >> y = 7; …what happens when you type this?
Suppressing Output This is good practice! You don't need all of your program displayed in the command window, just the relevant info. >> x = 6 x = 6 >> y = 7; …what happens when you type this?

26 Script File Example Pythagoras' Theorem: Hypotenuse2 = Height2 + Base2
Create a script file and name it Pyth.m Clear the command window Clear all variables from memory Display your name and seat number Declare height of triangle as 3 units Declare base of triangle as 4 units Find the hypotenuse of the triangle Hypotenuse A basic example to give students an idea of putting everything they learnt in this lecture in one place. Give students time to write their program and then go through the program on the next slide. Height Base

27 How To Turn In All Assignments
You must print two things for every MATLAB assignment: Script file Command window output … if you do not turn in both, you will lose points  This gives the students an idea of how they should submit their homeworks. They need to submit 2 things – a print of the script file and a print of the command window.

28 Example Script File Printout
Example Command Window Printout This gives the students an idea of how they should submit their homeworks. They need to submit 2 things – a print of the script file and a print of the command window.

29 Self Check… Do you understand these concepts?
Arithmetic Operations with scalars: 5+3, 5*3 , 5^3 Precedence of operations: 7+8/2 vs. (7+8)/2 Variable assignment: a=8, b=12 Calculations with variables: a+b, a*b Important commands: clc, clear Use of semi colons (;) to suppress output Display function: disp(‘Brutus Buckeye’) Mathematical functions: sin(pi/2) , exp(2) Calculate the value of (cos2xsinx + tan(x/2)) / exp(3x) for x = 30 degrees By the end of this lecture, students should be able to understand the examples on this slide.

30 Homework Assignment MAT-01 Introduction to Matlab


Download ppt "Introduction to MATLAB"

Similar presentations


Ads by Google