Introduction To Matlab
Introduction To Matlab
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.
Arithmetic Operations
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
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
MATLAB supports six relational operators. Less Than Less Than or Equal Greater Than Greater Than or Equal Equal To Not Equal To < <= > >= == ~=
MATLAB supports three logical operators. not ~ and with or or | and % highest precedence & % equal precedence % equal precedence with
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
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
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
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.
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
3D Graph
Draw a circle
> t0p/02p; > =:i2:*i > po(i() cst) > ltsnt, o() > ai sur > xs qae
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
for x = array
A while loop in MATLAB while expression while x <= 10 % execute these commands end
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
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
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.
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
58.9049