0% found this document useful (0 votes)
115 views

Introduction To Matlab

MATLAB is a high-level computing language highoptimized for scientific computation and, in particular, matrix manipulation. It makes many common mathematical operations simple and fast. The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the enter key is pressed, the statement is executed, and another prompt appears.

Uploaded by

Debadri Nandi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

Introduction To Matlab

MATLAB is a high-level computing language highoptimized for scientific computation and, in particular, matrix manipulation. It makes many common mathematical operations simple and fast. The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the enter key is pressed, the statement is executed, and another prompt appears.

Uploaded by

Debadri Nandi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Introduction to MatLab

R.Saravanakumar Senior Lecturer , School of Electrical Sciences VIT


cceee 2003

WELCOME TO ALL PARTICIPANTS

What is Matlab?
Matlab

is a high-level computing language highoptimized for scientific computation and, in particular, matrix manipulation. It makes many common mathematical operations used by scientists simple and fast.

Matlab

is an interpreted language code is compiled each time it is run. has excellent visualization tools.

Matlab

MATLAB


MATLAB is a program for doing numerical computation. computation. It was originally designed for solving linear algebra type problems using matrices. matrices. Its name is derived from MATrix LABoratory. LABoratory. MATLAB has since been expanded and is now has built-in functions for solving problems builtrequiring data analysis, signal processing, optimization, and several other types of scientific computations. computations. It also contains functions for 2-D and 3-D graphics and animation. animation.

MATLAB


The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the enter key is pressed, the statement is executed, and another prompt appears. If the statement is terminated with the semicolon ( ; ) before pressing the enter key, no results will be displayed. Otherwise results will appear before the next prompt. The following slide is the text from a MATLAB screen.

MATLAB Math & Assignment Operators


Power ^ or .^ a^b or a .^ b * or .* a*b or a.*b Multiplication / or ./ a/b or a ./ b Division or \ or .\ .\ b\a or b .\ a .\ NOTE: 56/8 = 8\56 8\ - (unary) + (unary) Addition + a+b Subtraction a- b Assignment = a = b (assign answer to a)

Arithmetic Operations

Operation addition subtraction multiplication division exponentiation

Algebraic Form a+b ab axb ab ab

Matlab Scalar a+b ab a*b a/b a^b

Some Useful MATLAB commands


   

who whos help lookfor

  

what a: clear Clear all variables from work space clear x y Clear variables x & y from work space

List known variables List known variables plus their size Ex: >> help sqrt Help on using sqrt Ex: >> lookfor sqrt Search for keyword sqrt in m-files mEx:>> what a: List MATLAB files in

Some Useful MATLAB commands all m-files in current directory List m what
       

dir ls type test delete test cd a: chdir a: a: pwd which test

List all files in current directory Same as dir Display test.m in command window Delete test.m Change directory to a: Same as cd Show current directory Display current directory path to test.m

Other MATLAB symbols


>> ... , % ; : prompt continue statement on next line separate statements & data start comment which ends at end of line suppress output and used also as row separator in matrix specify range

MATLAB Relational Operators




MATLAB supports six relational operators. Less Than Less Than or Equal Greater Than Greater Than or Equal Equal To Not Equal To < <= > >= == ~=

MATLAB Logical Operators




MATLAB supports three logical operators. not ~ and with or or | and % highest precedence & % equal precedence % equal precedence with

MATLAB Display formats




MATLAB supports 8 formats for outputting numerical results. format long format short e format long e format hex format bank format + format rat format short 16 digits 5 digits plus exponent 16 digits plus exponent hexadecimal two decimal digits positive negative or zero rational number (215/6) default display

Matlab Selection Structures




An if - else if - else structure in MATLAB. word. Note that elseif is one word. if expression1 % is true % execute these commands elseif expression2 % is true % execute these commands else % the default % execute these commands end

Searching with the Matlab Path




When you type in a variable name (e.g., force), Matlab will do the following: force),
  

check if it is in the current workspace; if not then check to see if it is a built-in function; if not then builtcheck to see if a file named force.m is in current directory; if not then check to see if force.m exists anywhere in the Matlab Path (searched in the order listed); if not then Report an error
Note: this is not entirely complete but close enough for now

Plotting with MATLAB




There are commands in MATLAB to "annotate" a plot to put on axis labels, titles, and legends. For example:

