0% found this document useful (0 votes)
36 views

Lab-01-Introduction To Matlab-Solution

Uploaded by

hufuo0faypi0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lab-01-Introduction To Matlab-Solution

Uploaded by

hufuo0faypi0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of frères Mentouri Engineering: Sciences and Technologies

Faculty of Science and Technology Cours of MATLAB


Department of electronics Academic-Year: 2023-2024

LAB 1: INTRODUCTION TO MATLAB (MATLAB GUI)


Exercise 01 (MATLAB GUI): MATLAB works with three main types of windows on your computer screen.
These are the command window, the figure window and the editor window.
1. Explain the main functionalities of each window.
2. Explore MATLAB’s help capability by trying the following (explain each command based on your
understanding):
>> info >> help ops >> help diary >> lookfor inverse
>> help plot >> helpwin >> help elfun >> help sqrt
3. Type the following commands and explain the results shown on the command window
>> a = 21; b = [8,2]; c = 9.65; x= 'Hello!';
>> whos
4. Type demo and explore some of the demos of MATLAB commands.
Exercise 02 (Command Window)
1. Use the command line to define a scalar x=3, a vector y=[1, 2, 3] and a matrix z=[1, 2, 3; 4, 5, 6].
2. Calculate the values of m, n and w and display the results with 15 decimals.
1 4 6 𝑚
𝑚= 2
+ × , 𝑛 = 𝑚2 − , 𝑤 = 𝑚4 𝑒 −𝑚
2+3 5 7 𝑚+3
3. Evaluate the expression 𝒂𝟑 + √𝒃𝒅 − 𝟒𝒄, where 𝑎 = 1.2, 𝑏 = 2.3, 𝑐 = 4.5, and 𝑑 = 4.
Exercise 03 (Plotting in MATLAB)
1. Use the function plot (.) to generate a line plot for an array x of positive numbers from 1 to 20.
2. Try the function stem(.) on x and explain the differences between the generated plots.
Exercise 04 (MATLAB programming): MATLAB programming is done using M-files, i.e., files that have the
extension .m. These files are created using a text editor.
1. Open the text editor and create a new m-file “average.m”.
2. Type in the following program and save it.
function y=average(x)
L=length(x);
sum=0;
for i=1:L
sum=sum+x(i);
end
y=sum/L; % the average of x
3. Call the program from the command window to execute it for x=1:100.
4. Using the code below, plot the function 𝑦(𝑥) = 2𝑒 −0.2𝑥 for the range 0 ≤ 𝑥 ≤ 10. Try it in the MATLAB
editor, then call the program in order to generate the plot.
clc,clear;
x=[0:0.1:10];
y=2.*exp(-0.2 .* x);
plot(x,y); % the plot instruction
Exercise 05 (variables and data types): Give the type and the result of the following expressions:
▪ mod (213,100) / 10 213 / mod (17,10) mod (round (25.475), 10)
▪ int32(3) - 1 * 4 + 2 3 - (1 * 4 + 2) base2dec('1B',16) + 4 - 2
▪ 'a' + 1 ~= 'B' 'i' + 1 == 'k' + 1 round (3.2 * 3) - 24.5 / 7
▪ Int32(213) / 100 213.0 / 100 213 / int32(100)
▪ 0 | ~(0) 7 >= 3 + 4 7>4>3
▪ base2dec('16',8) 3 + 4 | 15 - 8 (10 == 10) & ~(10 == 10)

1|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

LAB 1: INTRODUCTION TO MATLAB – SOLUTIONS


The MATLAB Working Environment: MATLAB environment is highly configurable, which means that it may
not look exactly like this on your machine.

The MATLAB GUI consists of several components and windows that help users manage their work. The five
important windows are:
1. The Command Window is where you type in MATLAB commands. These can be simple equations you
want evaluated, or more complex expressions involving MATLAB scripts or functions.
2. The Command History Window shows the commands you have entered in the past. You can repeat
any of these commands by double-clicking on them, or by dragging them from the Command History
Window into the Command Window. You can also scroll back to previous commands by using the up
arrow in the Command Window. When you learn how to edit MATLAB script files or functions, you
can also drag commands from the Command History Window into a file.
3. The Workspace shows the list of variables that are currently defined, and what type of variable each is.
(i.e., a simple scalar, a vector, or a matrix, and the size of all arrays) Depending on the size (i.e., type) of
the variable, its value may also be shown. If any of the variables in the Workspace are plottable, they
may be plotted quickly and easily by right-clicking on the variable name and selecting a plot type.
4. The Current Directory Window shows the contents of the current working directory.
o Double click on any file to open it in a (text) editor
o Right-click on MATLAB scripts and function files to execute the commands contained therein.
Right-click on data file to import the data as MATLAB variables.
o Change directories by clicking on folders or use the Current Working Directory text box at the
top of the MATLAB working environment.
5. The File Details Window shows full details of the files in the current working directory.
Windows may be re-arranged according to your personal preferences, including dragging windows away from the
MATLAB work environment.

2|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

Exercise 01 (MATLAB GUI)


MATLAB works with three types of windows on your computer screen. These are the Command window, the
Figure window and the Editor window.
1. Main functionalities of each window:
▪ The figure window only pops up whenever you plot something.
▪ The editor window is used for writing and editing MATLAB programs (called M-files) and can be
invoked in Windows from the pull-down menu after selecting File | New | M-file.
▪ The command window is the main window in which you communicate with the MATLAB
interpreter. The MATLAB interpreter displays a command >> indicating that it is ready to accept
commands from you.
2. Explore MATLAB’s help capability by trying the following (explain each command):
>> info Display contact details of MathWorks for further assistance:

For information about MathWorks, go


to:http://www.mathworks.com/company/aboutus/contact_us
or call +1 508 647 7000.
>> help This command helps you to explore MATLAB’s help capability and
documentation. It displays the help text for the functionality specified by
name, such as a function, method, class, toolbox, or variable.
>> help plot Gives a description the plot function. This MATLAB function creates a
2-D line plot of the data in Y versus the corresponding values in X.

>> helpwin This command lists topics for groups of functions in the MATLAB® File
Help window. It shows brief descriptions of the topics and provides links
to display help.
>> help ops Gives the list of all operators and special characters in MATLAB
classified in different categories. In fact, the MATLAB language uses
many common operators and special characters that you can use to
perform simple operations.

>> help diary Gives a description the diary function. This function creates a log of
keyboard input and the resulting output (except it does not include
graphics). The output of diary is an ASCII file, suitable for printing or for
inclusion in reports and other documents. If you do not specify filename,
MATLAB creates a file named diary in the current directory.

3|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

>> help elfun This command shows the full lists of elementary functions in MATLAB.
MATLAB provides hundreds of built-in functions covering various
scientific and engineering computations.

>> lookfor inverse If you don't know the exact command, but (atleast !) know the keyword
related to the task you want to perform, the lookfor command may assist
you in tracking the exact command. It searches for quick summary
information in each command related to the keyword.
For example, suppose that you were looking for a command to take the
inverse of a matrix. MATLAB does not have a command named inverse;
so, the command help inverse will not work. Thus, by typing lookfor
inverse, you can see the various commands available for the keyword
inverse.

>> help sqrt Gives a short description of the square root pre-built function. sqrt(X) is
the square root of the elements of X.

1. Type the following commands and explain the results displayed on the command window
>> a = 21; b = [8,2]; c = 9.65; x= 'Hello!'
>> whos
All created variables are displayed in the Workspace window of the interface MATLAB and permanently
stored in memory. The whos command allows to display the list of these variables in text mode: After
running the commands we get, the list of variables present in the workspace as well as their properties:
>> a = 21; b = [8,2]; c = 9.65; x= 'Hello!';
>> whos
Name Size Bytes Class Attributes

a 1x1 8 double
b 1x2 16 double
c 1x1 8 double
x 1x6 12 char

3. Type demo and explore some of the demos of MATLAB commands.

4|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

MATLAB has a wonderful demonstration program that shows its various features through interactive
graphical user interface. By typing demo command at the MATLAB prompt, we can invoke the
demonstration program (see Figure) and the program will guide you throughout the tutorials.

Exercise 02 (Command Window)


1. Use the command line to define a scalar x=3, a victory y=[1, 2, 3] and a matrix z=[1, 2, 3; 4, 5, 6].
MATLAB variables are created with an assignment statement. The syntax of variable assignment is:
>> variable name = a value (or an expression).
Once a variable has been created, it can be reassigned.

Matrices and arrays are the fundamental representation of information and data in MATLAB. Actually,
All MATLAB variables are multidimensional arrays, no matter what type of data. A matrix is a two-
dimensional array often used for linear algebra.
To create an array with three elements in a single row, separate the elements with either a comma (,) or
a space. This type of array is a row vector.
To create a matrix that has multiple rows, separate the rows with semicolons.
>> x=3
x =
3
>> y=[1,2,3]
y =
1 2 3
>> z=[1,2,3;4,5,6]
z =
1 2 3
4 5 6
2. Use the command line to calculate the values of m, n and w and report the results with 15 decimals.
MATLAB by default displays only 4 decimals in the result of the calculations. However, the command
format controls how the results of computations are displayed.
1 4 6 >> format long
𝑚= 2
+ ×
2+3 5 7

5|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

