Value for Function with 2nd order Central difference scheme

조회 수: 15 (최근 30일)
VIKASH
VIKASH 2023년 8월 12일
답변: Anu 2023년 9월 30일
I am trying to write code for the above problem but getting wrong answer, Kindly help me to find the error in the code or suggest if there is any better alternate way to write code for the problem.
Right answer is 2.3563
c=1.5;
h=0.1;
x=(c-h):h:(c+h);
Fun=@(x) exp(x)-exp(-x)/2;
dFun=@(x) 2*exp(x)+2*exp(-x)/2;
F=Fun(x);
n=length(x);
dx= (F(:,end)-F(:,1))/(2*h)
dx = 4.6009

채택된 답변

Star Strider
Star Strider 2023년 8월 12일
See First and Second Order Central Difference and add enclosing parentheses to the numerator of your implementation of the cosh function.
  댓글 수: 2
VBBV
VBBV 2023년 8월 12일
편집: VBBV 2023년 8월 12일
c=1.5;
h=0.1;
x=(c-h):h:(c+h);
Fun=@(x) (exp(x)-exp(-x))/2; % parenthesis
dFun=@(x) 2*(exp(x)+exp(-x))/2; % parenthesis
F=Fun(x);
n=length(x);
dx= (F(:,end)-F(:,1))/(2*h)
dx = 2.3563
Anu
Anu 2023년 9월 30일
c = 1.5;
h = 0.1;
x = (c - h):h:(c + h);
Fun = @(x) (exp(x) - exp(-x)) / 2;
F = Fun(x);
n = length(x);
dx = (F(3) - F(1)) / (2 * h); % Corrected calculation of derivative at x=c

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

추가 답변 (1개)

Anu
Anu 2023년 9월 30일
  • c is the central point.
  • h is the step size.
  • x is a vector of values around c.
  • Fun is the function you want to calculate the derivative for.
  • F is the function values at the points in x.
  • dx calculates the derivative at the central point c using finite differences.

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by