95 lines
2.1 KiB
Mathematica
95 lines
2.1 KiB
Mathematica
|
%% Assignment 1
|
||
|
% Name: ______________
|
||
|
%
|
||
|
% Date: 21/2/2019
|
||
|
%
|
||
|
% This is a template file for the first assignment to get started with running
|
||
|
% and publishing code in Matlab. Each problem has its own section (delineated
|
||
|
% by |%%|) and can be run in isolation by clicking into the particular section
|
||
|
% and pressing |Ctrl| + |Enter| (evaluate current section).
|
||
|
%
|
||
|
% To generate a pdf for submission in your current directory, use the following
|
||
|
% three lines of code at the command window:
|
||
|
%
|
||
|
% >> options.format = 'pdf';
|
||
|
% >> options.outputDir = pwd;
|
||
|
% >> publish('assignment1.m', options)
|
||
|
%
|
||
|
%% Problem 1
|
||
|
% a)
|
||
|
x = 5
|
||
|
% b)
|
||
|
y = 4.2 * 10 ^ (-2)
|
||
|
% c)
|
||
|
r = sqrt(pi)
|
||
|
% d)
|
||
|
rate = 0.01
|
||
|
t = 6
|
||
|
T = 12
|
||
|
money = 1000
|
||
|
interest = money * (exp(rate * t / T) - 1)
|
||
|
% e)
|
||
|
a = 1 + i
|
||
|
b = i
|
||
|
i = 2
|
||
|
e = exp(i * pi)
|
||
|
d = exp(b * pi)
|
||
|
% \texttt{i} is interpreted as the imaginary unit when assigning to a and b
|
||
|
% until it is defined to 2. In the subsequent expressions \textit{i} is
|
||
|
% interpreted as 2.
|
||
|
c = exp(1i * pi)
|
||
|
% Here \textit{1i} is interpreted as the imaginary unit, making $c = 1$
|
||
|
|
||
|
%% Problem 2
|
||
|
A = [1 -2 0 ; -2 1 -2; 0 -2 1]
|
||
|
Z = zeros(9,9)
|
||
|
B = ones(9,9) * 3
|
||
|
C = (eye(9) - 1) * -1
|
||
|
D = diag([[1:5],[4:-1:1]])
|
||
|
E = repmat(transpose(1:9), 1, 5)
|
||
|
|
||
|
%% Problem 3
|
||
|
A = fliplr(A)
|
||
|
B(2, :) = repmat(1, 1, 9)
|
||
|
C(1, :) = []
|
||
|
F = E(1:2, 1:2)
|
||
|
E(:, 1) = flipud(E(:, 1))
|
||
|
|
||
|
%% Problem 4
|
||
|
geteps
|
||
|
getxmin
|
||
|
getxmax
|
||
|
|
||
|
function myeps = geteps
|
||
|
y = 1;
|
||
|
x = 2 * y;
|
||
|
while 1 + y ~= 1
|
||
|
x = y;
|
||
|
y = y / 2;
|
||
|
end
|
||
|
myeps = x;
|
||
|
end
|
||
|
|
||
|
function xmin = getxmin
|
||
|
y = 1;
|
||
|
x = 2 * y;
|
||
|
while y ~= 0
|
||
|
x = y;
|
||
|
y = y / 2;
|
||
|
end
|
||
|
xmin = x;
|
||
|
end
|
||
|
|
||
|
function xmax = getxmax
|
||
|
y = 1;
|
||
|
x = y;
|
||
|
while y ~= +inf
|
||
|
x = y;
|
||
|
y = y * 2;
|
||
|
end
|
||
|
xmax = typecast(bitor(typecast(x, 'uint64'), 0x000FFFFFFFFFFFFF), 'double');
|
||
|
end
|
||
|
% geteps does not differ from eps, as getxmax does not differ from realmax.
|
||
|
% However, getxmin returns the nearest positive floating point value to 0
|
||
|
% including denormalized numbers, while realmin returns the smallest
|
||
|
% (ignoring sign) non denormalized floating point number.
|