Error in using diff for derivative

조회 수: 6 (최근 30일)
Anantha Krishna B
Anantha Krishna B 2022년 1월 18일
댓글: Torsten 2022년 1월 19일
Hello
I am writing a function for calculating a root of a function using Newton's Method. In this i need to calculate value of derivative of the function. So I used diff but I am getting an error. I am thinking that the error is because diff only acts on symbolic functions and this somehow causes error. Is there any alternative way?
Below is my code
function [root] = Newton_rap_fun(a, error, f)
% function for newton's method
% a is the initial approximation, error is the error tolerance given by
% user
% f is the given function
while (true)
if (diff(f(a)) ~= 0)
b = a - f(a)/diff(f(a));
if (abs(b-a) <= error)
root = b;
break
end
else
root = a;
end
end
  댓글 수: 3
Anantha Krishna B
Anantha Krishna B 2022년 1월 19일
@Torsten Thank you, this is a clever way. But is there any function that evaluates the derivative at point? I am asking because there will be some situations in which I need to evaluate higher order derivatives.
Torsten
Torsten 2022년 1월 19일
Numerical difference approximations exist also for derivatives of order n>1:
Or use symbolic maths and MATLAB's diff-command.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by