Write a computer program (Matlab) which implements the method of inverse quadrat
ID: 649871 • Letter: W
Question
Write a computer program (Matlab) which implements the method of inverse
quadratic interpolation to solve the equation f(x) = 0. List the iterates
after the method reaches the the region of convergence, which for our
purposes will be the point at which the first significant digit of the
solution is obtained and then retained in all subsequent iterations. Stop
the iterations when five digits of the solution have been obtained. Test
the method for the functions
f(x) = x^5 - (cos x)^2 . The root is around 0.8477. and
f(x) = 128x^4 - 58x^3 + 126x^2 - 23x + 1 . Two roots are around
0.0686 and 0.1232
Explanation / Answer
function v = polyinterp(x,y,u)
n = length(x);
v = zeros(size(u));
for k = 1:n
w = ones(size(u));
for j = [1:k-1 k+1:n]
w = (u-x(j))./(x(k)-x(j)).*w;
end
v = v + w*y(k);
end
end
function [b,k] = iqi(f,x)
a = x(1);
b = x(2);
c = x(3);
k = 0;
while abs(c-b) > eps*abs(c)
x = polyinterp([f(a),f(b),f(c)],[a,b,c],0);
a = b;
b = c;
c = x;
k = k + 1;
disp(sprintf('x%d = %16.14f',k,c))
end
end
disp('Find a root of the function :')
J0 = @(x) (x*x*x*x*x - cos(x)*cos(x));
disp('Initial guesses are x0 = 0, x1 = 1, x2 = 5')
[x,iter] = iqi(J0,[1.0 2.1 3.5]);
disp(sprintf('A root of J0 is x = %f. It was computed in %d iterations. ',x,iter))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.