Lab-01-Introduction To Matlab-Solution
Lab-01-Introduction To Matlab-Solution
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
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
>> 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
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.
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)
>> 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
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