0% found this document useful (0 votes)
9 views3 pages

MATLAB Risk Management Skills Guide

Uploaded by

musemwatanaka
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)
9 views3 pages

MATLAB Risk Management Skills Guide

Uploaded by

musemwatanaka
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

MCom in Risk Management

Risk Management Computing Skills 2025


Revision session

Recap
when to use short circuiting : matlab tells us when to use short circuiting.
Short-circuit operators
The AND and OR logical operators have short-circuit versions expressed in MATLAB by && and ||.
• Short-circuiting prevents the execution of the second argument when the result of the first argument means
that the result is already certain. This speeds up execution and prevents unintended consequences when used
correctly.
a(2,2) && a(2,3)
1 &&0 : the first value doesnt guarantee if the output will
Relational and logical operations be correct . It requires both to be true but we have true
and false so answer becomefalse

a(1,1) > a( 2,1) a(1,1) +a (2,3) || a(2,2) - a(2,1)


>> a = [0 1 2; 2 1 0]; 0> 2 0 + 0 || 1-2
0 : false 0 || -1 0 is false , -1 is true , the
b1 = a (1 ,1) > a (2 ,1) zero doesnt not guarantee us a
b2 = a (2 ,2) && a (2 ,3) true . so answer is 1
b3 = a (1 ,1) + a (2 ,3) || a (2 ,2) - a (2 ,1) b5= b3 && a(1,1) < a(2,2 )
b4 = a (: ,2) > a (: ,1) 1 && (0<1)
b6 = a(1,:) ==a(2, :)
b5 = b3 && a (1 ,1) < a (2 ,2) 1 && 1
[0 1 2 ] ==[2 1 0 ]
b6 = a (1 ,:) == a (2 ,:) [0 1 0 ] 1
b4 = a(:,2)> a(:,1)
[1 1] > [0 2]
implemented element wise
Array and Logical indexing [1 0 ]
• Array indexing - Vectors of index values can be used to index elements into other vectors. If x and y are vectors,
then x(y) is the vector [x(y(1)), x(y(2)),..., x(y(n))], where n = length(y).
• Logical indexing - If x and y are vectors or matrices of the same size, and y consists only of the boolean values,
i.e., zeros and ones, then y can be used to reference elements in x.
• Wherever y has a 1 in a certain position, the element in x in that corresponding position is returned. All other
elements in x are discarded. The vector or matrix y can be generated using a logical operation on a vector or
matrix or the logical command.

If-elseif-else
• The if-elseif-else statement makes use of decision structures, which are statements that allow the code to
execute different sequences of instructions for different cases.
• A good way to think of the if statement is as a true or false question. It asks your code if something is true,
and then tells it what to do next based on the answer.

• So, if statements essentially mean: ‘If something is true, then do something, otherwise do something else.’
• The general syntax is:
if condition1
statementA
elseif condition2
statementB
else
statementC
end

1
For Loops
• The for loop repeats one or more statements a given number of times.

• The general syntax is


until means the ats what
for indexvar = j:m:k has to happen for the
statements while loop to stop . So
end we have to negate it for
the code to run.
• It is used when you know how many iterations need to be executed. conditions need to be
intialised outside the
While loops loop

• The while loop tells the computer to do something as long as a condition is met.

• A while loop consists of a block of code and a condition. The condition is evaluated, and if the condition is
true, the code within the block is executed. This repeats until the condition becomes false.
• Therefore, if we don’t know how many repetitions are required in advance, a while loop is preferable over a
for loop.

• The general syntax of the while loop is:


while condition
statements
end
• If the condition is a vector, then all the elements need to be true. When the condition is false, the loop will
terminate.
• The variables that make up the condition being tested must be initialised before the loop and changed by the
statements, otherwise the while loop will run forever! If you run your code and then suspect that you may be
stuck in an infinite loop, click on the Command Window and press Ctrl+C to interrupt the execution.

Nested Loops
• A nested loop is a loop within a loop: an inner loop within the body of an outer one. These loops may be for
or while loops. For example, the following construction shows two nested for loops:
for indexvar1 = j:k
for indexvar2 = l:m works for matrix , loops through the rows and through columns.
statements
end
end everything that can be written in a script can be done in a function.
function allows you to use as many x’s as possible. first word in a function file is function .
function name cannot be equal to the output variable.
Functions
• Functions are useful when a certain part of the code must be repeated many times or if we want to make it
into a basic tool available for future use.

