0% found this document useful (0 votes)
36 views8 pages

Practical Work N 1

Uploaded by

sarahhamz1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

Practical Work N 1

Uploaded by

sarahhamz1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of Algiers 1

Faculty of Sciences
Department of Computer Science

Numerical Methods Practical Works

L2 Computer Science

2024-2025
Practical Work “01”
I. The MATLAB environment. II. Basic syntax.

I.1 Introduction. II.1 Starting point.


MATLAB, or "matrix laboratory", is a Execute the following commands:
numerical computation software produced by Commands Results
MathWorks (www.mathworks.com). MATLAB is a >> 5+5 ans = 10
simple, highly efficient language, optimized for >> x =15-10 x=5
matrix processing and numerical computation. >> 7/0 ans = Inf
The MATLAB environment is a super-complex >> x=10; y = 25
>> y=25
calculator with an interpreted environment.
>> %15*4
>> a = 2; b = 7; c = a * b c = 14
I.2 MATLAB interface. Remarks
When MATLAB is started, a window like 1. By default, all calculations are assigned
this appears. to the ans variable.
2. You can assign variables in a simple way.
3. Variables such as ‘’Inf’’ are already
predefined in MATLAB.
4. The semicolon (;) indicates the end of the
instruction.
However, if you wish to hide the MATLAB
output for an expression, add a
semicolon after the expression.
5. The (%) symbol is used to indicate a
comment line.
You can also write a comment block as
MATLAB has the following panels: follows %{comment %}.
• File Directory Window: This panel gives 6. You can have several assignments on the
you access to project folders and files. same line.
• Command Line Window: This is the main
area where commands can be entered at II.2 Command history
the command line. It is indicated by the
In the command window, type the expressions
command prompt (>>).
Commands Results
• Variable Window (Workspace): The >> sin (pi/2) ans = 1
workspace displays all variables created >> 732 * 20.3 ans = 1.4860e+04
and/or imported from files. >> x = sqrt(16) x=4
• Script Editor Window: This panel is used >> clear
to create and edit scripts. >> clc
Now click on on the keyboard.
Remark encounters a new variable name, it creates the
MATLAB saves the command history. You variable and allocates the appropriate memory
can recover previously entered instructions, space. If the variable already exists, MATLAB
modify them and reuse them using the arrows replaces the original contents with new ones.
on the keyboard.
Remark
II.3 Variables. The character string is a line vector, to
In the MATLAB environment, variables create it we write the characters between two
are considered as matrices. Variable names quotation marks.
consist of a letter followed by a number of
letters, digits or underscores ( _ ). II.3.2 Special variables.
MATLAB is case-sensitive. Type the following expressions.
Commands Results
II.3.1 Data types. >> pi ans = 3.1416
>> 3i ans = 0.0000 + 3.0000i
Type the following expressions
>> x = ans/0 x = NaN
Commands Results
>> 'Hello World!' ans = 'Hello World!'
>> n = 2345; a = 2345 MATLAB supports the following special
>> a = double(n) variables and constants.
>> b = uint32 (789.50) b = 790 Name Meaning
>> c = 5678.92347; d = 5679 ans The most recent response.
>> d = int32 (rn) i, j The imaginary number √−1.
>> 20 == 0 ans = 0 Inf Infinite number.
NaN Not a Number.
MATLAB offers several fundamental data pi 𝜋.
types. Each data type stores data in the form of
a matrix. II.4 Commands.
Data type Description II.4.1 The command “format”
int8 Signed integers on 8 bits.
Type the following expressions
uint8 Unsigned integers on 8 bits.
int16 Signed integers on 16 bits. Commands Results
uint16 Unsigned integers on 16 bits. >> pi ans = 3.1416
int32 Signed integers on 32 bits. >> format long
uint32 Unsigned integers on 32 bits. >> pi ans = 3.141592653589793
single Numerical data with simple precision. >> x = 7 + 10/3 + 5 x = 15.333333333333334
double Numerical data with double precision. >> format rat
logical logical values 1 (True) or 0 (False) >> 4.678 * 4.9 ans = 2063/90
char Strings
By default, MATLAB displays numbers with four
MATLAB does not require any type declarations decimal values. This is short format. However, if
or dimension statements. When MATLAB
you want more precision, you need to use the >> who Your variables are: a c
format command. >> exist b ans = 0

Command Description MATLAB commands for managing a session.


short The short format command Command Goal
displays 4 digits after the decimal clc Erase command window.
point. clear Deletes variables from the
long The long format command displays memory.
15 digits after the decimal point. exist Checks for the existence of a
short e Displays in exponential form with file or variable.
four decimal places plus exponent. global Declares a global variable.
long e Displays in exponential form with 15 help Looking for help.
decimal places plus exponent. lookfor Search for help by keyword.
rat Give the closest rational quit Stop MATLAB.
expression resulting from a who Display current variables.
calculation. whos Display current variables
There are still other commands, but we're (long display).
interested in these format commands.
II.4.3 Input and output commands
Exercise 01 MATLAB provides the following input and
Use MATLAB to evaluate the following output commands
expressions: Command Goal
(a) 1/0 disp Displays the contents of an
array or string.
(b) 0/0
fscanf Read data from a file.
(c) 22/7 format Controls the display format on
(d) 22/7 with format long the screen.
fprintf Writes entries for the screen
II.4.2 Session management commands. or file.
input Displays prompts and waits
Type the following expressions
for input.
Commands Results
>> clear
>> clc II.5 Operators.
>> a = 15*8 a = 120 MATLAB supports the following elementary
>> b = exp(15) b = 3.2690e+06 operations:
>> c = 15-9 c=6 1. Arithmetic operators.
>> who Your variables are: a b c 2. Relational operators.
>> whos Name Size Bytes Class
3. Logical operators.
a 1x1 8 double 4. Bitwise operations.
b 1x1 8 double 5. Set operators.
c 1x1 8 double This tutorial focuses on the first three.
>> clear b
II.5.1 Arithmetic operators. 2) Use the help command to get information
Operator Description about the command “nchoosek”
+ Addition. 3) Use this command to verify your answer.
- Subtraction.
* Matrix product.
/ Right division II.5.2 Relational operators.
\ Left division Operator Description
^ Matrix power. < Less than
.* Product element by element. <= Less than or equal to
./ Right division element by > Greater than
element. >= Greater than or equal to
.\ Left division element by == Equal to
element. ~= Different from
.^ Element by element power.
II.5.3 Conditional operators.
Example Operator Description
Commands Results && Logical AND
>> a = 10; c = 30 || Logical OR
b = 20; d = -10 ~ Logical NOT
c=a+b e = 200
xor (A, B) Logical exclusive Or
d=a-b f = 0.5000
e=a*b g=2
f =a / b z = 343 III. Saving your work.
g=a\b Type the following expressions
x = 7;
Commands Results
y = 3;
>> pi ans = 3.1416
z=x^y
>> a = log(10) a = 2.3026
>> x = a^2+15*a-1 x = 38.8407
Exercise 02 >> save mywork.mat
Use one MATLAB line to evaluate the >> clear
>> clc
following expression:
>> load mywork.mat
(4.172 + 9.131844)3 −18

−3.5 + (11.2 − 4.6)∗(7 − 2.91683)−0.4
Remarks.
1. The save command is used to save all
Exercise 03 workspace variables, in the form of a file
1) Using MATLAB calculator evaluate 𝐶413 with the .mat extension, in the active
𝑛! directory.
where 𝐶𝑝𝑛 = (𝑛−𝑝)!𝑝!.
2. The load command is used to restore
already saved variables.
IV. Script files To execute a script, simply type its name in the
So far, we've used the MATLAB environment as a command window, or press the green Run
calculator. MATLAB is also a very powerful button in the editor tab.
programming language.
MATLAB lets you write series of commands to a V. Conditional instructions.
file and execute the file as a complete unit, V.1 if…end
Syntax
IV.1 Script file (M-files). if <condition>
Script files are program files with the .m statement(s) will execute if condition is true
extension. In these files, you write a series of end
commands that you wish to execute together.
Scripts do not accept input or return output. Example
They operate on data in the workspace. In the script editor window, type:
Commands
IV.2 Create and edit M-files. a = 10;
if a < 20
To create script files, you need a text fprintf (a is less than 20\n’);
editor. You can open the MATLAB editor in one of end
two ways. fprintf ('the value of a is: %d\n', a);
1) Using the command prompt Results
Type edit in the command prompt. This a is less than 20
will open the editor. Or edit <fileName> to the value of a is: 10
edit an existing file.
If you are creating the file for the first V.2 if…else…end
time, MATLAB will ask you to confirm it. Syntax
2) Using IDE if <condition>
Choose NEW -> Script. statement(s) will execute if condition is true
This also opens the editor and creates a else
statement(s) will execute if the condition is false
file named Untitled.
end
You can name and save the file after
typing the code.
Example
In the script editor window, type:
Example
Commands
In the script editor window, type:
a = 100;
Commands Results if a < 20
>> a = 5; b = 7; c = 12 fprintf ('a is less than 20’);
c=a+b d = 12.6570 else
d = c + sin(b) fprintf ('a is greater than 20\n’);
end
Remark fprintf ('the value of a is: %d\n', a);
Results ...
a is greater than 20 Otherwise
the value of a is: 100 <statements>
end

V.3 if…elseif…elseif…else…end
Example
Syntax In the script editor window, type:
if <condition 1>
Commands
Executes when expression 1 is true.
grade = 'B';
elseif <condition 2>
Executes when expression 2 is true. switch(grade)
elseif <condition 3> case 'A'
Executes when expression 3 is true. fprintf ('Excellent! \n’);
else case 'B'
Executes when none of the conditions are true. fprintf ('Well done \n');
end case 'C'
fprintf ('Well done \n');
case 'D'
Example fprintf ('You passed \n');
In the script editor window, type: case 'F'
Commands fprintf ('Better try again \n');
a = 100; otherwise
if a == 10 fprintf ('Invalid grade \n');
fprintf ('the value of a is 10\n’); end
elseif(a==20) Results
fprintf ('the value of a is 20\n’); Well done
elseif a == 30
fprintf ('the value of a is 30\n’);
else
fprintf ('None of the values match \n');
fprintf ('The exact value is: %d\n', a);
end
Results
None of the values match
The exact value is: 100

V.4 The switch instruction


Syntax
switch <switch expression>
case <condition 1 >
<statements>
case < condition 2>
<statements>
...

You might also like