EECS 639- HW4
Waleed Khan
Problem 1.b:
MATLAB script:
%% Problem 1b: Bisection method
clc
clear
format long
a = 2;
b = 3;
iterCount = 0;
while abs(b-a)>1e-10
m = a + (b-a)/2;
fa = a^4 - a^3 - 10;
fb = b^4 -b^3 - 10;
if sign(fa) == sign(fb)
a = m;
else
b = m;
end
iterCount = iterCount +1;
end
sprintf('tolerance achieved after %i iterations',iterCount)
Output:
type Dhw4p1
eecs639hw4p1
ans =
'tolerance achieved after 34 iterations'
diary off
Problem 2:
MATLAB script:
%% Problem 2: Fixed-point iteration
clc
clear
format long
xGuess = 2;
errorTol = 1e-10;
xDiff = 1; % difference between successive iteration ouputs
iterCount = 0;
while (xDiff > errorTol)
xNew = (1/xGuess)+(xGuess/2); % x = g(x) (R.H.S)
xDiff = abs(xNew-xGuess);
xGuess = xNew;
iterCount = iterCount+1;
end
sprintf('The value of sqrt(2) is found to be %1.10d after %i
iterations',xNew,iterCount)
Output:
eecs639hw4p2
ans =
'The value of x is 1.5000000000e+00 after 1 iterations'
ans =
'The value of x is 1.4166666667e+00 after 2 iterations'
ans =
'The value of x is 1.4142156863e+00 after 3 iterations'
ans =
'The value of x is 1.4142135624e+00 after 4 iterations'
ans =
'The value of x is 1.4142135624e+00 after 5 iterations'
ans =
'The value of sqrt(2) is found to be 1.4142135624e+00 after 5 iterations'
diary off
Problem 3:
MATLAB script:
%% Problem 3: Finding convergence rate
clc
clear
format long
x = [0 0.181194 0.330597 0.405299 0.442649 0.449740 0.447224 0.447422 0.447432];
e = zeros(1,8);
C = zeros(3,7);
% r values for linear, superlinear and quadratic convergence
r = [1 (1+sqrt(5))/2 2];
for i = 1:length(x)-1
e(i) = abs(x(i+1)-x(i));
end
for i = 1:3
for j = 1:length(e)-1
C(i,j) = e(j+1)/(e(j))^r(i);
end
end
e,C
Output:
e =
0.181194000000000 0.149403000000000 0.074702000000000 0.037350000000000
0.007091000000000 0.002516000000000 0.000198000000000 0.000010000000000
C =
1.0e+02 *
0.008245471704361 0.005000033466530 0.004999866134776 0.001898527443106
0.003548159638979 0.000786963434022 0.000505050505051
0.023697789874470 0.016190002471876 0.024847194127598 0.014480708483353
0.075568182522894 0.031798287127879 0.098202285153803
0.045506317562177 0.033466754124951 0.066930820256158 0.050830721368293
0.500375072483285 0.312783558832347 2.550760126520872
Problem 4:
MATLAB script:
%% Problem 4: Mortgage interest rate calculation
clc
clear
format long
n = 360; % payment period
i = 0.05; % initial guess value for interest rate/period (in decimal)
errorTol = 1e-10;
iDiff = 1; % difference between successive iteration ouputs
iter = 0;
while iDiff > errorTol
f = 135*i+(1+i)^(-n) -1;
df = 135-n*(1+i)^(-n-1);
iNew = i - f/df;
iDiff = abs(iNew-i);
i = iNew;
iter = iter+1;
end
sprintf("the max interest rate(per month) is %f %%",100*i)
sprintf("the max interest rate(per year) is %f %%",12*100*i)
Output:
type Dhw4p3
eecs639hw4p4
ans =
"the max interest rate(per month) is 0.674992 %"
ans =
"the max interest rate(per year) is 8.099901 %"
diary off
Problem 5:
MATLAB script:
%% Problem 5: Fractals
clc
clear
format long
failureCount = 0;
% Number of grid points
n = 150;
% Generation of grid coordinates (x0,y0)
x = linspace(-2,2,n);
y = linspace(-2,2,n);
% Generation of matrix of starting complex no. guess values: z0 = x0+y0i
z = zeros(length(x),length(y));
for j = 1:length(x)
for k = 1:length(y)
z(j,k) = x(j) + y(k)*1i;
end
end
zColor = cell(length(x),length(y)); % array is cell type as it has to store
% characters (r,g,b,k) rather than single/double type values
% Function call for Newton's method and generation of color matrix
for j = 1:length(x)
for k = 1: length(x)
zColor{j,k} = NewtonMethod(z(j,k));
end
end
% Plotting the values
for j = 1:length(x)
for k = 1:length(y)
color = zColor{j,k};
scatter(x(j), y(k), 25, color, 'filled');
hold on
end
end
%% Function for Newton's Method for f(z) = z^3 -1
function color = NewtonMethod(z0)
z1 = 1;
z2 = -0.5 + sqrt(3)*1i/2; z2 = round(z2,3);
z3 = -0.5 - sqrt(3)*1i/2; z3 = round(z3,3);
errorTol = 1e-4;
iterCount = 0;
zDiff = 1;
zGuess = z0;
while zDiff > errorTol && iterCount < 20
f = zGuess^3 - 1;
df = 3*zGuess^2;
zNew = zGuess - f/df;
zDiff = abs(zNew-zGuess);
zGuess = zNew;
iterCount = iterCount+1;
end
% root defines the root of f(z) within 20 iterations
root = round(zNew,3);
switch root
case z1
color = 'r';
case z2
color = 'g';
case z3
color = 'b';
otherwise
color = 'k';
end
end
Output: