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

Mod&Sim Matlab Lect3 Functions Conditional LoopsP1

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

Mod&Sim Matlab Lect3 Functions Conditional LoopsP1

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

CCE402: Modeling and Simulation

Lecture 3

 Functions
 Conditional statements
 Loop statements

Dr. Sawsan Abdellatif 1


2
 A Matlab function is a collection of commands that does a specific task
and must have a unique name.
 Functions can improve Code Readability and Reusability.
Example:

Example:

Function name: “Cartesian2polar”


Inputs: x,y
Outputs: r, theta
3
 You can call a Matlab function from:
 A script file,
 The Command Window,
 Another function

4
 The Matlab Editor pops up and write your function as :

 Delete everything in the Editor and type the following code in the Editor

 Save this function as add2.m


 The name of the file MUST be
exactly the same as the name of
the function and must be
followed by the .m extension.
5
 You must give the correct number of input arguments or you will get an error.
 Matlab executes the add2 function, and the returned result is assigned to the
variable a.

 Note that the function arguments x, y and


z do not actually appear in the
Workspace window and they do not exist
in Matlab memory, as they were only
temporary function variables.

6
7
To call this function:

8
 Many functions do not calculate values, but rather accomplish a task such as
printing formatted output.

Example

Call

 since the function isn’t returning any value, it cannot be called from an
assignment statement:

9
 A variable that is created within a function has a limited scope. This means
that this variable can be only accessed or modified by this function.
 This variable is called a local variable.

Answer:

 The variables a, r, and c are local


Call this function variables to the function pow. To
check this for the variable r, type at
the Command Prompt

10
 A variable created in the Command Window cannot be accessed by a
function.
Example:

Even though we have created the variable r in the Command Window,


the pow function cannot access this variable.

 Similarly, a variable created in a script file cannot be accessed by a


function. 11
 Write a MATLAB function to calculate the 2D distance between
two points. Then call this function from the command window
using any two points and show the expected result.

12
13
Syntax

x=1;
y=-4;
r=1;
b=0;
if (x>0 & y<3)
r=3;
b=b-1;
r=2 end
r = 3, b=-1 14
Example:
 Write a function that has two arguments and returns a value which is equal to
the addition of both arguments.
 This function checks whether both arguments are scalars:
 If both arguments are scalars, the function performs the addition;
 Otherwise, it displays a warning message and returns without attempting
to perform the addition.

15
16
Syntax

r=2 r=3
17
r=2 r=3

18
Syntax

19
r=2

Note that even though condition y<-2 is


true, the command r=3 is not executed

20
Syntax

The term “expression” here can be either a scalar or a string character.

21
Suppose that we would like to convert 10 cm to meters.

y = 0.1

22
The expression case {'km', 'kilometer'} means that the command
y=1000*x; is evaluated when units variable is equal to either 'km' or
'kilometer'. 23
24
 The for keyword is used to run a piece of code for a specific number of
times.

Syntax

Where . approximates down a real number toward the nearest lower integer

 The loop stops executing the commands when the value of the iteration
Variable is greater than the final value.
25
Write Matlab code to generates the first 7 numbers of Fibonacci series

26
>> x
x=0.5314
27
>> x
x=0.5905 28
>> x
x=0.9
29
 Matlab will not execute the commands inside the loop since the number of
iterations is negative. The value of ix is [ ] i.e., empty matrix

>> x
x=1
30
−6−6
The number of iterations of a for loop = +1=6+1=7
−2
The values of iteration variable are 6, 4, 2, 0, -2, -4, -6

>> x
x=0.4783
31
You can follow the execution of the program on a step-by-step basis using
the debug tools available in Matlab to check the results for each iteration

32
Try it without
loop

33
Syntax

final value1−initial value1


Number of iterations of outer loop 𝑁1 = +1
increment1

final value2−initial value2


Number of iterations of inner loop 𝑁2 = +1
increment2

Number of total iterations of nested loop N = 𝑁1xN2

m: Number of rows
n: Number of columns 34
of Example 19

Try it
without
loop

35
Answer

Try it
without
loop
36
37
38

You might also like