Solutions to Week 2 exercises 8/14/20, 3:31 PM
Solutions to Week 2 exercises
Contents
Exercise Set 1 Exercise set 2 Exercise set 3
Exercise Set 1
ans =
1.8006e-06
% (a)
f = @(x) x.^2.*cos(pi*x).*exp(-x.^2);
f(4)
ezplot(f,[-5,5])
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 1 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
(b)
function [ fixedPoint,flag ] = myiterator( func, criterion, x0 ,tol )
%MYITERATOR A mega-iterator
% performs fixed point iterations on a function
% input: func function handle for the fnction to be iterated
%
%
%
%
%
%
MAXITS = 10;
flag = 1;
i.e. xp1 = func(x)
criterion function handle to a logical valued function
that gives the convergence criterion for stopping the
iteration
x0 the initial guess
tol the tolerance (used in the criterion)
% default to indicate convergence
x = x0;
xp1 = func(x);
count = 1;
while ~criterion(x,xp1,tol) && (count < MAXITS)
x = xp1;
xp1 = func(x);
count = count +1;
end
if count >= MAXITS
flag = 0;
fixedPoint = nan;
else
fixedPoint = xp1;
end
(d)
abstol = @(x1,x2,tol) abs(x1-x2) < tol;
reltol = @(x1,x2,tol) abs(x1-x2) < tol*max(abs(x1,x2));
%(c)
type myiterator
type Ex1eDriver
Ex1eDriver
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 2 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
function Ex1eDriver( )
%EX1E A driver for Ex1e
% Detailed explanation goes here
newtonRoot5 = @(x) x/2+5/(2*x);
abstol = @(x1,x2,tol) abs(x1-x2) < tol;
reltol = @(x1,x2,tol) abs(x1-x2) < tol*max(abs(x1,x2));
[root,flag] = myiterator(newtonRoot5,abstol,2,1e-10)
root = 2.2361
flag = 1
(e)
function [root,count] = NewtonSquareRoot(x0,a)
% A function to compute the square root of a
% Uses Newton's method
% Now with a while loop
%
if nargin < 2
a = 5; % default value
end
if x0 == 0
disp('Error: zero not allowed as input');
root = nan; % give return value
return
else
x0 a
the initial guess
the number to find the square root of
default value : 5
% Input:
%
%
% Output: root
% count the number of iterations required
% Usage: [root, count] = NewtonSquareRoot(2,9)
the estimate of the root
type NewtonSquareRoot
NewtonSquareRoot(2,5)
NewtonSquareRoot(2)
NewtonSquareRoot(2,10)
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 3 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
xold = x0; % initialize xold
count = 1; % initialize count
xnew = xold/2+a/(2*xold); % do 1 iteration before test
while abs(xnew^2-a) >= 1e-10
xold = xnew;
xnew = xold/2+a/(2*xold);
count = count + 1;
end
root = xnew;
end
end ans =
2.2361
ans = 2.2361
ans = 3.1623
Exercise set 2
no answers – just for python devotees
Exercise set 3
(a)
clf; % clears the current figure window
theta = linspace(0,2*pi,60);
y = sin(theta);
plot(theta,y,’–o’);
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 4 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
(b)
clf
n = 1:20;
p = 0.145; % for example
y = 7.8*p.^n;
semilogy(n,y);
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 5 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
(c)
clf
n = 1:100;
p = 1.45; % for example
y = 7.8*n.^p;
loglog(n,y);
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 6 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
(d)
clf
x = linspace(0,1);
y1=x.^2; y2 = sin(x);y3 = sin(y1);
plot(x,y1,x,y2,x,y3); % or use `hold on’
% easier to put labels on using the Plotting tools in the figure window
% Insert -> TextArrow
% then Save As … your favourite graphics format – here .fig
open(‘Ex3d.fig’);
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 7 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 8 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
(e)
clf
a= 2; b = 3;
t = linspace(0,2*pi);
x = a*cos(t); y = b*sin(t);
plot(x,y);
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 9 of 10
Solutions to Week 2 exercises 8/14/20, 3:31 PM
Published with MATLAB® R2016b
https://app.lms.unimelb.edu.au/bbcswebdav/pid-7228486-dt-content-rid-55100855_2/courses/MAST30028_2019_SM2/answers2_2018.html Page 10 of 10