two matlab assignments
functions/Functions Tasksheet.pdf
Sensitivity: Internal
Mathematical Software Functio n s Tasksheet
https://uk.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html
1) Write the following single variable functions as functions in MATLAB
a. 𝑓(𝑥) = sin(2𝑥2)
b. 𝑔(𝑥) = √𝑥2 + 400
c. ℎ(𝑥) = tan(𝑐𝑜𝑠(𝑥))
2) Create functions for the following
a. (𝑓 ∘ 𝑔 ∘ ℎ)(𝑥) Composite function
b. 𝑓(𝑥) + 𝑔(𝑥) + ℎ(𝑥) Addition
c. 𝑓(𝑥) ⋅ 𝑔(𝑥) ⋅ ℎ(𝑥) Mutliplication
d. Evaluate these functions with 𝑥 = 1
3) Write the following as multivariable functions in MATLAB
a. 𝑓(𝑥, 𝑦) = 𝑥2 + 𝑦2
b. 𝑔(𝑥, 𝑦) = 2𝑥𝑦
c. ℎ(𝑥, 𝑦, 𝑧) = 𝑥𝑦 + 𝑧2
d. Compute the following
i. 𝑓(3,4)
ii. √𝑓(3,4)
iii. 𝑔(5,3)
4) We want to extend our knowledge a little further
Write functions for +, -, *, / and call them ‘add’, ‘subtract’, ‘multiply’, ‘divide’
Now use these functions to compute
3 + 2 ⋅ 6 − 2
10
5) Write a function called fact which takes in a paramenter 𝑛 and returns 𝑛!
Please use a for loop, do not copy the example on the MATLAB help guide
e.g. fact(5) should return 5! = 120
Advanced 1) Write a function my_trace to compute the trace of a matrix
2) Write a function my_sum to sum all the elements of a matrix
3) Write a function sum_rows to sum all the rows of a matrix, note this should return back a row vector
4) Write a function to sum_cols sum the columns of a matrix, note this should return back a column vector
5) Combine the functions in 3 and 4 into 1 function sum_row_or_cols which takes an additional parameter
that let’s you choose to sum either columns or rows. 1 should sum over columns, 2 should sum over rows.
i.e. you should call sum_row_or_cols(A,1) or sum_row_or_cols(A,2)
functions/Matrix trace and sum.pdf
Sensitivity: Internal
Matrix Trace and Sum
Sensitivity: Internal
𝑚 × 𝑛 Matrix
𝐴 =
𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑎21 𝑎22 ⋯ 𝑎2𝑛 𝑎31 𝑎32 ⋯ 𝑎3𝑛 ⋮ ⋮ ⋮ ⋮
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛 e.g.,
𝐴 = 4 3 1 6 2 6 1 3 3 5 2 0
𝑡𝑟 𝐴 =
𝑖=1
𝑛
𝑎𝑖𝑖
Is this valid?
Sensitivity: Internal
Trace
Valid for square matrices
𝐴 =
𝑎21 𝑎22 ⋯ 𝑎2𝑛 𝑎21 𝑎22 ⋯ 𝑎2𝑛 ⋮ ⋮ ⋮ ⋮ 𝑎𝑛1 𝑎𝑛2 ⋯ 𝑎𝑛𝑛
𝐴 = 4 3 1 2 6 1 3 5 2
𝑡𝑟 𝐴 =
𝑖=1
𝑛
𝑎𝑖𝑖
This is known as the trace, what is this adding up?
The diagonal, here 𝑡𝑟 𝐴 = 4 + 6 + 2 = 12
Sensitivity: Internal
Trace
𝐴 =
𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑎21 𝑎22 ⋯ 𝑎2𝑛 𝑎31 𝑎32 ⋯ 𝑎3𝑛 ⋮ ⋮ ⋮ ⋮ 𝑎𝑛1 𝑎𝑛2 ⋯ 𝑎𝑛𝑛
𝑡𝑟 𝐴 =
𝑖=1
𝑛
𝑎𝑖𝑖 = 𝑎11 + 𝑎22 + 𝑎33 + ⋯+ 𝑎𝑛𝑛
It is adding up the main diagonal
Sensitivity: Internal
Trace
A = [1 2 3; 3 4 1; 4 3 4];
trace(A)
my_trace(A)
function t = my_trace(A)
t = 0
[m,n] = size(A)
for i=1:m
t = t + A(i,i)
end
end
Sensitivity: Internal
𝑚 × 𝑛 Matrix
𝐴 =
𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑎21 𝑎22 ⋯ 𝑎2𝑛 𝑎31 𝑎32 ⋯ 𝑎3𝑛 ⋮ ⋮ ⋮ ⋮
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛
𝑖=1
𝑚
𝑗=1
𝑛
𝑎𝑖𝑗
Is this valid?
What is it doing?
Sensitivity: Internal
𝑚 × 𝑛 Matrix
𝐴 =
𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑎21 𝑎22 ⋯ 𝑎2𝑛 𝑎31 𝑎32 ⋯ 𝑎3𝑛 ⋮ ⋮ ⋮ ⋮
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛
𝑖=1
𝑚
𝑗=1
𝑛
𝑎𝑖𝑗 =
𝑖=1
𝑚
𝑎𝑖1 + 𝑎𝑖2 + 𝑎𝑖3 …+ 𝑎𝑖𝑛
First summing the rows, then summing these values.
It is adding all the elements of the matrix!
Sensitivity: Internal
Sum
A = [1 2 3; 3 4 1; 4 3 4];
sum(sum(A))
my_sum(A)
function s = my_sum(A)
s = 0
[m,n] = size(A)
for i=1:m
for j=1:n
s = s + A(i,j)
end
end
end