Using MatLab For finding the local extrema of some function |g(x)|, one evaluate
ID: 3834159 • Letter: U
Question
Using MatLab
For finding the local extrema of some function |g(x)|, one evaluates |g(a)|, |g(b)|, and |g(x_critical)| where x_critical represents critical point(s) which satisfy: g'(x_critical) = 0 Define, f(x) g'(x), and suppose that f(x) is a polynomial of degree n with n distinct and real roots. i.e. f(x) = a_0 + a_1x + a_2x^2 + ... + a_n x^n. Then one can implement Newton's method plus the "deflation" method in order to find all the roots of f(x) (critical points of g(x)). Your algorithm should go something like this: I given the coefficients of a polynomial f(x), a_i, i = 0...n, k = n II Find a root, x_k, of f(x) using Newton's method. III Deflate the polynomial f(x) using the factor x - r where r = x_k. i.e., replace f(x) with h(x) = b_1 + b_2x + ... + b_k x^k - 1 b_k = a_k: b_i - 1 = a_i - 1 + rb_i i = k, ..., 2 IV decrement k and go back to (II) if k greaterthanorequalto 1. The roots of the derivatives of the Legendre polynomials have important practical uses in approximation theory. The sixth order Legendre polynomial is P_6(x) = 1/16(231 x^6 - 315 x^4 + 105 x^2 - 5) Find the roots of P'_6 (x) using your new deflation method.Explanation / Answer
Matlab function DeflateNewtonRootFinding.m
function X = DeflateNewtonRootFinding(a)
% in put a is the vector of coefficients of f(x) like [a0,a1,...,an]
n = length(a); % Computing the length of a
f = @(x) a(n); % creating a function handler of the function f
for k = n:-1:2
f = @(x) f(x).*x+a(k-1);
end
df = @(x) a(n)*(n-1); % Derivative of f
for k = n:-1:3
df = @(x) df(x).*x+a(k-1).*(k-2);
end
% loop to get n roots
for k = n:-1:2
X(k-1) = NewtonMethod(f,df,k); % Calling the function NewtonMethod
% Deflate the polynomial and up date f and df and call the function
% again
b(k-1) = a(k);
for i = k:-1:3
b(i-2) = a(i-1)+X(k-1)*b(i-1);
end
clear a;
a = b;
clear b;
f = @(x) a(k-1);
df = @(x) a(k-1)*(k-1);
for i = k:-1:3
f = @(x) f(x).*x+a(i-2);
end
for i = k:-1:4
df = @(x) df(x).*x+a(i-2).*(i-2);
end
end
end
function x = NewtonMethod(f,df,x)
tol = 10^-3;
y = f(x); % function value at x
dy = df(x); % function derivative at x
while (abs(y)>tol)&&(dy~=0)
x=x-y/dy; % finding xn+1
y = f(x); % function value at xn+1
dy = df(x);%function derivative at xn+1
end
if (dy==0) % Checking devision by zero
x = NewtonMethod(f,df,x+5); % incase of devision by zero update the initial guess
end
end
Testing the Program for (x-1)(x-2)(x-3) that is x^3-6x^2+11x-6
>> X = DeflateNewtonRootFinding([-6,11,-6,1])
X =
2.0000 1.0009 3.0000
Testing the function for given Legender polynomial
>> X = DeflateNewtonRootFinding([-5,0,105,0,-315,0,231])
X =
-0.9325 -0.6612 -0.2386 0.2386 0.6612 0.9325