washington state university - starting with matlab · web viewprogramming with matlab user defined...

34
Learning MATLAB Rabayet Sadnan Washington State Univ.

Upload: others

Post on 03-Feb-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Learning MATLAB

Rabayet Sadnan

Washington State Univ.

Outline

· Starting with Matlab

· Arrays and Matrices

· Plots

· Programming with Matlab

· User Defined Functions

· Polynomials and Curve Fitting

· Symbolic Math

Book Ref: Matlab, An Introduction With Applications Book by Amos Gilat

Starting With Matlab

(FolderScript/ EditorWorkspaceCommand Window)

Starting With Matlab- Windows

Windows – Figure Window

Windows – Editor/Script Window

Windows – Command Window

Notes for working in the Command Window:

· To type a command, the cursor must be placed next to the command prompt ( >> ).

· Once a command is typed and the Enter key is pressed, the command is executed.

· However, only the last command is executed. Everything executed previously (that might be still displayed) is unchanged.

· Several commands can be typed in the same line. This is done by typing a comma between the commands.

· When the Enter key is pressed, the commands are executed in order from left to right.

· It is not possible to go back to a previous line that is displayed in the Command Window, make a correction, and then re-execute the command.

· If a command is too long to fit in one line, it can be continued to the next line by

· typing three periods ... (called an ellipsis) and pressing the Enter key. The continuation of the command is then typed in the new line

· Semicolon (;) - If a semicolon ( ; ) is typed at the end of a command, the output of the command is not displayed

· Typing (%) - Comment

· ‘clc’ – Clear Command Window

(Windows – Command Window)

· Arithmetic Operations -

(Command Windows)

Some Notes –

· Variable start with letter

· Case sensitive

· No spaces are allowed

· No built-in function’s name as variable

· Predefined Variables – ‘ans’, ‘pi’, ‘inf’, ‘eps’ (2^-52), ‘i’, ‘j’, ‘NaN’

Useful Commands –

· Clear

· Clear x y z

· Clf

· Close

Creating Vector/Array with constant spacing:

What happens for m:n ?

(Arrays and Matrices)

Creating Vector/Array with Specific Number:

Multidimensional Array/Matrices:

Special Commands:

Array Operation

1. Transpose –

2. Array addressing –

3. Using colon –

4. Adding elements in Matrices –

5. Deleting elements –

(Arrays and Matrices) (Built-In function –)

Array Operations–

Mul/Div :

1. Element wise – A .* B or A ./ B

2. Matrix operation - A * B or A / B & A \ B

Inv: B = inv (A) Determinant: B = det (A)

Solve Linear eqn –

(Arrays and Matrices)

Array Operations – Built-in Commands

B = mean (A) B = max (A)

[pos B] = max (A)

B = min (A)

[pos B] = min (A)

B = sum (A) B = sort (A)

dot (A, C) cross (A, C)

(Plots)

Plot Command

Plot a function – 3.5x2 + 5x , from x = -12 to x =12

Multiple plots in same graph – plot (x,y, a,b, c,d)

plot (x,y) hold on; plot (a,b) hold on; plot (c,d)

Multiple plots in multiple subplots –

Subplot command:

subplot( row , column, pos)

subplot( 3, 2 , 2) plot (x,y)

Formatting Plot using commands –

Special Plots –

bar , pi, stairs, stem etc.

bar(x,y) , barh (x,y), stairs (x,y), stem (x,y), pie (x)

If-else , loops, logical operator

(Programming with Matlab)

If-else , loop, logical operator

For loop

While loop

Problem ----

· A user-defined function is a MATLAB program that is created by the user, saved as a function file, and then used like a built-in function.

· The function can be a simple single mathematical expression

· Or a complicated and involved series of calculations.

· In many cases it is a subprogram within a computer program.

(User Defined Function in Matlab)

Hmax

( v2 )

(0)=

2g

How to create a user-defined function?

1. Open a new script

2. Follow the basic structure

Basic Structure:

Saving the file: SAME AS FUNCTIONS NAME Problem:1.OC to F, and

1. In cases when the value of a relatively simple mathematical expression has to be calculated many times within a program, MATLAB provides the option of using anonymous functions.

2. An anonymous function is a user-defined function that is defined and written within the computer code (not in a separate function file) and is then used in the code.

3. An anonymous function is a simple (one-line) user-defmed function that is defined without creating a separate function file (m-file).

(User Defined Function in MatlabAnonymous Function)