フィルターのクリア

How to calculate a numerical approximate derivative vector of a function?

4 ビュー (過去 30 日間)
Jabir Al Fatah
Jabir Al Fatah 2014 年 5 月 31 日
コメント済み: Star Strider 2014 年 5 月 31 日
I have a given formmula: Yprimenum(i) = (Y(i+1) – Y(i)) / ∆X, where ∆X is the X step length, or equivallently X(i) – X(i-1). And I also have two given functions: X= [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 ] Y= [5 6 7 7.5 7.5 7.5 6.5 2.5 -5 -6 -6]
Now my task is to plot this function, Y, and calculate and plot the corresponding Yprimenum in the same graph. This is what I tried:
x= [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 ]
y= [5 6 7 7.5 7.5 7.5 6.5 2.5 -5 -6 -6]
yprime=2.*x;
Yprimenum=zeros(1, length(x)-1);
for i= 1:length(x)-1;
Yprimenum(i)=(y(i+1)-y(i))./(x(i+1)-x(i));
end
figure;
hold on;
plot(x,y);
plot(x,yprime);
plot(x,Yprimenum(i));
hold off;
shg;

採用された回答

Star Strider
Star Strider 2014 年 5 月 31 日
Your derivative, Yprimenum, is by definition one element shorter than x, so you have to eliminate the last entry of x to plot it:
x= [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 ]
y= [5 6 7 7.5 7.5 7.5 6.5 2.5 -5 -6 -6]
yprime=2.*x;
Yprimenum=zeros(1, length(x)-1);
for i= 1:length(x)-1;
Yprimenum(i)=(y(i+1)-y(i))./(x(i+1)-x(i));
end
figure;
hold on;
plot(x,y,'-b');
plot(x,yprime,'g');
plot(x(1:end-1),Yprimenum,'r');
hold off;
shg;
This is unavoidable with the method you used (and that the diff function uses) but there are ways to deal with it. This is one such.
  2 件のコメント
Jabir Al Fatah
Jabir Al Fatah 2014 年 5 月 31 日
perfect, thanks.
Star Strider
Star Strider 2014 年 5 月 31 日
My pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 5 月 31 日
編集済み: Andrei Bobrov 2014 年 5 月 31 日
x= [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 ]
y= [5 6 7 7.5 7.5 7.5 6.5 2.5 -5 -6 -6]
Yprimenum = diff(y)./diff(x);
other variant
Yprimenum = gradient(y,x);

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by