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

## Presentation on theme: "Introduction to MATLAB MECH 300H Spring 2008. Starting of MATLAB."— Presentation transcript:

Introduction to MATLAB MECH 300H Spring 2008

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 brackets 2. powers, 2 + 3^2 => 2 + 9 = 11 3. * /, 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 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 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 graph The 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 To edit the range of axis: On the figure window EDIT  Axes properties Example: First curveSecond 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: % N=the number of interval N=100; for i=1:N x(i)=i/N*2*pi; y(i)=sin(x(i)); end plot(x,y,'*')

Function statement Function is the statements which can be used by other programs or functions. function [] = function_name (); Example %function function [f,n] = funeval(x); n = length(x); f=sin(x).*exp(x)+ones(n,1); %The main program N= 100; dx = 10/N; for i = 1:N x = i * dx; end [f,n] = funeval(x); plot(x,f)

Download ppt "Introduction to MATLAB MECH 300H Spring 2008. Starting of MATLAB."

Similar presentations