(Exemplo 01) Criar um algoritmo em Matlab, usando o método de newton-rapshon, que ao digitar uma função na caixa de entrada, e um valor inicial. Nos forneça numa tabela as cinco primeiras iterações.
O método de Newton-Raphson é um algoritmo de busca de raízes que produz aproximações sucessivamente melhores para as raízes (ou zeros) de uma função real. Ele foi desenvolvido por Isaac Newton e Joseph Raphson
. A versão mais básica começa com uma função de uma única variável f definida para uma variável real x, a derivada da função f′ e um palpite inicial x0 para uma raiz de f. Se a função satisfazer suposições suficientes o palpite inicial estiver próximo, então, x1 é uma aproximação melhor da raiz do que x0.
Geometricamente, (x1, 0) é a interseção do eixo x e a tangente do gráfico de f em (x0, f(x0)): ou seja, o palpite melhorado é a raiz única da aproximação linear no ponto inicial. O processo é repetido até que um valor suficientemente preciso seja alcançado. O número de dígitos corretos aumenta aproximadamente duas vezes com cada etapa.
ALGORITMO
% Prompt the user to enter the function f_input = input('Enter the function: ', 's'); f = str2func(['@(x)' f_input]); % Prompt the user to enter the derivative of the function df_input = input('Enter the derivative of the function: ', 's'); df = str2func(['@(x)' df_input]); % Prompt the user to enter the initial value x0 = input('Enter the initial value: '); % Set the number of iterations N = 5; % Initialize the table T = zeros(N,2); % Perform the Newton-Raphson method for i = 1:N x1 = x0 - f(x0)/df(x0); T(i,:) = [i x1]; x0 = x1; end % Display the table T
(Exemplo 01) função f(x) = x^2 - 5*x + 6, com valor inicial x = 1.
ALGORITMO
% Define the function
f = @(x) x^2 - 5*x + 6;
% Define its derivative
df = @(x) 2*x - 5;
% Set the initial value
x0 = 1;
% Set the number of iterations
N = 5;
% Initialize the table
T = zeros(N,2);
% Perform the Newton-Raphson method
for i = 1:N
x1 = x0 - f(x0)/df(x0);
T(i,:) = [i x1];
x0 = x1;
end
% Display the table
T
SOLUÇÃO
T =
1.0000 1.6667
2.0000 1.9333
3.0000 1.9961
4.0000 2.0000
5.0000 2.0000
(Exemplo 02) função f(x) = x^4 - x^2 +1a , com valor inicial x = 1.
ALGORITMO
% Prompt the user to enter the function
f_input = input('Enter the function: ', 's');
f = str2func(['@(x)' f_input]);
% Prompt the user to enter the derivative of the function
df_input = input('Enter the derivative of the function: ', 's');
df = str2func(['@(x)' df_input]);
% Prompt the user to enter the initial value
x0 = input('Enter the initial value: ');
% Set the number of iterations
N = 5;
% Initialize the table
T = zeros(N,2);
% Perform the Newton-Raphson method
for i = 1:N
x1 = x0 - f(x0)/df(x0);
T(i,:) = [i x1];
x0 = x1;
end
% Display the table
T
SOLUÇÃO
T =
1.0000 2.0000
2.0000 1.6364
3.0000 1.5304
4.0000 1.5214
5.0000 1.5214
Algoritmo testado com a versão 2023 do Matlab.