Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a diary file called \"lab5.txt.\" The function sin(x) has a fixed point o

ID: 3825669 • Letter: C

Question

Create a diary file called "lab5.txt." The function sin(x) has a fixed point of 0. Start with an initial guess of 2 and use the method of fixed point iteration to derive this fixed point. Why do you think this algorithm is converging so slowly? If you want to find a root of f(x), then f(x) = 0 f(x) + x = x This means you can always try fixed point iteration to try and find a root of function. Use the method of fixed point iteration to find roots of the following functions on the given intervals. sin (x), [-5, 5]. In(x), (0, 2) -ln(x), (0.2) The golden ratio is the positive solution to the equation x^2 - x - 1 = 0. Use Newton's Method to find the golden ratio. Newton's Method is an open root finding method that uses the following formula. When Newton's Method works, the sequence {x_n} converges to a root of f(x). x_n+ 1 = x_n - f(x)/f'(x) Use Newton's Method with f(x) = sin(x) to estimate pi. Use an initial of 3. Turn the diary file off and upload "lab5.txt" to Canvas.

Explanation / Answer

2.

% Finding the nontrivial root of
% f(x) = sin(x)
% using the Simple Fixed-Point Iteration

clear all

x = 2.0 %initial guess
Es = 0.1 %tolerance
Ea = 1000; %randomly large relative approximate error
xold = x;   
n = 0; %iteration counter

while Ea > Es
x = sin(x) + x;
Ea = abs((x-xold)/x)*100;
xold = x;
n = n + 1;
end

x %the root
n %number of iterations

3. a)

function y = fixedpoint(g,p0,tol,max1)
for k=-5:max1
p = g(p0);
abserr = abs(p-p0);
relerr = abserr/( abs(p)+eps );
% eps is a MATLAB defined constant for machine epsilon, to avoid
% division by 0
if (abserr<tol) & (relerr<tol)
break % jump out of the loop; we’re done
end
p0 = p;
end
if (k==max1)
disp("The algorithm did not converge")
end
y = p

g = sin(x)
p0 = 2
tol = 0.1
max1 = 5
[y] = fixedpoint(g,p0,tol,max1)

b)

function y = fixedpoint(g,p0,tol,max1)
for k=0:max1
p = g(p0);
abserr = abs(p-p0);
relerr = abserr/( abs(p)+eps );
% eps is a MATLAB defined constant for machine epsilon, to avoid
% division by 0
if (abserr<tol) & (relerr<tol)
break % jump out of the loop; we’re done
end
p0 = p;
end
if (k==max1)
disp("The algorithm did not converge")
end
y = p

g = ln(x)
p0 = 0
tol = 0.1
max1 = 2
[y] = fixedpoint(g,p0,tol,max1)

c)

% Finding the nontrivial root of
% f(x) = -ln(x)
% using the Simple Fixed-Point Iteration

clear all

x = 0.2 %initial guess
Es = 0.1 %tolerance
Ea = 1000; %randomly large relative approximate error
xold = x;   
n = 0; %iteration counter

while Ea > Es
x = -ln(x) + x;
Ea = abs((x-xold)/x)*100;
xold = x;
n = n + 1;
end

x %the root
n %number of iterations

4.

xk = 3
fxk = xk^2-xk-1
fprintf("Initial guess : x0 =%25.15e fx0 = %d " , xk, fxk);

for k = 1: 10
xk = xk - (xk^2-xk-1)/(2*xk-1);
fxk = xk^2-xk-1;
fprintf("iteration : %li xk = %25.15e , fxk = %d ", k, xk, fxk);
end

5.

x = 3;
Tol = 0.0000001;
count = 0;
dx=1; %this is a fake value so that the while loop will execute
f=-13; % because f(-2)=-13
fprintf('step x dx f(x) ')
fprintf('---- ----------- --------- ---------- ')
fprintf('%3i %12.8f %12.8f %12.8f ',count,x,dx,f)
xVec=x;fVec=f;
while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed
count = count + 1;
fprime = cos(x);
xnew = x - (f/fprime); % compute the new value of x
dx=abs(x-xnew); % compute how much x has changed since last step
x = xnew;
f = sin(x); % compute the new value of f(x)
fprintf('%3i %12.8f %12.8f %12.8f ',count,x,dx,f)
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote