Sie sind auf Seite 1von 10

NEW ERA UNIVERSITY

College of Engineering and Technology


Department of Electronics Engineering

LABORATORY REPORT NO. 1


INTRODUCTION TO MATLAB

NAME: TIONGCO, Princess Hergrace I. SCHEDULE: T4PM-7PM

SECTION: 4ECE-A INSTRUCTOR: Engr. Esmeralda Maniquis

RATING

Laboratory Report No. 1


Introduction to MATLAB

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.

it also offers conventional programming by means of script files

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

To develop a logical plan of attack for solving particular problems

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

It performed the mathematical operation of exponent and gave the answer of = 8.

3. Try using the arrow keys, What is the result?

The result is the same.

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.

5. Try the following codes separately,

a. sqrt(pi)

= 1.7725

b. sin(x) (assign value for x in radian)


= sin(30) = -0.9880

c. exp (x) (assign value for x)

=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;

Enter x to check that x is a vector; it is a row vector.

Then type:

size(x)

What is the result? ans = 1 11

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

0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121

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

What is the result?


Type sin(2*x). What is the new result?

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):

a=[1 2;2 -1];

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)

-Entering the set of equations

who

Your variables are:

a b x

-Declaring the variables


x = double(x), y = double(y)

x=

-from x, this gives us x=1, y=1


whos

Name Size Bytes Class Attributes

a 2x2 32 double

b 2x1 16 double

x 2x1 16 double

-this functions declares the current variables, their sizes and types

Explain how each command line solved the linear equations.

c. Check of results: After executing either (a) or (b) above type the following commands (exactly as they are):

x+2*y % should give ans=4

2*x-y % should give ans=3

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.)

What particular demo did you choose? Describe briefly.


- Basic Matrix Operations that shows the basic techniques and functions for working with matrices in the
MATLAB language.

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

6. Try the following set of codes:

a. magic(10) (try replacing the value)


b. [x y ] = meshgrid(-8 : 0.5 : 8);
r = sqrt(x.2 + y.2) + eps;
z = sin(r) ./ r;
mesh(z);
then try:
surf(z), shading flat

Describe briefly.

-The line of codes provided this graph that looks like a Mexican hat in a way.

III. Vectors and Matrices


1. To create a row vector is explained in the previous activities, however creating a column vector
A = [5; 3; 2] or A = [5 3 2]'
Now, create a matrix with the following elements:

Write down the MATLAB codes that you used.

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

These data are the


result of an experiment on heat conduction through an iron bar. The array temp contains the corresponding
temperature. (Display with grids)

Try using different line styles and colors. Show your new graph. (Use help)

Das könnte Ihnen auch gefallen