Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

PROGRAMMING IN MATLAB

Uploaded by

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

PROGRAMMING IN MATLAB

Uploaded by

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

Mustapha Stambouli Mascara University 2nd Year License & Engineering

Sciences and Technologie Faculty 2024-2025


Department of Electrical Engineering Professor REGUIEG.KH

MATLAB Practical Session N°04: PROGRAMMING IN MATLAB 2) Statements usage


2.a. The switch statement evaluates an expression and
1) M-files: Scripts and functions
executes one of several groups of statements based on the
To take advantage of MATLAB’s full capabilities, we need to know value of that expression. It is particularly useful when you
how to construct long and complex sequences of statements. This have multiple conditions to check against a single variable.
can be done by writing the commands in a file and calling it from
Syntax:
within MATLAB. Such files are called “m-files” because they must
have the filename extension “.m”. This extension is required in order The basic syntax of a switch statement in MATLAB is as
for these files to be interpreted by MATLAB. follows:
switch switch_expression
There are two types of m-files:
case case_expression1
A. Script files contain a sequence of usual MATLAB commands, % Statements for case 1
that are executed (in order) once the script is called within case case_expression2
MATLAB. % Statements for case 2
...
B. Function files, on the other hand, play the role of user otherwise
defined commands that often have input and output. You can % Statements if no cases match
create your own commands for specific problems this way, End
which will have the same status as other MATLAB commands. Example 1: Basic Switch Case
This script evaluates grades based on letter grades using
Exemples: switch case.
Write a program that calculates the area of a circle given its radius in
script file, then in function file. What did you notice grade = 'B'; % Define the grade
switch grade
Script function case 'A'
r = 5; % Define radius function [ area ] = circle_area( r ) fprintf('Excellent!\n');
case 'B'
area = = pi * r^2;
fprintf('Well done!\n');
disp(['Area of the circle with area = pi * r^2; % Area formula A = ?r^2 case 'C'
radius ', num2str(radius), ' is end fprintf('Good job!\n');
case 'D'
', num2str(area)]); fprintf('You passed.\n');
case 'F'
Mustapha Stambouli Mascara University 2nd Year License & Engineering
Sciences and Technologie Faculty 2024-2025
Department of Electrical Engineering Professor REGUIEG.KH

fprintf('Better try again.\n'); Example :


otherwise for i = 1:5
fprintf('Invalid grade.\n'); disp(i^2);
end end
2.b. If Statement executes a block of code based on 3.b. A while loop is used when you want to repeat a block
whether a condition is true. The syntax is: of code an indefinite number of times until a certain
if condition condition is met. The condition is checked before each
% statements iteration.
elseif another_condition Syntax:
% statements while condition
else % Statements to execute
% statements End
End
Example :
Example : Basic If Statement
This script checks if a number is positive, negative, or zero. i = 1;
while i <= 5
number = -5; % Define a number disp(i);
% i = i + 1;
if number > 0 end
disp('The number is positive.'); Practicle exercices
elseif number < 0 1) execute a function named matrix_sum.m that
disp('The number is negative.'); computes the total sum of all elements in matrix A by
else transforming it into a column vector.
disp('The number is zero.'); 2) Execute a script that define the function F and plot it
end in figure.

3) For and while loops


3.a. A for loop is typically used when you know in advance 3) Execute a function calculates the factorial of a number
how many times you want to execute a statement or a block 𝑛!
of statements. devided by its self:
𝑛
Syntax: 4) Write a script demonstrates a switch statement for
for index = values days of the week
% Statements to execute 5) Write the script of Example: Basic If Statement as
End function file.

You might also like