You are on page 1of 14

Family Name: BAGUNDANG Date Performed: 07/12/2023

First Name: ALMANZUR Date Submitted: 07/12/2023


Numerical Solutions Laboratory
Experiment No. NUMECO 3001B-05
Useful commands and built-in functions in MATLAB
I. OBJECTIVES

At the end of the experiment, students must be able to:

1. learn additional commands and functions in MATLAB


2. learn how to plot graphs in MATLAB
3. learn how to apply MATLAB built-in functions to engineering related problems.

II. PRE-LAB REQUIRED READING

The students are expected to have read the following topics:

Appendix A (Introductory MATLAB) of Numerical Methods for Scientists and


Engineers: An Introduction with applications using MATLAB 3 rd Edition by Amos Gilat
and Subramaniam

III. EQUIPMENT

A computer with MATLAB installed.

IV. PROCEDURE

Read and understand carefully before performing the following procedures.

1. Open your computer and look for the MATLAB application. The icon of the
MATLAB application should look like the image shown in Figure 5-1. Click the icon
and open MATLAB.

Figure 5-1

2. After clicking or opening MATLAB, you should see the environment shown in
Figure 5-2.
Figure 5-2. MATLAB environment

3. You should be able to see the following windows in the online MATLAB
environment: Home tab, Current folder window, address field, workspace window,
status bar, and command window. In addition, there is also a graphics window and
edit window. (Note: Today we will use the graphics window as we will plot graphs in
this experiment.)
4. The colon operator is a powerful tool for creating and manipulating arrays. A colon
can be used to separate two numbers, MATLAB generates the numbers between them
using an increment one. Enter the following commands below:
5. >> a = 1:5
>> A = 21:26
6. Screen shot the results of the operation and paste it in D-1 in the data section.
7. We can also adjust the increment by separating three numbers, MATLAB will
generate the numbers between the first and third numbers using an increment equal to
the second number. Enter the following commands below:
>> b = 2:2:12
8. >> B = 0.5:0.25:2.5
9. Screen shot the results of the operation and paste it in D-2 in the data section.
10. Aside from creating series of numbers, the colon can also be used to select random
individual rows and column of a matrix. As a demonstration, enter the following
commands:
>> c = [3 5 7; 2 4 6; -1 0 1]
>> C = c(2, : )
11. Screen shot the results of the operation and paste it in D-3 in the data section.
12. The colon can also be used to selectively extract a series of elements from within an
array. Enter the following commands:
>> d = [5 8 13; 11 -2 8; 14 17 9]
>> D = [1 -2 2; 3 5 8; 11 -8 22]
>> f = d(2:1)
>> F = D(1:2)
13. Screen shot the results of the operation and paste it in D-4 in the data section.
14. The linspace function generates a row vector of equally spaced points. It has the form:
linspace(x1, x2, n) which generates n points x1 and x2. As an example, enter the
following commands:
>> linspace(0, 1, 6)
>> linspace(0, 4, 5)
>> linspace(1, 50)
15. Screen shot the results of the operation and paste it in D-5 in the data section.
16. The logspace function generates a row vector that is logarithmically equally space it
has the form logspace(x1, x2, n) which generates n logarithmically equally space
points between decades 101 and 102. As an example, enter the following commands:
>> logspace(-1, 2, 4)
>> logspace(1, 4)
17. Screen shot the results of the operation and paste it in D-6 in the data section.
18. In MATLAB, aside from numbers alphanumeric information or character strings can
be represented by enclosing the strings within single quotation marks. As an example,
enter the following commands:
1. >> g = 'MATLAB'
2. >> G = 'LABORATORY'
3. Screen shot the results of the operation and paste it in D-7 in the data section.
4. Each character in a string is one element in an array. Thus, we can concatenate strings
together. As an example, enter the following command:
1. >> h = [g G]
2. >> H = [G g]
3. Screen shot the results of the operation and paste it in D-8 in the data section.
Note: The table below is a summary of built-in MATLAB functions available to operate
on strings.
4. In MATLAB, we can display strings in multiple lines, use the sprint function and
insert the two-character sequence \n between the strings. As an example, enter the
command:
>> disp(sprintf(‘Hello\nworld!’))
5. Screen shot the results of the operation and paste it in D-9 in the data section.
6. MATLAB and its toolboxes have a rich collection of built-in functions. You can use
online help to find out more about the built-in functions. Enter the following
command:
>> help log
>> help elfun
7. Screen shot the results of the operation and paste it in D-10 in the data section.
8. MATLAB also allows graphs to be created quickly and conveniently. For example,
we will create a graph for velocity versus time for an ordinary linear motion. Enter the
following commands:
>> v1 = 5
>> a = 3
>> t = [0: 10]
>> v = v1*t + 0.5*a*(t.^2)
>> plot(t, v)
9. Screen shot or copy the plotted graph of velocity versus time and paste it in D-11 in
the data section.
10. MATLAB also allows polar plots can be created quickly and conveniently. As a
demonstration, enter the following commands:
>> Theta = (0:360)
>> rho = 2*cos(2*Theta)
>> polarplot(Theta, rho)
11. Screen shot or copy the plotted graph of Theta versus rho and paste it in D-12 in the
data section.
12. Write/encode your observations/insights/perceptions regarding MATLAB in the
Observation and Discussion section.
13. Save this document with the required data and write-ups and submit it as an
attachment to this experiment.
V. DATA

D-1: Colon operator with increment equal to one

D-2: Colon operator with adjusted increment


D-3: Random selection of rows and/or column of a matrix using the colon operator

D-4: Specific selection of rows and/or column of a matrix using the colon operator
D-5: The linspace function

D-6: The logspace function


D-7: Using string characters in MATLAB
D-8: Concatenating string characters in MATLAB

D-9: Displaying string characters in multiple lines


D-10: Using the help function to call different commands from a category of functions D-11:
Plotting a graph using MATLAB
D-11: Plotting a graph using MATLAB

D-12: Plotting
a polar graph using MATLAB

VI. QUESTIONS

1. What command will you use to declare and produce the output shown in Figure 1
using the vector declaration and colon operator?

Figure 1

>> A = [1, 2, 3; 2, 4, 6; 3, 2, 1]
2. Plot a polar graph which displays the equation r = 2 sin 3A. What is the commands
needed to plot the graph of the equation?
VII. OBSERVATIONS/DISCUSSION
Discuss your observations on the experiment performed. Write your answer on the space
provided.

-During the experiment, I discovered that Matlab contains a large range of functions that
can be utilized to address a number of problems, offering effective and efficient solutions.
Furthermore, I realized that Matlab can generate graphs and tabular data for any
mathematical topic.

You might also like