Create a function in MATLAB called newton bisect which combines Newton’s method
ID: 3283699 • Letter: C
Question
Create a function in MATLAB called newton bisect which combines Newton’s method and the bisection method in a way that uses the best features of each. The algorithm works as follows: 1. Start with an interval [a1, b1] that contains a root of the function f. As your first approximation x1, select x1 = a1.
2. Perform an iteration of Newton’s method on x1 to obtain x2. • If x2 is between a1 and b1, then set either [a2, b2] = [a1, x2] or [a2, b2] = [x2, b1] depending on which contains the root of the function. • If x2 is not between a1 and b1, then use x2 = a1 + b1 2 instead, and set either [a2, b2] = [a1, x2] or [a2, b2] = [x2, b1] depending on which contains the root of the function.
3. Repeat step 2 using x2 and the interval [a2, b2].
Explanation / Answer
%%% Find the roots of x^3-x-1 using bisection method and plot the error
f=@(x) x^3-x-1; %%% input function
a=1; %%% interval in which we have to find root
b=2;
for i=1:100
c=(a+b)/2;
if f(c)>0
b=c;
else a=c;
end
end
a=1; b=2; p = c;
for i=1:100
c=(a+b)/2;
er(i)=f(c)-f(p);
if f(c)>0
b=c;
else a=c;
end
end
fprintf('Root of given equation is %f',c)
plot(er);
title('Plot of error')
xlabel('Number of iterations')
ylabel('Error')
grid on;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.