>> % To put a label on the axes we would use: >> xlabel ('X-axis label') ('X>> ylabel ('Y-axis label') ('Y>> % To put a title on the plot, we would use: >> title ('Title of my plot')

Introduction to Matlab

Matlab(MATrix LABoratory) is a tool to do numerical computations, display information graphically in 2D and 3D, and solve many other problems in engineering and science.

Text after ">>" and "%" is what you type.  a = [ 1 2 3; 4 5 6; 7 8 9 ]  a*a


 

Finally use exit or quit to leave Matlab.

Approach to Engineering Problem Solving




Five Steps  State the problem clearly  Describe the input and output information  Work the problem by hand (or with a calculator) for a simple set of data  Develop a MATLAB solution  Test the code for a variety of data

Matrices Equation Many problems in engineering and science result in matrics equations. It is convenient to solve matrics equation using Matlab. To solve the following equations: x + 2y = 5 2x + 6y = 14

a=[1 2; 2 6]  b=[5; 14]  x=a\b x=a\

3D Graph

Draw a circle

> t0p/02p; > =:i2:*i > po(i() cst) > ltsnt, o() > ai sur > xs qae

Draw many functions togather

>> >> >> >> >> >>

t=0:pi/50:2*pi; y1=sin(t); y2=cos(t); y3=sin(t-0.25); y4=cos(t-0.25); plot(t,y1,t,y2,t,y3,t,y4)

MATLAB Matrices


A matrix can be created in MATLAB as follows (note the commas AND semicolons):

matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9] matrix = 1 4 7 2 5 8 3 6 9

MATLAB Repetition Structures




A for loop in MATLAB for x = 1: 0.5 : 10 % execute these commands end

for x = array

A while loop in MATLAB while expression while x <= 10 % execute these commands end

Scalar - Matrix Addition


a=3; b=[1, 2, 3;4, 5, 6] b= 1 2 3 4 5 6 c= b+a % Add a to each element of b c= 4 5 6 7 8 9

Scalar - Matrix Subtraction


        

a=3; b=[1, 2, 3;4, 5, 6] b= 1 2 3 4 5 6 c = b - a %Subtract a from each element of b c= -2 -1 0 1 2 3

Scalar - Matrix Multiplication


        

a=3; b=[1, 2, 3; 4, 5, 6] b= 1 2 3 4 5 6 c = a * b % Multiply each element of b by a c= 3 6 9 12 15 18

Scalar - Matrix Division


a=3; b=[1, 2, 3; 4, 5, 6] b= 1 2 3 4 5 6 c=b/a % Divide each element of b by a c= 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000

Sample Problem
Ti ( i s) 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 Temper t re ( eg. F) 105 126 119 129 132 126 131 135 136 132 137

What is the average from 0.0 to 1.0 min? Units?

MATLAB Solution
         

% Compute and print average temperature % Plot Temperature (deg. F) vs Time (mins) time(1) = 0.0; time(2) = 0.5; time(3) = 1.0; temps(1) = 105; temps(2) = 126; temps(3) = 119; average = mean(temps); disp(average); disp('degrees F') % Plot & Label Temperature vs Time plot (time,temps);title('Temperature Measurements') xlabel ('Time, minutes') ylabel ('Temperature, degrees F'), grid

Practice Using MatLab


x  2 x  x  6.3 f ! 2 x  0.05005 x  3.14
MatLab >>numerator = x^3 - 2*x^2 + 6.3; >>denominator = x^2 + 0.05005x 3,14; >>f = numerator / denominator
3 2

Matlab Function Format


             

function fout = mfilename( arg list ) Statements Function output must be set equal to fout. mfilename is the name of the mfile where function is saved. Our Function Area of circle sector function fout = areasector(ri, ro, theta) % function to calculate area of circle sector AO = ro^2; AI = ri^2; Area = theta*(AO - AI)/2; fout = Area; Note use of ; to prevent undesired output in command window. Create this code in Matlab editor and save as areasector.m . Remember code is case sensitive. % is used for comments.

Click on File>New in Desktop. Editor window opens.

Input and Save Code

Click on File>Save As in the Editor. Save as areasector.m in the Matlab/work directory.

Use New Function


Function

now available for use in command window. For example, to find area of a quarter circle of inside radius 5 and outside radius 10
>> ans

areasector(5, 10, pi/2) =

58.9049

You might also like