function [ ] = Assignment3()
function [x, nsteps] = find_zero_bisection(f, a, b, epsilon)
if (sign(f(a)) ~= sign(f(b)))
mid = (a+b)/2;
val = f(mid);
nsteps = 0;
while (abs(val) > epsilon)
if (sign(f(a)) == sign(val))
a = mid;
else
b = mid;
end
mid = (a + b)/2;
val = f(mid);
nsteps = nsteps + 1;
end
x = mid;
end
end
root =
2.8284
nsteps =
17
function [x, nsteps] = find_zero_Newton(f, x0, epsilon)
h = 0.0001;
df = @(x) (f(x + h) - f(x))/h;
x = x0;
nsteps = 0;
while (abs(f(x)) > epsilon)
x = x- f(x)/df(x);
nsteps = nsteps + 1;
end
end
root =
2.8284
nsteps =
3
function draw_involute(fx, fy, t0, t1)
t_step = 0.01;
t = [t0: t_step : t1];
N = length(t);
L = 0;
iv_x = zeros(N,1);
iv_y = zeros(N,1);
for i = 2:N
dx = fx(t(i)) - fx(t(i-1));
dy = fy(t(i)) - fy(t(i-1));
dL = sqrt(dx^2 + dy^2);
L = L + dL;
iv_x(i) = fx(t(i)) - dx*L/dL;
iv_y(i) = fy(t(i)) - dy*L/dL;
plot([fx(t(i)) iv_x(i)], [fy(t(i)) iv_y(i)]);
end
plot(iv_x, iv_y);
end
f = @(x) x.^2 - 8;
[root, nsteps] = find_zero_bisection(f, 0, 5, 0.0001)
f = @(x) 6 - x.^2;
[root, nsteps] = find_zero_bisection(f, 0, 5, 0.0001)
root =
2.4495
nsteps =
14
f = @(x) x.^2 - 8;
[root, nsteps] = find_zero_Newton(f, 2.5, 0.0001)
Let a_n = 0.1/2^n, and b_n = nsteps from calling
find_zero_bisection(f,0,5,a_n) for f = @(x) x.^2 - 8;
Plot n vs b_n for n = 1..40
figure('Name', 'Bisection vs. Newton step count');
f = @(x) x.^2 - 8;
for n=1:40
an = 0.1/2^n;
[root, nsteps] = find_zero_bisection(f,0,5,an);
bn(n) = nsteps;
end
plot([1:40],bn);
hold on;
Let a_n = 0.1/2^n, and b_n = nsteps from calling
find_zero_Newton(f,2.5,a_n) for f = @(x) x.^2 - 8;
Plot n vs b_n for n = 1..40
for n=1:40
an = 0.1/2^n;
[root, nsteps] = find_zero_Newton(f,2.5,an);
bn(n) = nsteps;
end
plot([1:40],bn);
figure('Name', 'Involute of a parabola');
fx = @(t) t;
fy = @(t) t.^2;
t = [-3:0.001:3];
plot(fx(t), fy(t));
hold on;
draw_involute(fx,fy,0,3);
a = zeros(10,1);
a(1) = 2;
dH = 0.0001;
H = [-1:dH:1];
r = @(h) sqrt(1-h.^2);
for dim = 2:10
a(dim) = sum(a(dim-1).* r(H).^(dim-1) ).*dH;
end
a
a =
2.0000
3.1416
4.1888
4.9348
5.2638
5.1677
4.7248
4.0587
3.2985
2.5502
end