Derivative of a function in a particular point

조회 수: 7 (최근 30일)
george veropoulos
george veropoulos 2021년 7월 16일
댓글: george veropoulos 2021년 7월 19일
Hi
I have an external function y= function fa(x)
y=sin(x./pi)
end
i want in the main program to find the derivative of fa in numerical point
thank you
George

채택된 답변

george veropoulos
george veropoulos 2021년 7월 16일
format long
x = 2 ;
h = sqrt(eps) ;
f1=(sin(x + h)./(x+h) - sin(x)./x) / h % -0.400000
% -0.435397773981094
f2=cos(x)./x-sin(x)./x^2
% -0.435397774979992
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 7월 16일
format long
x = 2 ;
h = sqrt(eps(x)) ;
f1=(sin(x + h)./(x+h) - sin(x)./x) / h
f1 =
-0.435397778831590
f2=cos(x)./x-sin(x)./x^2
f2 =
-0.435397774979992
george veropoulos
george veropoulos 2021년 7월 16일
yes sqrt(eps(x) )

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

추가 답변 (1개)

Jan
Jan 2021년 7월 16일
Use one of the quotients of differences to get a numerical approximation of the derivative:
x = 1.2345;
h = sqrt(eps);
dy_right = (fa(x + h) - fa(x)) / (h)
dy_left = (fa(x) - fa(x - h)) / (h)
dy_both = (fa(x + h) - fa(x - h)) / (2 * h)
function y = fa(x)
y = sin(x./pi)
end
  댓글 수: 9
Jan
Jan 2021년 7월 18일
@george veropoulos: The numerical analysis for the optimal choice of h is still worth to write a PhD thesis. As said before, the 2nd derivative of the function matters. To estimate this, you need a further small variation. Because this is expensive, if the function to be evaluated is huge, some heuristics are useful. This can be an important part of the processing time if you optimize an expensive function. A related field is the optimal choice of temproal and spatial steps sizes in finite element problems, e.g. the simulation of the earth clima.
The rule of thumb: If the function and the argument are about 1 and do not explode nearby, sqrt(eps) is a fair choice between the cancellation and discretization error. The "fairness" can be checkd by testing 10*sqrt(eps) and 0.1*sqrt(eps): If they reply the same derivative, you can assume to be on the right side. For a professional simulation, the explanation must sound more seriously. ;-)
george veropoulos
george veropoulos 2021년 7월 19일
thank you all! very helpfull dicussion
George

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by