Numerical derivative central difference

조회 수: 4 (최근 30일)
Arctgx
Arctgx 2020년 3월 24일
답변: Ameer Hamza 2020년 3월 25일
Hello, i try to write a code about central difference. But something is wrong. My code is below. What is wrong?
function derivativecentral(numericalvalue, step, n)
centraltable=[0 -1 0 1 0;0 1 -2 1 0;-1 2 0 -2 1;1 -4 6 -4 1];
x=numericalvalue-2*step: step:numericalvalue+2*step;
m=2;
if (n/2)==floor(n/2); m=1; coeff = centraltable(n,:);
der=sum(coeff.*fonk(x)) / (m*step^n);
display(der);
end
end
function [f] = fonk(x) f=x*exp(x); end
At the end of code, i am writing derivativecentral(0,0.1,1)
But there is nothing.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 25일
The code displays nothing because the condition did not become true. Try the following code with a different input
derivativecentral(0,0.1,4)
function derivativecentral(numericalvalue, step, n)
centraltable=[0 -1 0 1 0;0 1 -2 1 0;-1 2 0 -2 1;1 -4 6 -4 1];
x=numericalvalue-2*step: step:numericalvalue+2*step;
m=2;
if (n/2)==floor(n/2); m=1; coeff = centraltable(n,:);
der=sum(coeff.*fonk(x)) / (m*step^n);
display(der);
end
end
function [f] = fonk(x)
f=x.*exp(x); % <---- this is syntax for element-wise multiplication
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by