Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Q1: Part (c)

Code:
% 1 (c)
syms x
%% function f(x) = arctan(x)
f_x = atan(x);
%% Maclaurin expension of arctan(x) for 9th order
T=taylor(f_x, x, 'Order', 9);
%% Integration of |f(x) T^9| at range [-1 1]

values = (int(abs(f_x-T) ,x ,[-1, 1]));


pretty(values )

Q2: part (a)


Its Q2 (a) the function that i have verified through
the following demo2a.m file (in the demo file i have
called the function by providing a set of input
arguments as vector T and it resulted in two values [N1
and N2], satisfying the provided threshold error values
of vector T)

Code:
function [N1, N2] = HW2_2a_20209999(T)

% HW2 guide code for IJ 2-(a)


% input T is scalar
% output N1 and N2 are scalar
% Fill in the areas marked with @

e = exp(1);
%% For N1
% initial staring point k=0
N1 = 0;
approximation1 = 1;
error1 = abs(e - approximation1);
% Untile the condition is satisfied, N1 grows up 1 by 1
while error1>T(1)
N1 = N1 + 1;
approximation1 = approximation1+ 1/factorial(N1);
% use the command "factorial(n)" to compute the n!
error1 = abs(e - approximation1);
end

%% For N2
% initial staring point k=0
N2 = 0;
approximation2 = (1 + 1/N2)^N2;
error2 = abs(e - approximation2);
% Untile the condition is satisfied, N2 grows up 1 by 1
while error2>T(2)
N2 = N2 + 1;
approximation2 = (1 + 1/N2)^N2;
error2 = abs(e - approximation2);
end

end
%------------------------------

demo2a.m:
This one is the main function form where i have called the function
HW2_2a_20209999 of question Q2(a)

Code:

%[N1, N2] = HW2_2a_20209999([T(1) T(2)])


[N1, N2] = HW2_2a_20209999([0.2 0.2])

Q2 part (c)

Function code:

function [D] = HW2_2c_20209999(A)


% HW2 guide code for IJ 2-(c)
% input) A is n by n square matrix.
% output) D is a scalar which is a determinant of A.
% Fill in the areas marked with @
[m, n] = size(A);
D = 0;
if m ~= n
fprintf('Error : Matrix is not a square\n');
else
if m == 1
D = A;
elseif m == 2
D = (A(1,1)*A(2,2)) - (A(1,2)*A(2,1));
else
for i = 1:m
rows = 1:m;
rows(i) =[]
D = D + (-
1)^(i+1)*A(i,1)*HW2_2c_20209999(A(rows,2:n));
end
end
end

end
%-------------------------------------------------

Demo code to verify the result of function HW2_2c_20209999,


I have given and input matrix A to the function and verified the
results is truly correct.

clear all
A = [1 2 3;4 5 6;7 9 9]
[D] = HW2_2c_20209999(A)

You might also like