Beruflich Dokumente
Kultur Dokumente
RATING
MATLAB:
stands for MATrix LABoratory, because the system was designed to make matrix computations particularly
easy
is a powerful computing system for handling the calculations involved in scientific and engineering
problems
It offers immediate execution of statements, or even groups of statements, in the Command Window.
Objective:
To familiarize student with the MATLAB environment
To be able to apply the basic commands in MATLAB which would be use in future laboratory reports.
To get to grips with the essentials of MATLAB and of technical computing
Activity:
I.
1. Try to type the following codes in the Command Window and briefly describe the results for each: (the press
Enter)
a. 2 + 3
It performed the mathematical operation of addition and gave the answer of =5.
b. 3-2
It performed the mathematical operation of subtraction and gave the answer of =1.
c. 2*3
It performed the mathematical operation of multiplication and gave the answer of =6.
d.
It performed the mathematical operation of division and gave the answer of =0.5.
e. 23
It performed the mathematical operation of exponent and gave the answer of =8.
f. 2\1
It performed the mathematical operation of division and gave the answer of =6.
2. Try the following commands and briefly describe the results for each:
a. 2 .* 3
It performed the mathematical operation of multiplication and gave the answer of =6.
b. 1 ./ 2
It performed the mathematical operation of division and gave the answer of = 0.5.
c. 2 . 3
4. Now let us assign values to variables to do arithmetical operations with the variables:
a. a=3
a=3
b. b=3;
b=3
c. c = a+b
c=6
d. a+7
=10
e. a*10
= 30
What are the results for each? What do you think happened to the codes in b and e? Explain briefly.
The code in b was stored to the variable b and in e, the variable a as 3 was multiplied to 10, giving the
of 30.
a. sqrt(pi)
= 1.7725
=exp(0) = 1
II.
1. MATLAB also handles vectors (generally referred to in MATLAB as arrays). The easiest way of defining a vector
where the elements (components) increase by the same amount is with a statement like:
x=0:10;
Then type:
size(x)
Now try:
y=2.*x
y = 0 2 4 6 8 10 12 14 16 18 20
w=y./x
ans = NaN 2 2 2 2 2 2 2 2 2 2
z = sin(x)
Columns 1 through 10
Column 11
-0.5440
2. To draw a reasonably nice graph of sin (x) is to enter the following commands:
x=0:0.1:10;
z = sin(x);
plot(x,z), grid
3. Systems of
linear equations are very important in engineering and scientific analysis. A simple example is finding the solution
of two simultaneous equations,
e.g.
x+2y = 4, 2xy = 3.
Two approaches to the solution of this system of equations are given next. This is followed by a check of the results
using the arithmetic operations you have already learned to use.
a. Approach 1: This approach is the matrix method approach. Type the following commands (exactly as they are):
b = [4; 3];
x = a\b
which result in
x=
2
i.e. x = 2, y = 1.
b. Approach 2: This approach uses the built-in solve function. Type the following commands (exactly as they are):
[x,y] = solve(x+2*y=4,2*x-y=3)
who
a b x
x=
a 2x2 32 double
b 2x1 16 double
x 2x1 16 double
-this functions declares the current variables, their sizes and types
c. Check of results: After executing either (a) or (b) above type the following commands (exactly as they are):
4. Demo
a. Try demo at the command line.
b. Another way is to double-click Demos in the Launch Pad, which is found by clicking the START
button in the lower left-hand corner of the MATLAB desk- top.
c. (If you cant see Demos, click on the question mark to open the help browser, or you can launch the
demonstration programs by clicking on it in Demos in the pull-down menu under Help at the
top of the MATLAB desk- top.)
5. Most applications have built-in help system. Write down the procedure on how you can use the help system of
MATLAB.
The help system can be called in different ways. The most important ones are listed below (please try them out
immediately in Matlab):
Via Help > Product Help. In so doing, the Help Browser/Help Navigator is opened (see figure below)
By means of the F1 key. If you do this from within the Command Window, a reduced version will be
opened, similar to other sub-windows of the desktop. In the bottom left corner you can find the link
Open Help Browser allowing for switching to the full display.
Help for specific functions: doc function name, e.g. doc sin. In this way, the help system entry for this
function is immediately displayed.
Similarly, you can select a command typed in the command window before and call help for it by means of
the context menu (right click) Help on Selection (or by pressing F1).
If you don't know exactly what entry you are looking for, you can enter the help system's search function
by direct command, e.g. docsearch operators
Describe briefly.
-The line of codes provided this graph that looks like a Mexican hat in a way.
Exercise:
1. Create a row vector called x whose elements are integers from 1 to 9. Create another row vector called temp
whose elements are:
15.6
17.5
36.6
43.8
58.2
61.6
64.2
70.4
98.8
Try using different line styles and colors. Show your new graph. (Use help)