|
|
Line 21: |
Line 21: |
| Read more [https://www.gnu.org/software/octave/doc/interpreter/Comments.html about comments]. | | Read more [https://www.gnu.org/software/octave/doc/interpreter/Comments.html about comments]. |
|
| |
|
| % Função e derivada
| | = Command evaluation = |
| f = @(x) 1 - x^3; % f(x) = 1 - x^3
| |
| f_prime = @(x) -3*x^2; % f'(x) = -3*x^2
| |
|
| |
|
| % Condição inicial e tolerância
| | The output of every command is printed to the console unless terminated with |
| x0 = 2; % ponto inicial
| | a semicolon <code>;</code>. The [https://www.gnu.org/software/octave/doc/interpreter/XREFdisp.html disp] command can be used to print output |
| tolerancia = 1e-6; % tolerância
| | anywhere. Use [https://www.gnu.org/software/octave/doc/interpreter/XREFquit.html exit] or [https://www.gnu.org/software/octave/doc/interpreter/XREFquit.html quit] to quit the console. |
| max_iter = 100; % máximo de iterações para evitar loop infinito
| | Read more [https://www.gnu.org/software/octave/doc/interpreter/Simple-Examples.html about command evaluation]. |
| iter = 0; % contador de iterações
| |
| erro = inf; % inicializando o erro como infinito
| |
|
| |
|
| % Método de Newton-Raphson
| | <syntaxhighlight lang="octave">t = 99 + 1 # prints 't = 100'</syntaxhighlight> |
| while erro > tolerancia && iter < max_iter
| | <syntaxhighlight lang="text">t = 100</syntaxhighlight> |
| iter = iter + 1; % incrementar contador de iteração
| |
| x1 = x0 - f(x0) / f_prime(x0); % fórmula de Newton-Raphson
| |
| erro = abs(x1 - x0); % erro absoluto entre iterações
| |
| x0 = x1; % atualizar x0 para o novo valor
| |
| end
| |
|
| |
|
| % Resultado
| | <syntaxhighlight lang="octave">t = 99 + 1; # nothing is printed |
| disp(['Raiz aproximada: ', num2str(x1)]); | | disp(t);</syntaxhighlight> |
| disp(['Número de iterações: ', num2str(iter)]);
| | <syntaxhighlight lang="text"> 100</syntaxhighlight> |
|
| |
|
| = Elementary math = | | = Elementary math = |
Line 159: |
Line 150: |
|
| |
|
|
| |
|
| % Função f(x) = 1 - x^3
| | = Plotting = |
| f = @(x) 1 - x^3;
| |
|
| |
|
| % Derivada de f(x) -> f'(x) = -3*x^2
| | The function [https://www.gnu.org/software/octave/doc/interpreter/XREFplot.html plot] can be called with vector arguments to |
| f_prime = @(x) -3*x^2;
| | create 2D line and scatter plots. Read more |
| | [https://www.gnu.org/software/octave/doc/interpreter/Two_002dDimensional-Plots.html about plotting]. |
|
| |
|
| % Função para aplicar o Método de Newton-Raphson
| | <syntaxhighlight lang="octave">plot (i / 10, w); |
| function [raiz, num_iteracoes, iteracoes_x, iteracoes_y] = newton_raphson(x0, tolerancia)
| | title ('w = sin (i / 10)'); |
| % Inicializar variáveis
| | xlabel ('i / 10'); |
| iter = 0; % Contador de iterações
| | ylabel ('w');</syntaxhighlight> |
| erro = inf; % Inicializa o erro como infinito
| |
| x = x0; % Definir o ponto inicial
| |
| iteracoes_x = x; % Inicializar vetor de iterações para o gráfico
| |
| iteracoes_y = f(x); % Inicializar valores de f(x) para o gráfico
| |
|
| |
| % Iterar até que o erro seja menor que a tolerância
| |
| while erro > tolerancia && iter < 100
| |
| iter = iter + 1; % Incrementa a iteração
| |
| x_new = x - f(x) / f_prime(x); % Fórmula de Newton-Raphson
| |
| erro = abs(x_new - x); % Calcula o erro absoluto
| |
| x = x_new; % Atualiza x
| |
|
| |
| % Armazena os valores de x e f(x) para o gráfico
| |
| iteracoes_x = [iteracoes_x, x];
| |
| iteracoes_y = [iteracoes_y, f(x)];
| |
| end
| |
|
| |
| % Retorna a raiz aproximada, número de iterações e os valores das iterações
| |
| raiz = x; % Raiz aproximada
| |
| num_iteracoes = iter; % Número de iterações
| |
| end
| |
|
| |
|
| % Condições de cada caso
| | [[File:Using octave-1.png|thumb|center]] |
| % Caso 1: x0 = 2 e tolerância = 1e-6
| |
| x0_case1 = 2;
| |
| tolerancia_case1 = 1e-6;
| |
| | |
| % Caso 2: x0 = 7 e tolerância = 1e-3
| |
| x0_case2 = 7;
| |
| tolerancia_case2 = 1e-3;
| |
| | |
| % Resultado para o primeiro caso (x0 = 2, tolerância = 1e-6)
| |
| [raiz_case1, iter_case1, iteracoes_x_case1, iteracoes_y_case1] = newton_raphson(x0_case1, tolerancia_case1);
| |
| disp(['Caso 1: Raiz aproximada: ', num2str(raiz_case1)]);
| |
| disp(['Número de iterações: ', num2str(iter_case1)]);
| |
| | |
| % Resultado para o segundo caso (x0 = 7, tolerância = 1e-3)
| |
| [raiz_case2, iter_case2, iteracoes_x_case2, iteracoes_y_case2] = newton_raphson(x0_case2, tolerancia_case2);
| |
| disp(['Caso 2: Raiz aproximada: ', num2str(raiz_case2)]);
| |
| disp(['Número de iterações: ', num2str(iter_case2)]);
| |
| | |
| % Plotagem do gráfico
| |
| figure;
| |
| | |
| % Caso 1: Plotando as iterações
| |
| subplot(2,1,1);
| |
| plot(iteracoes_x_case1, iteracoes_y_case1, '-o', 'LineWidth', 2, 'MarkerSize', 6);
| |
| title('Método de Newton-Raphson - Caso 1 (x0 = 2)');
| |
| xlabel('Iterações (x)');
| |
| ylabel('f(x)');
| |
| grid on;
| |
| | |
| % Caso 2: Plotando as iterações
| |
| subplot(2,1,2);
| |
| plot(iteracoes_x_case2, iteracoes_y_case2, '-o', 'LineWidth', 2, 'MarkerSize', 6);
| |
| title('Método de Newton-Raphson - Caso 2 (x0 = 7)');
| |
| xlabel('Iterações (x)');
| |
| ylabel('f(x)');
| |
| grid on;
| |
|
| |
|
| = Strings = | | = Strings = |