• MATLAB allows you to create your own functions. A function is similar to a script file in that it is also saved
with a .m extension but differs in that it can take input arguments and/or returns output arguments. The general
form of a function is given by: all the list of inputs needed for the
function [list of output arguments] = function name(list of input arguments) fuction are put here. these inputs
.. within the function file , the subfunctions are are not going to be redefined in
.
included . they cannot be accessed outside the the function and overrides what
output argument1 = ...; came in the input.
script
output argument2= ...;
.. template telling us what to do given inputs ,outputs and functions . so when we are
. calling a function , copy everything in the function file from the output , paste it into the
end script. above this pasted , should be the inputs , tell matlab what inputs are required

• All constructs that can be used in the script (if statement, loops), are also used in the same way in a function.

• A function handle is a pointer to a function. This provides a way of passing functions as input parameters to
other functions or constructing handles to functions defined inline instead of stored in a program file.
• A number of MATLAB’s numerical routines require a function to be passed as an input. Suppose you have
written a function as function [output_arg] = my_function(input_arg).
2
function handles stores an association to an existing function, to input a function into an existing function.
anonymous functions do not have a function name and are referred to to using a handle .

• You can create a handle to the function using >> my_handle = @my_function;. The ’@’ symbol indicates that
you would like the handle to my_function to be stored in the variable my_handle. It is now possible to call
my_function using my_handle.
• In other words, my_handle(x) is equivalent to executing my_function(x). Furthermore, my_handle can now
be passed as an input argument when calling other functions.

• Anonymous functions allow a quick means of creating single-expression functions without the need to create a
separate function M-file. The syntax for an anonymous function is fhandle = @(input_arguments) expression.
Note that the expression need not return a scalar, it may also return a vector, or a matrix, etc.
• As an example, highlighted part is the anonymous part
>> sqsum = @(x, y) x^2+y^2;
is an anonymous function that squares and sums two input variables, e.g., >> sqsum(1, 2) gives the answer
5.

• Note that anonymous functions are called anonymous because they don’t have function names they only have
function handles. The anonymous function is called using its function handle, as in the example above.

Debugging
• Types of errors:
– Syntax Errors occur when the syntax in any line of your code is incorrect, or when you provide a function
with inputs that are of the wrong shape, size, and/or type, or are otherwise not valid for the function in
question. A typo, or typographic error, could be considered as a syntax error.
– These are always caught by the MATLAB compiler and reported via error messages. Typically, an error
message clearly indicates the cause of error; for instance, the line number, the incorrect piece of code,
and an explanation.
– Such messages usually give enough information about where the problem is and what needs to be done.

– Algorithmic Errors occur when the program executes perfectly, but the result you receive is not what you
expect.
– The main technique involved in debugging these types of bugs is to compute the answer to the problem
you expect by some means other than MATLAB so that you know what the solution should look like and
you are able to investigate where the code breaks.
– Once you have done this, you are now able to step through the code and check that it computes the
correct result at each step.
• The Editor/Debugger enables you to work through a script or a function M-file line-by-line in debug mode,
examining and changing variables on the way. It allows you to observe which commands are being executed
and what the values of variables are while the code is running.

• To enable Debug mode you need to set one or more breakpoints at the places in the code where you wish to
pause. On the very left of the editor window, you will see the line numbers of the commands in your code.
Some of these will have a dash next to them, indicating that this is place which can be used as a break point.
Clicking on a dash changes it into a red dot setting a breakpoint at that position. Having set one or more
breakpoints, run the script in the editor by clicking the run button.

• When MATLAB reaches the break point, execution is paused, and a green arrow appears at the right of the
breakpoint in the Editor. The arrow indicates the next statement to be executed. You will also notice that
the prompt in the command window changes to K>> to remind you that MATLAB is in Debug mode. You can
now issue commands at the prompt to view and change variables. Also, if you move your mouse pointer over a
variable name in the editor, a little pop-up will show you its value.

• It is possible to step through the script line by line using Step. If you want to step into a function call
in the current block of code, use Step In. To step out of a function you have stepped into, Step Out. To
continue execution until the next break point use Continue, and to stop execution and quit debugging select
Quit Debugging.
• Note that if in the middle of a debugging session you discover a bug and wish to correct it, exit debug mode
first, otherwise the editor will not allow you to run the corrected code.

set a break point : by clicking on the number in the script .


runs up until the break point or red box . 3
step line by line … seeing how the variables are supposed to change .

You might also like