17
Introduction to MATLAB MECH 300H Spring 2008

Introduction to MATLAB MECH 300H Spring 2008. Starting of MATLAB

  • View
    231

  • Download
    2

Embed Size (px)

Citation preview

Introduction to MATLAB

MECH 300H

Spring 2008

Starting of MATLAB

Starting of MATLAB• MATLAB file operations use the current directory and the search path as reference points. Any file you want to run must either be in the current directory or on the search path

Arithmetic Operators

MATLAB works according to the priorities:1. quantities in brackets2. powers, 2 + 3^2 => 2 + 9 = 113. * /, working left to right (3*4/5=12/5)4. + -, working left to right (3+4-5=7-5)

Number and format• The ‘e’ notation is used for very large or very small numbers:

• The constant ‘π’ is represented by the command ‘pi’

• All computations in MATLAB are done in double precision, which means about 15 significant figures. The format how MATLAB prints numbers is controlled by the ‘format’ command

Common built-in function

Suppressing input

• One often does not want to see the result of intermediate calculations terminate the assignment statement or expression with semicolon, ‘;’

• The value of x is hidden. Note also we can place several statements on one line, separated by commas or semicolons.

M-files

• In writing programs, it is suggested to write it in the editor and save it in a file named, abc.m, instead of writing in the command window

• The Editor/Debugger provides a graphical user interface for text editing, as well as for M-file debugging. To create or edit an M-file use File -> New or File -> Open, or use the edit function

Matrix

432

987

4516

A

To entry a matrix:

• Separate the elements of a row with blanks or commas

• Use a semicolon ; to indicate the end of each row

• Surround the entire list of elements with square brackets, [ ]

For example:

A is created and is a 3x3 matrix

Matrix

432

987

4516

A

To extract a element from a matrix or to insert a element to a matrix:• recall the name of matrix • insert the row and column number into a bracket• A ( i, j ) – i and j represent row and column no. respectively

For example,

The Colon Operator

• Colon ‘:’ generate vector, say x, from n to m with unit increment

• Use increment other than one, say pi/4

Plotting graphThe default is to plot solid lines. A solid white line is produced by

To put a title and label the axes, we use

Plotting graph

0 0.5 1 1.5 2 2.5 3 3.5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

To edit the range of axis:

On the figure window

EDIT Axes properties

Example:

First curve Second curve

Export Figures

In the figure window, one can save the current figure in many different formats, such as “tiff”, “jpg”, “eps”,”fig”

MATLAB only recognizes “.fig” format.

Flow control statement

• The flow control statement used in MATLAB is similar to that in C/C++ language

• For Loops - repeats a group of statements a fixed, predetermined number of times

• While Loops - repeats a group of statements an indefinite number of times under control of a logical condition

• If -evaluates a logical expression and executes a group of statements when the expression is true

Example for Loop• Plot the function

• MATLAB code:

)sin(xy )2,0( x

% N=the number of intervalN=100;for i=1:N x(i)=i/N*2*pi; y(i)=sin(x(i));endplot(x,y,'*')

0 1 2 3 4 5 6 7-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

x

y

Function statement

• Function is the statements which can be used by other programs or functions.

function [] = function_name ();• Example

%functionfunction [f,n] = funeval(x);n = length(x);f=sin(x).*exp(x)+ones(n,1);

%The main programN= 100;dx = 10/N;for i = 1:N x = i * dx;end[f,n] = funeval(x);plot(x,f)