>> m=1/(2+3^2)+(4/5)*(6/7)
m =
0.776623376623377
𝑚 >> format long
𝑛 = 𝑚2 −
𝑚+3 >> n=m^2-(m/(m+3))
n =
0.397504254262324
𝑤 = 𝑚4 𝑒 −𝑚 >> format long
>> w=m^4*exp(-m)
w =
0.167324134895788

3. Evaluate the expression 𝒂𝟑 + √𝒃𝒅 − 𝟒𝒄, where 𝑎 = 1.2, 𝑏 = 2.3, 𝑐 = 4.5, and 𝑑 = 4.
Note: if you do not wish to see the intermediate results, you can suppress the numerical output by putting
a semicolon (;) at the end of the line
a=1.2;
b=2.3;
c=4.5;
d=4;
a^3+sqrt(b*d)-4*c
ans =
-13.2388
Exercise 03 (Plotting with MATLAB)
1. Use the function plot (.) to generate a line plot for an array x of positive numbers from 1 to 20.
>> x=1:20 % create a victor of 20 elements from 1 to 20.
x =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
>> plot(x)

2. Try the function stem(.) on x.

>> stem(x)

6|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

3. Explain the differences between the generated plots.


The main point of difference between the two functions plot(.) and stem(.) is that plot displays the
continuous values for the curve. Example: drawing a graph of y= sin(x) using a pencil without removing
its contact from paper.
On the other hand, stem displays the discrete values of the points on the curve. Example: drawing the
graph of y=sin(x) for specific values such as 0,15,30,45,60and 90degrees respectively. Then join these
points from the X-axis.
Exercise 04 (MATLAB programming): MATLAB programming is done using M-files, i.e., files that have the
extension .m. These files are created using a text editor.
1. Open the text editor and create a new m-file “average.m”.
To create an m-file, choose New from the File menu and select Script. This procedure brings up a text
editor window in which you can enter MATLAB commands. To save the m-file, simply go to the File
menu and choose Save as (remember to save it with the '. m' extension). Type the name “average.m”.
2. Type in the following program and save it.
function y=average(x)
L=length(x);
sum=0;
for i=1:L
sum=sum+x(i);
end
y=sum/L; % the average of x

Note: each time you edit the file, you must save it before closing it.
3. Call the program from the command window to execute it for x=1:100.
x=1:100;

7|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

average(x)
ans =
50.5000
4. Using the code below, plot the function 𝑦(𝑥) = 2𝑒 −0.2𝑥 for the range 0 ≤ 𝑥 ≤ 10. Try it in the MATLAB
editor, then call the program in order to generate the plot.
clc,clear;
x=[0:0.1:10];
y=2.*exp(-0.2 .* x);
plot(x,y); % the plot instruction
Code explanation:
1. clc clears all the text from the Command Window, resulting in a clear screen. Whereas, clear:
erases the variables from previous runs this will reduce chances of error in subsequent runs.
2. The second line (x=[0:0.1:10];) generates an array with first element as 0 and last element as
10, where each successive elements have a common difference of 0.01. It is an arithmetic
progression.
Steps for running the code:
Step 01: Create the m-file for this code and type it.
Step 02: Save the file with the name “plotting.m” before closing it.
Step 03: In the command line type the name of the file for running it and you will get the flowing plot.

Exercise 05 (variables and data types): Give the type and the result of the following expressions:

>> mod (213,100) / 10 >> 213 / mod (17,10) >> mod (round (25.475), 10)
ans = 1.3000 (type: 8 double) ans = 30.4286 (type: 8 double) ans = 5 (type: 8 double)
>> 3 - 1 * 4 + 2 >> 3 - (1 * 4 + 2) >> z=base2dec('1B',16) + 4 - 2
ans = 1 (type: int32) ans = -3 (type: 8 double) ans = 29 (type: 8 double)
>> 'a' + 1 ~= 'B' >> 'i' + 1 == 'k' + 1 >> round (3.2 * 3) - 24.5 / 7
ans = 1 (type: logical) ans = 0 (type: logical) ans = 6.5000
>> int32(213) / 100 >> 213.0 / 100 >> 213 / int32(100)
ans = 2 (type: logical) ans = 2.1300 (type: 8 double) ans = 2 (type: int32)
>> 0 | ~(0) >> 7 >= 3 + 4 >> 7 > 4 > 3 (7>4=1 and 1>4=0)
ans = 1 (type: logical) ans = 1 (type: logical) ans = 0 (type: logical)
base2dec('16',8) >> 3+4 | 15-8 >> (10 == 10) & ~(10 == 10)
ans = 14 (type: 8 double) ans = 1 (type: logical) ans = 0 (type: logical)

8|Page

You might also like