in this code for finding root by newton raphson method i gave derivative of f in g. instead of giving derivative directly how can i get derivative using any code?

2 visualizaciones (últimos 30 días)
this is a matlab code for finding root for a function using newton raphson method... here i knew derivative of f so that i gave that in g..suppose f is a function whose derivative is little difficult to find, then how can i modify this code for using any function f?
clear
close all
clc
f= @(x) x^4 -x^2 - 80;
g =@(x) 4*x^3-2*x;
x1=3.1;
e=10^-5;
n=100;
for i =1:n
x2= x1-(f(x1)/g(x1));
fprintf('x%d=%.6f\n',i,x2)
if abs(x2-x1)<e
break
end
x1=x2;
end

Respuesta aceptada

Torsten
Torsten el 1 de Sept. de 2024
Movida: Torsten el 1 de Sept. de 2024
syms x
f = x^4-x^2-80;
g = jacobian(f);
f = matlabFunction(f)
f = function_handle with value:
@(x)-x.^2+x.^4-8.0e+1
g = matlabFunction(g)
g = function_handle with value:
@(x)x.*-2.0+x.^3.*4.0

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by