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 次查看(过去 30 天)
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

采纳的回答

Torsten
Torsten 2024-9-1
移动:Torsten 2024-9-1
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

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by