Document

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 5

TP03:Control statements (if and switch statements,for and

While loops)

1/ Comments:
_Explains the use of % FOR Comments

2/ Logical expressions:
2.1 Logical operations : Discusses comparison operators we have :
( == its meaning equality, < is less than, <= is less than or equal to, > its mean greater than, >= is
greater than or equal to,~= is inequality) , and logical operators (& is and logical , | its mean the
or logical, ~ its the negation logical ).
In Matlab , a logical variable can take the values 1(true) or 0(false) , for example:

>> x= 30;

>>y= 55;

>> x<y % display 1 (true)

ans=

>>x==y %display 0 (false)

ans=

2.2 Matrix comparaison: introduces functions like isequal and isempty


Isequal:used to test if two ore more arrays are equal in size and content. It returns
1(true) if all the input arrays are identical , and 0(false) otherwise. for example:

A= [1,2; 3,4];

B=[1,2; 4,3];

result= isequal ( A,B) %

returns 0 (false)

Isempty:tests if a matrix is empty . it returns 1 if it does , and 0 if it doesnt. foe example :


A= [ ];

result = isempty (A) %

returns 1 (true)

3/ Flow control structures:


if: basic conditionel structure , and it comes in the form:***

( if (condition) instruction-1....n end) or ( if else end) or ( if elseif else end)

switch:conditionel structure based on variable values, it come in the form:


( switch, case value-1...n, otherwise, end)

for:loops with a defined number of iterations .

while:loops that continue while a condition is true .

4/ Summary exercise:
they are some predefined functions in Matlab describes built-in functions ( sum, prod, mean,
diag, sort ) and how to simulate them progeammatically. Foe example:

n=length(V);

sum1 =0;
for i = 1 : n

sum1=sum1+V(i)

end

disp (sum1)

5/Functions:
In computer science , a function is a reusable block of code that takes inputs and returns result.
In mathematics, a function maps each input to exactly one output.

5.1/ Creating function in a M-Files:


In MATLAB, you can create custom functions by writing their code in M-Files , which should be
saved with the same name in function. and its contains a large number of predefined functions
such as ( sin, cos, sqrt, sum.....etc). For example:

>> edit root2.m

function r = root2 (number)

r = number/2;

precision = 6 ;

for i = 1 : precision

r = (r+nombre . / r) / 2;

end

end
TP04 : Graphics (Management of graphics windows , plot)

MATLAB provides a robust visualization system for efficiently creating and


displaying graphical data . This course introduces the basics of drawing curves in
Matlab.

1/The plot function :


Drawing curves with vectors and matrices, explaining how Matlab interprets single/multiple
vectors and matrices as inputs for X and Y axes. And it has serval forms :( contains two vectors of
the same size as arguments, contains a single vector as an argument, or a single matrix or two
as an argument)

Example :

>> x= [0 :1 :10]

X=

0 1 2 3 4 5 6 7 8 9 10

>> y=x.^2

Y=

0 1 4 9 16 25 36 49 64 81 100

>> plot(x,y)

2/Change the apparearance of curve :


Details how to modify line styles , colors , and markers using specific characters and strings in
the plot function .
Example :

%red color , dotted and with stars

>> plot( x , y , ‘ r:* ‘)

3/Annotation of a Figure :
Describes adding titles, axis labels , text , annotations , and grids to graphs for better
comprehension.

4/ Drawing multiple curves un the same figure :


Explains how to overlay multiple plots in the same figure using commands like hold on or
providing multiple arguments in plot

5/Manipulating the axes of a figure :


Show how to control axis limits , scaling , and visibility using the axis command and related
options like axis square or axis equal

F(x)=x^3 -2

>>x= -2 : 0.5 : 2 ;

>>y= x.^3-2 ;

>>plot(x,y)

6/Other types of charts :


Introduces additional MATLAB charting functions like (bar , bar 3 ), and mentions others like (hist
, pie , and polar plots)

For example :

>>x=[3,5,2,6]

>>y=[4,1,5,2]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy