0% found this document useful (0 votes)
13 views7 pages

3.matlab Week3

The document discusses MATLAB scripts, functions, control flow and operators. It covers topics like if, elseif and else statements, for and while loops, relational and logical operators. It also provides examples of scripts and functions to calculate simple mathematical operations and control flows.
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)
13 views7 pages

3.matlab Week3

The document discusses MATLAB scripts, functions, control flow and operators. It covers topics like if, elseif and else statements, for and while loops, relational and logical operators. It also provides examples of scripts and functions to calculate simple mathematical operations and control flows.
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/ 7

23-06-2023

Week 3 Week 2
Session-I Brief Sumup

© MR Summer of Core - 2023 27 © MR Summer of Core - 2023 28

 Mathematical Functions
 Some mathematical functions you have used. MATLAB as a programing language
 sqrt(x), round(x)
 sum(x), sum(x, dim), sum(sum(x))
 prod(x), prod(x, dim), prod(prod(x))
 max(x), min(x) over array or matrix (column/row)
 sin(x), asin(x), log(x), log10(x), exp(x)
Script
 List of elementary math functions help elfun ↵

 List of specialized math functions help specfun ↵ Function

© MR Summer of Core - 2023 29 © MR Summer of Core - 2023 30


23-06-2023

 What are Scripts ?  Scripts


 Script- Sequential collection of commands  Creating, saving and running a script.

 File extension .m  Scripts can be written in any text-editor like notepad,

 Could be very basic, more like MATLAB as a calculator notepad++, and others.

 Earlier, we have executed commands in Command window  MATLAB editor –specialized for creating M-files and graphical

which could not be saved or executed several times. MATLAB debugger: Highly recommended

 What do we do ?  edit edit filename.m

 Create a file with list of commands  Variables overwritten – Take extreme care
 Save the file  Commenting scripts--- Very Very Important
 Run the file  Matrix operations, etc.,
 Run scripts from command window
© MR Summer of Core - 2023 31 © MR Summer of Core - 2023 32

 What are Functions?  Scripts Vs Functions


 Function- similar to scripts with file extension .m
 Programs or routines that accept certain input arguments and
produce one or more outputs.
 Each function has its own area of workspace - Important
 Different inbuilt functions you have already used
 User-defined functions
 [outputs] = function_name(inputs)

© MR Summer of Core - 2023 33 © MR Summer of Core - 2023 34


© MR
23-06-2023

 Let’s Try more Scripts and Functions


 [outputs] = function_name(inputs)

 Area of trapezoid
 Temperature conversion from Fahrenheit to Centrigrade
 C = 5/9 X (F - 32).
 Output Commands
Control Flow and Operators
 disp
 fprintf

Summer of Core - 2023 35 © MR Summer of Core - 2023 36

 Control Flow  Control Flow


 MATLAB is also a programming language  Relational and Logical Operators
 Decision making or control flow  Relational: compares two

structures to control command numbers and determines whether

execution statement is true of false


 Or(A,B) , and(A,B)
 Control Flow
 if  for … end
 If … end, if … else …  for variable = expression
statements
end, if elseif … else …
end
end  Compute A.^2 using for loop and compare
 Examples on using if  Nested Loops
© MR Summer of Core - 2023 37 © MR Summer of Core - 2023 38
23-06-2023

 Control Flow
 Control Flow
 if

Week 3
 If … end, if … else …
end, if elseif … else …
end

Session-II  More examples on using if

© MR Summer of Core - 2023 39 © MR Summer of Core - 2023 40

 Control Flow  Control Flow


 Relational and Logical Operators  for
 Relational: compares two  Good idea to indent the loops for enhanced readability
numbers and determines whether  Examples using nested loops
statement is true of false  While … end
 Or(A,B) , and(A,B)  while expression
statements
 for … end end
 for variable = expression  Be careful of indefinite loops
statements
end  Extreme care for Equality comparisons
 Precision errors (eps)
 Compute A.^2 using for loop and compare
 Nested Loops
© MR Summer of Core - 2023 41 © MR Summer of Core - 2023 42
23-06-2023

 Practice Problem  Practice Problem


 Consider
 Create a row vector, 𝐀, of size 1×5 such that each member is
 A = Magic matrix of size 5
square of its column number
 B = Magic matrix of size 3
𝐀 = [ 1 4 9 16 25 ]
 Add elements from (1, 1) to (3, 3) from both matrix. Call it matrix
 Create another vector, 𝑩, of size 1×5 such that each member is
C.
double of its column number
 Find ‘square of max (C) – cube of min (C)’
𝑩 = [ 2 4 6 8 10 ]
 Find the product of max(𝑨) and min(𝑩)

© MR Summer of Core - 2023 43 © MR Summer of Core - 2023 44

 Control Flow  Operator Precedence


 break statement in while  The order in which MATLAB evaluates an expression
and for loops
 continue statement in for
loop
 return, continue,
switch

 Let’s try out a few examples


© MR Summer of Core - 2023 45 © MR Summer of Core - 2023 46
23-06-2023

 Practice Quiz  Cheat Sheet


Create a magic matrix A of size 4
Assign its main diagonal to another variable B
Change the elements of B by their squares
Assign 1st row of A to C and last column of A to D
Multiply 1st element of C with 1st element of D
Multiply 2nd element of C with 2nd element of D and so on…
Call this new array E

What is the combined sum of all elements in B and E?

not done

© MR Summer of Core - 2023 47 © MR Summer of Core - 2023 48

 Cheat Sheet  Acceptable Tolerance: an Example


 Consider three variables:
a = 0.3;
b = 0.2;
c = 0.1;
 If (a – b – c) equals ZERO, print ‘Good to
go.’
If not, print ‘Something is fishy.’
 The way out?
Use the concept of acceptable tolerance.

© MR Summer of Core - 2023 49 © MR Summer of Core - 2023 50


23-06-2023

Data Types – Class of


Variables
Reserved for other sessions

© MR Summer of Core - 2023 51

You might also like