0% found this document useful (0 votes)
4 views13 pages

OPM Chapter 3 Element of Programmings

Uploaded by

kaouthermounir6
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)
4 views13 pages

OPM Chapter 3 Element of Programmings

Uploaded by

kaouthermounir6
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/ 13

Ministry of Higher Education and Scientific Research University

of Algiers 1
Faculty of Sciences, Department of Mathematics

1st year Mathematics

Programming tools (MATLAB)

Prepared by:

Dr KHETTABI Imen

Associate Professor B Department of Mathematics


Faculty of Sciences, University of Algiers.

Year: 2024-2025
Chapter 3:

Elements of programming
1.1 Reading data in a program (Inputs)
1.2 Writing data in a program (Outputs)
1.3 Script
1.4 Function
1.5 Control structure
1.5.1 Conditional Structure (if … elseif … else)
1.5.2 Multiple Conditional Structure (switch … case)
1.5.3 Conditional loop (while)
1.5.4 Iterative loop (for)
1.6 Creating and Running MATLAB Programs Using M-Files
9

Chapter3 : Elements of Programming


We have seen how to use MATLAB to execute commands or evaluate
expressions by writing them on the command line. Typically, the commands
used are written as a single instruction (sometimes on a single line).
However, some problems require multiple instructions to describe their
solutions, which necessitates using multiple lines.
In MATLAB, we can find:
• Script
• Function

3.1 Reading Data in a Program (Inputs)


To read a value provided by the user, we can use the input() command
with thefollowing syntax:

variable = input(' indicative sentence ')

When MATLAB executes such an instruction, the indicative sentence will


be displayed to the user, waiting for the user to enter a value.

Example:
10

3.2 Writing Data in a Program (Outputs)


The “disp” Command
disp is a basic MATLAB function used to display text or variable values in
the command window. It's typically used for quickly showing results.

Syntax:

disp(text or variable)

Example:

The “num2str” Command


num2str converts numbers to strings so that you can combine numbers
with text for display using disp.
Syntax:
num2str(variable)

Example:
11

The “fprintf” Command


fprintf is a MATLAB function used to display formatted text or variable values in
the command window or write them to a file. It provides more control over the
output format compared to disp.

Syntax:
fprintf( format text, variables)

Example:
12

3.3 Script
A script is a program or a sequence of MATLAB instructions saved in a file
with the extension ".m".

3.4 Function
A function is a set of instructions grouped in a structure that allows it to
have input variables (the data) and output variables (the results).

function[Output1, Output2, …] = function_name(Input1, Input2, …)

Notes:

- The name of the function saved in a file with the ".m" extension must be
identical to the defined function name.
- The end of the function is optional.
- Do not name functions with hyphens, such as "vector-product".
- Do not name functions using multiple words separated by spaces, such as
"vectorproduct".

Example :
13

3.5 Control structure

3.5.1 Conditional Structure (if … elseif … else)


The set of instructions is executed only if the expression is true. Multiple
exclusive tests can be combined.

Syntax

if expression1
instructions1 …
elseif expression2
instructions2 …
else
instructions3 …
end

Example :
14

3.5.2 Multiple Conditional Structure (switch … case)


In this structure, a numerical expression is successively compared to
different values. As soon as a match is found, the corresponding block of
instructions is executed.
Syntax

Switch expression
case value1,
instructions1 …
case value2,
instructions2 …
case value3,
instructions3 …
………
Otherwise instructions …
end

Example :
15

3.5.3 Conditional Loop (while)


This mechanism allows a series of instructions to be repeated as long as a
condition is achieved.
Syntax

while expression

instructions …

end

Example1 :
This script uses a while loop to calculate the square of numbers from 1 to 5.
16

Example2 :
Compute the sum s = 1 + 2/2! + 3/3! + .... stop when s > 2.5

3.5.4 Iterative Loop (for)


This loop executes the internal block as many times as specified by a variable
acting as a counter.

Syntax

for variable = begin : step : end


instructions …
end
17

Example 1 :

Example2 :

Example3 :
18

3.6 Creating and Running MATLAB Programs Using M-Files


M-files can store data, programs (scripts) or functions.
To create an M-File, once we've written our code, save the file with a .m
extension (example: First_Program.m).
19

Running the Program:


To execute the program, simply click on Run or return to the MATLAB
command window and type the file name (without the .m extension) as
follows:
The program will execute immediately.

You might also like