Why am I getting this Error Message?

조회 수: 1 (최근 30일)
Zifeng Qiu
Zifeng Qiu 2020년 6월 21일
댓글: Zifeng Qiu 2020년 6월 21일
I am trying to take the second order of central difference derivative of a set data that was provided, why did I get an error message like this? I checked the formula many many times and it is correct. Don't know why, since the first order derivative works perfectly.
  댓글 수: 1
dpb
dpb 2020년 6월 21일
What is height(j-1) when j==1?

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

답변 (1개)

Monalisa Pal
Monalisa Pal 2020년 6월 21일
Becasue the loop starts from j = 1 and when you are trying to access height(j-1) on line 6 during slope1 evaluation height(0) becomes undefined. You may follow this link for more description: https://www.mathworks.com/matlabcentral/answers/494553-first-and-second-order-central-difference
In short your for loop can start from 2 and you calculate slope(1) separately. Or you may skip slope1 calculation when j = 1 as follows:
for j = 1:length(height)-1
slope(j) = (height(j+1) - height(j))/h;
if j ~=1
slope1(j) = (height(j+1) - 2*height(j) + height(j-1))/h^2;
end
end
  댓글 수: 1
Zifeng Qiu
Zifeng Qiu 2020년 6월 21일
Thank you, this is super helpful

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by