Introduction to matlab
This presentation is the property of its rightful owner.
Sponsored Links
1 / 30

Introduction to MATLAB PowerPoint PPT Presentation


  • 117 Views
  • Uploaded on
  • Presentation posted in: General

Introduction to MATLAB. Alternative MATLAB Introduction Lecture authored by Annie Abell. 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.

Download Presentation

Introduction to MATLAB

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Introduction to matlab

Introduction to MATLAB

Alternative MATLAB Introduction Lecture authored by Annie Abell


Introduction to matlab1

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.


Introduction to matlab

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:

>>


Introduction to matlab

Command prompt

Workspace (variable values)

Current Directory (lists file, etc.)

Command History (what has been typed)

Command Window (where commands are entered)


Matlab s working directory

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


Introduction to matlab

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.

PRECEDENCEOPERATION

FirstParentheses (innermost pair first)

SecondExponentiation.

ThirdMultiplication and division

FourthAddition and subtraction


Introduction to matlab

Precedence of Operations

Task : Find the average of 7 and 8

>> (7+8)/2

ans =

7.500

… correct! 

>> 7 + 8 / 2

ans =

11

…not what we wanted 


Introduction to matlab

Precedence of Operations

Task: Add the cube root of 27 to the 5th root of 32

>> 27 ^ 1/3 + 32 ^ 0.2

ans =

11 … not what we wanted 

>> 27 ^ (1/3) + 32 ^ 0.2

ans =

5


Introduction to matlab

Variable Definition

>> x = 5

x =

5

>> x = x + 5

x =

10

This statement does not make any mathematical sense but in programming language it changes the value of ‘x’ to a new value.


Introduction to matlab

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

ans =

1152


Introduction to matlab

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…


Introduction to matlab

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


Introduction to matlab

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


Introduction to matlab

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


Introduction to matlab

Built-In Math Functions: Examples

Task: calculate sin(∏/ 2). Note: ∏ is defined by MATLAB as pi.

>> sin(pi/2)

ans =

1


Introduction to matlab

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


Introduction to matlab

Built-In Math Functions: Examples

Task: calculate sin2x, where x = ∏/4

>> x = pi/4

x =

0.7854

>>sin(2*x)

ans =

1


Introduction to matlab

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

x =

0.5236


Introduction to matlab

Built-In Math Functions: Examples

Task: Using (cos2x sin2x + 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


Introduction to matlab

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!!


Introduction to matlab

Saving a Script File

Valid File Names

  • Prob1a.m

  • 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


Introduction to matlab

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'


Introduction to matlab

The Display Function: disp()

This function displays a message to the command window. It can be a string of text or variablecontents.

To display your message as text,use single quotes around your message:

>> disp(‘Brutus Buckeye’)

Brutus Buckeye


Introduction to matlab

The Display Function: disp()

To display any variable contents, do not use quotes:

>> x = 5

x =

5

>> disp(x)

x =

5


Introduction to matlab

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?


Introduction to matlab

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

Height

Base


Introduction to matlab

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 


Introduction to matlab

Example Script File Printout

Example Command Window Printout


Introduction to matlab

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


Introduction to matlab

Homework Assignment

MAT-01

Introduction to Matlab


  • Login