






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
MATLAB provides a single platform for computation, visualization, programming and software development. All problems and solutions in MATLAB are in a notation ...
Typology: Schemes and Mind Maps
1 / 11
This page cannot be seen from the preview
Don't miss anything!







What is MATLAB?
MATLAB (short for MAT rix LAB oratory) is a language for technical computing, developed by The Mathworks, Inc. (A matrix is a rectangular array or table of usually numerical values.) MATLAB provides a single platform for computation, visualization, programming and software development. All problems and solutions in MATLAB are in a notation very similar to the way mathematics is written. In addition, you can use MATLAB to build Graphical User Interfaces (GUIs) so that you can develop user-friendly custom software.
MATLAB is widely used in all areas of applied mathematics in education and research at universities and in industry. MATLAB is a great tool for solving a wide variety of problems in science, engineering, and finance. In addition, MATLAB has powerful graphic tools and can produce nice pictures in both 2D and 3D. It is also a programming language (similar to C) and is one of the easiest programming languages for writing mathematical programs.
Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.
This is where you enter commands. The MATLAB environment consists of menus, buttons and a writing area similar to an ordinary word processor. There are plenty of help functions that you are encouraged to use. The writing area that you will see when you start MATLAB, is called the command window. In this window you give the commands to MATLAB. For example, when you want to enter a command or run a program you have written for MATLAB you start in the command window by typing its name after the prompt. ( MATLAB’s prompt is >>. ) Once you have typed the command you wish MATLAB to perform, press Enter. The command window is also useful if you just want to use MATLAB as a scientific calculator or as a graphing tool.
Quitting MATLAB: To quit MATLAB, at a MATLAB prompt type command quit or exit , then press Enter.
Using MATLAB as a (scientific) calculator
MATLAB’s computational engine uses (about) 15 decimal digits in computations (unless directed to do otherwise). So it can be used as a powerful calculator for arithmetic expressions and the calculation of values of function expressions.
Arithmetic Operations MATLAB command
*WARNING: You must indicate multiplication using the . Examples: In expressions it good practice to use parentheses as illustrated below. This is particularly important for fractions which are used in MATLAB.
MATLAB expressions Value shown in format long e Value in format short
(1.5 - 4.67)*(9/3.1) -9.203225806451613e+000 -9.
(5.2 - 3.72)^(1/2) 1.216552506059644e+000 1.
(7.02 + 12.3)/(4.25 – 3.125) 1.717333333333333e+001 17.
In MATLAB you can control how a numerical value is displayed by using what are called format commands. After a MATLAB prompt type command format long e or format short to get the displays above. Other such commands are format long and format short e.
The number π (pi) in MATLAB can be accessed by using the name pi. Here is what is displayed for π in various formats.
Note that the “e formats” display in scientific notation; 5.6782e+002 means 5.6782 ×10 2.
Also note that the last decimal digit in a “short display” is obtained by rounding.
Regardless of the display format used to show values on the screen computations are done with 15 digit arithmetic.
Use command help format for the description of other display formats.
Format short display for pi 3. Format long display for pi 3. Format short e display for pi 3.1416e+ Format long e display for pi 3.141592653589793e+
Special case: A value which is exactly zero will be displayed as a single zero. If you see 0.0000, the value is not exactly zero. Change to a long format and re-display it.
Operators, Elementary Functions, & General Purpose Commands
Arithmetic operators. plus + minus - times * power ^ divide \
Relational operators. Equal == Not equal ~=
Less than < Greater than > Less than or equal <= Greater than or equal >=
Logical operators. and & or | not ~
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Trigonometric & Hyperbolic sin - Sine. sinh - Hyperbolic sine. asin - Inverse sine. asinh - Inverse hyperbolic sine. cos - Cosine. cosh - Hyperbolic cosine. acos - Inverse cosine. acosh - Inverse hyperbolic cosine. tan - Tangent. tanh - Hyperbolic tangent. atan - Inverse tangent. atan2 - Four quadrant inverse tangent. atanh - Inverse hyperbolic tangent. sec - Secant. sech - Hyperbolic secant. asec - Inverse secant. asech - Inverse hyperbolic secant. csc - Cosecant. csch - Hyperbolic cosecant. acsc - Inverse cosecant. acsch - Inverse hyperbolic cosecant. cot - Cotangent. coth - Hyperbolic cotangent. acot - Inverse cotangent. acoth - Inverse hyperbolic cotangent.
Exponential exp - Exponential. log - Natural logarithm. log10 - Common (base 10) logarithm. log2 - Base 2 logarithm and dissect floating point number. pow2 - Base 2 power and scale floating point number.
sqrt - Square root. nextpow2 - Next higher power of 2.
Complex abs - Absolute value. angle - Phase angle. complex - Construct complex data from real and imaginary parts. conj - Complex conjugate. imag - Complex imaginary part. real - Complex real part.
Calculus Operators diff -Difference and approximate derivative when working on a vector. diff -Differentiate when working on a string or symbolic expression; will do higher derivatives & partial derivatives. int -Integrate; indefinite or definite depending upon the arguments. taylor -Taylor series expansion.
Basic Plotting Tools
plot -Plots a set of ordered pairs. plot3 -Plots a set of ordered triples. ezplot -Easy to use function plotter.
General Purpose Commands home -Moves the cursor to the upper left corner of the Command Window and clears the visible portion of the window. You can use the scroll bar to see what was on the screen previously. clc - Clear command window; scrollbar not available. demo - Run demonstrations. who - List current variables. whos - List current variables, long form. clear - Clear variables and functions from memory. load - Load workspace variables from disk. save - Save workspace variables to disk. saveas - Save Figure or model to desired output format. quit - Quit MATLAB session. exit - Exit from MATLAB. what - List MATLAB-specific files in directory. type - List M-file. which - Locate functions and files. path - Get/set search path. addpath - Add directory to search path. rmpath - Remove directory from search path. diary - Save text of MATLAB session.
Diary File: At times you will need to record the results of your MATLAB session as part of an assignment. Have a flash drive ready to use so you can save your MATLAB work. Method 1: Change the Current Directory field in the desktop toolbar to the drive letter associated with the flash drive. You can also use the name of a folder on the flash drive. Now type command diary filename.txt followed by the Enter key. Each computation (but not graphics) you make in MATLAB will be saved in your directory in a text file named filename.txt. Method 2: Use a path name to the flash drive. Example: diary f:\numanalhw\section2_1problem This saves things to the f-drive into folder numanalhw using file name section2_1problem
You can then edit the saved file using your favorite text editor. When you have finished your MATLAB session you can turn off the recording by typing diary off at the MATLAB prompt. If you want to stop your MATLAB session for any reason, you can reopen the diary file the next time you start MATLAB. If you use the same file name, the results of your new MATLAB session will be written at the end of the old diary file. You may want to use different names for each session on an assignment, and then merge the files.
Example: int(f,-1, 3) gives the numerical result 16. (Here -1 was the lower limit and 3 the upper limit.)
To use diff or int on inline functions you must convert it to a character string. For example command char(g ) displays g =
cos(2pix + ang)** and then diff(g) will compute the derivative with respect to x.
Plotting equations in MATLAB
A simple way to plot a function given by a formula is to use the command ezplot. Usually you want to specify an interval for the domain.
Example: For function g1 = ‘exp(-x)sin(x)’* to graph it over [-2, 10] we use command ezplot(g1,[-2,10]). The figure produced is By default the title at the top of the graph is the expression for the function. You can change the title. For instance if you want to put your name on the graph use command title(‘YOUR NAME’). You can also label the axes using commands xlabel and ylabel in a similar fashion. For example , xlabel(‘X-Axis’) and ylabel(‘Vertical Axis’). (Note the use of single quotes.)
There are times when you want to graph more than one function on the same set of axes using the same domain [a, b]. To plot two functions, f and g which you have defined previously between single quotes the following set of commands will do the job.
ezplot (f, [a,b]) hold on %<== this command holds the graph so we can plot a second time ezplot(g,[a,b]) hold off %<== this command release the graph so new plot can be generated
To generate two graphs and have them both be viewable: ezplot (f, [a,b]) figure,ezplot(g,[a,b]) %<== the figure command generates a second graphics window for the second plot If you omit the figure command the second plot will be the only one viewable.
A drawback to this approach is that the two curves will be in the same color.
To print a graph you have generated use the File drop down menu at the top of the plot.
-2 0 2 4 6 8 10
-1.
-0.
0
x
exp(-x) sin(x)
To copy a graph you have generated so you can paste it into a document use the Edit drop down menu.
To close or erase all the graphs currently generated us command close all.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
All numbers are stored internally using the “long” format specified by the IEEE floating point standard. Floating point numbers have about 16 significant decimal digits and a finite range of about 10 -308^ to 10 308.
To see which directories/folders are on the search path type the command path. To change the search path select Set Path from the File menu in the desktop, and use the Set Path dialog box. The command addpath can be used to add directories to the path, and command rmpath can be used to remove directories from the path,
help sin SIN Sine of argument in radians. SIN(X) is the sine of the elements of X.
help sqrt SQRT Square root. SQRT(X) is the square root of the elements of X. Complex results are produced if X is not positive.
command st = 'Hi there. ' displays st = Hi there.
Strings are not commands. Strings supply information. Strings are used for messages and for formulas as previously illustrated.