CS计算机代考程序代写 %% Question 2.5 (c) Solution

%% Question 2.5 (c) Solution
xguess = linspace(-3,3,100);
x = zeros(3,length(xguess));

for case1=1:3
for i = 1:length(xguess)
x(case1,i) = fixed_point_g(xguess(i),case1);
end
end
for i = 1:3
subplot(3,1,i)
plot(xguess,real(x(i,:)),’-‘);
ylim([-1,3]);
xlim([-3,3])
end

%% Question 2.5 (f) Solution
x_newton_raphson = zeros(1,length(xguess));
x_secant = zeros(1,length(xguess));

for i = 1:length(xguess)
x_newton_raphson(i) = newton_raphson_5(xguess(i));
x_secant(i) = secant_5([xguess(i),xguess(i)+0.1]);
end
for i = 1:3
subplot(2,1,1)
plot(xguess,x_newton_raphson,’-‘);
subplot(2,1,2)
plot(xguess,x_secant,’-‘)
ylim([-1,3]);
xlim([-3,3])
end

function sol = fixed_point_g(xg,case1)

if case1==1
g = @(x)sqrt(1+2*x);
elseif case1==2
g = @(x)0.5*(x^2-1);
elseif case1==3
g = @(x)1/(x-2);
end

x = [xg];
tol = 10e-6; % Specified Tolerance
i = 1;
max_iter = 100; % Maximum number of iterations before the loops stops

while abs(x(i)^2-2*x(i)-1)>tol && itol && itol && i