Vous êtes sur la page 1sur 5

Introduction to MATLAB

Introduction to MATLAB
MATLAB, which stands for MATrixLABoratory, is a state-of-the-art mathematical software package,
which is used extensively in both academia and industry. It is an interactive program for numerical
computation and data visualization, which along with its programming capabilities provides a very
useful tool for almost all areas of science and engineering. Unlike other mathematical packages, such
as MAPLE or MATHEMATICA, MATLAB cannot perform symbolic manipulations without the use
of additional Toolboxes. It remains however, one of the leading software packages for numerical
computation.

1. Starting up MATLAB (on Windows® Systems)


MATLAB package can be launched either by entering the command matlab on the command
prompt or using the MATLAB icon on the desktop.

2. Command line Help


MATLAB Help is available from the command line prompt. Exercise the following commands.
a. >> help d. >> more on
b. >> help help e. >> help elfun
c. >> help elfun f. >> help matfun

3. Demos
Demonstrations are invaluable since they give an indication of MATLAB’s capabilities. A
comprehensive set is available by entering demo on the command prompt.

4. Basic Mathematical calculations on MATLAB


The basic arithmetic operators are used in conjunction with brackets: ( ). The symbol ^ is used to
get exponents (powers). In the evaluation process, MATLAB works according to these priorities.
a. Quantities in brackets.
b. Powers.
c. * / : working left to right
d. + - : working left to right
Evaluate theresults of the following expressions manually using the above priority list and then
verify the results on MATLAB.
a. >> 7 – 2^2 c. >> 12 – 2^2^3 + 3*3*4
b. >> 6 + 2*5 – 5^2 d. >> 3^2*(4*5)-5

5. Numbers and formats


MATLAB recognizes several different kinds of numbers as follows.

The “e” notation is used for very large or very small numbers.

Page 2 of 5
How MATLAB prints numbers is controlled by the format command and use help format
command to view help associated with this command. Following table lists how this command
can be used.

If you wish to use the default format, just enter format.

6. Variables and variable names


The result of a MATLAB calculation is automatically stored in a variable named ans and it is
always replaced by the result of the current calculation. We can use our own names to store values
as well. Legal names consist of any combination of letters and digits, starting with a letter. These
are allowable.
NextValue, Next2Value, y6, T3, p87hf4
These are not allowable.
Next-Value, 2Order, %y, @what
Most important thing about MATLAB variables is those are Case-sensitive, for example, ansand
ANS are two different variables. Moreover, you should avoid initializing predefined reserved
words such as eps, pi, i, j,etc.

7. Suppressing output
The output of the result of a calculation can be suppressed using the semicolon. Try these.
a. >> k=34 c. >> k=67;
b. >> k d. >> k

8. Useful functions
a. >>clc (Clear command window)
b. >> who (List current variables)
c. >>whos (List current variables, long form)
d. >> clear (Clear variables and functions from memory)

9. Vectors/Arrays
An array is a combination of elements and its length is specified by the number of elements it has.
Elements should be enclosed in square brackets. Arrays can be defined in these ways. Identify the
dimension of these arrays.
a. >>a=[1 2 5 7] c. >> c=[3 4 5; 5 2 5]
b. >> b=[-6 5 sqrt(9)] d. >> d=[1 -2; 5 6; 3 -8]
The function length is used to determine the length of an array. Note that, to do some arithmetic
operations, the dimensions of arrays should agree.

10. The Colon Notation


The Colon can be used to generate row vectors. Try these.
a. >> 1:5 b. >>-3:7

Page 3 of 5
c. >>5:-5 e. >>1:0.1:2
d. >>5:-1:-5 f. >>0:-0.2:-1

11. Picking up elements from an array


Array elements can be picked up by specifying row and column indices. Try the examples given
below.
a. >> t=[1 5 4 7 -1 8 6] e. >> t(1:2:7)
b. >> t(1) f. >> u=[1 2 4; 5 6 7]
c. >> t(0) g. >> u(2,2)
d. >> t(2:4) h. >>u(1:2,1:2)

12. Transpose and complex conjugate


Transpose of an array can be determined by using an inverted comma after the variable name. If
the variable is a complex variable, then the transpose is the complex conjugate. Try these.
a. >> u=[1 2 3; 4 5 6]
b. >> u’
c. >> h=6+8j
d. >> h’

13. Keeping a record


The command diary can be used to save all the text that appears on the screen to a specified text
file by issuing diary <filename>. The command diary off will conclude the diary operation.

The command save can be used to save the current values of all variables to a file. The command
save currentsessionwill create a file called currentsession.mat and store all the variable on that.
The command load can be used to invoke the saved parameters back to the session again.

14. Plotting graphs


The command linspace() can be used to create a linearly spaced vector. The command plot can
then be used to plot the required graph. Try these.
a. >> x=linspace(0, 2*pi, 100) g. >>ylabel(‘yaxis’)
b. >> plot(x, sin(x)) h. >> grid
c. >> t=linspace(0,360,360) i. >> x1=linspace(0,2*pi,100);
d. >> plot(t,sin(2*pi*t)) x2=linspace(0,2*pi,100)
e. >> title(‘Graph of y vs. t’) j. >> plot(x1,sin(x1),x2,cos(x2))
f. >>xlabel(‘x axis’) k. >> legend(‘sin curve’, ‘cos curve’)
Specialized Graphs
The command stairs is a special plotting command. It accepts two arguments in the same way
plot and stem do but plots a “stairstep” plot.
»t = 0:1/32:2 ; x = cos(2*pi*t) ;
»stairs(t,x) ;

The command stem is used to plot discrete-time functions. Each data point is
indicated by a small circle at the end of a line connecting it to the horizontal axis. The
instruction sequence,
»t = 0:1/32:2 ; x = sin(2*pi*t) ;
»stem(t,x) ;

Page 4 of 5
15. MATLAB loops
The following loop structures can be used on MATLAB.
a. for loop
b. while loop
c. switch..case

16. Writing MATLAB programs(scripts – M-file)


A multiline MATLAB script can be written on a separate editor window by selecting File → New
→ Blank M-file. After writing the program, it can be executed either using the green coloured
Save and Run button or just entering the file name of the M-file on the command prompt. Write
MATLAB programs in order to,
a. Calculate the sum of integers 0 to 100. This program should only display the result.
b. Use input command to pass parameter at the run-time and to plot a sinusoid depending on
the entered parameters (Frequency, Amplitude, phase).

17. Writing MATLAB functions


A multiline MATLAB functions can be written using the keyword function along with the
definitions of input and output parameters. The following structure has to be followed when
defining MATLAB functions.

function[list of outputs] = function-name(list of inputs)


.
.
end

Using this structure, try these.


a. Write a function named findAreato calculate the area of a circle when value of radius is
passed as a parameter.
b. Write a function named findMax to find the maximum value in an array which is passed
as a parameter.
c. Write a function named findFactorial to calculate the factorial value of a passed
parameter.
d. Write a function to sort all the elements in an array in the ascending order which is passed
as a parameter.

Page 5 of 5

Vous aimerez peut-être aussi