Simple Question: gradient function Formula

조회 수: 3 (최근 30일)
Santino M
Santino M 2015년 2월 2일
편집: Santino M 2015년 2월 3일
in the following line, x is a 200 element column vector, h is a scalar eg.say 20 or 30
a = gradient(x,h)
can you please tell how 'a' is computed from x and h? Thanks in advance

채택된 답변

Roger Stafford
Roger Stafford 2015년 2월 2일
For 2 <= k <= 199, the computation is a central difference:
a(k) = (x(k+1)-x(k-1))/(2*h)
However, for the two endpoints it is:
a(1) = (x(2)-x(1))/h
a(200) = (x(200)-x(199))/h
In case h is a vector of the same length as x, then it becomes a divided difference:
a(k) = (x(k+1)-x(k-1))/(h(k+1)-h(k-1))
and
a(1) = (x(2)-x(1))/(h(2)-h(1))
a(200) = (x(200)-x(199))/(h(200)-h(199))
  댓글 수: 1
Santino M
Santino M 2015년 2월 3일
편집: Santino M 2015년 2월 3일
Thanks Star Strider I did try this by plotting. Thanks Roger for your reply. I did not understand the need of dividing by the scalar value h in a gradient. I initially assumed that the h denoted spacing between points eg. for h=20; grad(k) = (x(k-10) - x(k+10))/h. apparently it is not so.Can you please tell me how to use gradient to achieve the above. I want to set the interval for calculating the difference while calculating gradient. I can as well make a small function to do the same. If there is a way to get it done through an in built function it would be faster..especially if it is used a large no. of times (say > 10000000 times or more)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by