Beruflich Dokumente
Kultur Dokumente
Go To Answer Directly
info@matlabassignmenthelper.com
Homework 4
Problem 4.1 :
Writing a function to perform multiple matrix operations
Write a function that will calculate the sum, difference, element-by-element multiplication, and
matrix multiplication of two arbitrary same size matrices at once. The results of function with
⎡1 2 ⎤ ⎡1 3⎤
given matrices ⎢ ⎥ and ⎢ ⎥ are shown as below in the command window:
⎣3 4 ⎦ ⎣ 2 4⎦
>> [s,d,ep,mp]=bop([1 2 ; 3 4],[1 3 ; 2 4])
s =
2 5
5 8
d =
0 -1
1 0
ep =
1 6
6 16
mp =
5 11
11 25
https://www.matlabassignmenthelper.com
⎡10 35 ⎤ ⎡5 3 ⎤
by-element multiplication, and matrix multiplication of ⎢ ⎥ and ⎢ ⎥ with your
⎣ 70 100 ⎦ ⎣9 25⎦
function in this problem.
Problem 4.2 :
Integrating two functions numerically over a given interval using function
handlers
( ax + bx + c )dx
10
i) y=∫ 2
where a = 1, b = 2, and c = 3
1
+∞ ⎛ ⎛ x − b ⎞2 ⎞ 1
ii) y = ∫ a ⋅ exp ⎜ − ⎜ ⎟ ⎟dx where a = , b = 0, and c = 1
⎜ ⎝ 2c ⎠ ⎟ 2π
−∞ ⎝ ⎠
Problem 4.3 :
Calculating the factorial of a non-negative integer
Write a function to calculate the factorial of a non-negative integer, n!. It is not allowed to use
recursive algorithm.
Homework 4 Solution
function [s,d,ep,mp]=bop(A,B)
%
% Problme 4.1: Writing a function to perform multiple matrix operations
%
% Input arguments: two same size matrices
% Output arguments: sum(s), difference(d), element by element product(ep)
% and matrix product(mp) of two matrices
%
Problem 4.2 : Integrating two functions numerically over the interval with function
handler
i) With ‘quad’ function, you can obtain numerical integration over [1,10]. First, you
should define the function you will evaluate, and calculate numerical value for given
integration interval.
ii) The procedure to evaluate Gaussian function is same as i), but we have indefinite
integration interval. ‘quad’ function can be used only for the integration over definite
interval. However, we can assume that indefinite number is approximated to very large
number such as 1010, since Gaussian function goes to 0 as x goes to infinity (or –
infinity), and we get very close number to what we get with infinite evaluations.
>> G=@(x)1/sqrt(2*pi)*exp(-(x/sqrt(2)).^2); % Define Gaussian
1.0000
more significant digits) The reason why they are different is that we just evaluate function
up to certain finite number, and there always exists error between numerical value and true
value. Therefore, evaluating tolerance is quite important in the numerical analysis. This
evaluation is acceptable if we have tolerance of 10-5, but it’s not if we have 10-6.
function o=fctrl(n)
%
% Problem 4.3: Calculating the factorial of non-negative integer with
function
%
% Define 0!=1
o=1;
% Calculate factorial
% if n<1, MATLAB skips 'for' loop (for n=0)
for i=1:n
o=o*i; % n!=(n-1)!*n
end