Discrete derivative via Matlab

조회 수: 138 (최근 30일)
Giacomo Alessandroni
Giacomo Alessandroni 2013년 10월 22일
댓글: Giacomo Alessandroni 2013년 10월 22일
Hi to all,
I have tried to derivative a simple sine function with diff.
Here are the code:
x = 0:pi/100:10*pi;
y = sin(x);
figure;
plot(y)
y1 = diff(y);
figure;
plot(y1)
The question is this: why the derivative (a cosine function) isn't with max and min of 1 and -1 like sin(x)?
If I do:
Y1=max(y1);
Y=max(y);
Scale = Y/Y1
I obtain Scale = 31.8362.
The same is for second derivative: to obtain function in scale I must apply Scale factor two times.
Who can explain me the correct way to obtain a discrete derivative via Matlab?

채택된 답변

Laurent
Laurent 2013년 10월 22일
If you want to calculate the derivative like this, you have to divide the change in y (dy) by the change in x (dx), so dy/dx. You get the dy's by running the 'diff' command. The dx is equal to the distance between your x-values, in this case pi/100.
So in your case:
y1=diff(y)/(pi/100);
  댓글 수: 1
Giacomo Alessandroni
Giacomo Alessandroni 2013년 10월 22일
Thank you very much.
Now I write the correct code for all:
dx = pi/100;
x = 0:dx:10;
y = sin(x);
% y' = dy/dx
y1 = diff(y)/dx;
plot(x(1:end-1), y(1:end-1), x(1:end-1), y1)

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by