Taking derivative of a time based function?
56 ビュー (過去 30 日間)
古いコメントを表示
I'm new to MatLab and want to plot out some time based functions.
As an example, I have a function y = ((t/T).^n).*exp(-t/T). I want to plot the function vs time (t), along with the derivative of the function vs t.
I'm trying to use the diff function, but it's not working right. Any suggestions? Below is my actual code:
T = 8;
n = 1;
t = 0:0.1:100;
y = ((t/T).^n).*exp(-t/T);
x = diff(y,t);
plot(t,y,t,x)
0 件のコメント
回答 (3 件)
Azzi Abdelmalek
2013 年 12 月 6 日
編集済み: Azzi Abdelmalek
2013 年 12 月 6 日
T = 8;
n = 1;
t = 0:0.1:100;
y = ((t/T).^n).*exp(-t/T);
x = diff(y)./diff(t);
plot(t,y,t(1:end-1),x)
% or use gradient
T = 8;
n = 1;
t = 0:0.1:100;
y = ((t/T).^n).*exp(-t/T);
x=gradient(y,t)
plot(t,y,t,x)
0 件のコメント
Wayne King
2013 年 12 月 6 日
編集済み: Wayne King
2013 年 12 月 6 日
Are you trying to differentiate symbolically or numerically?
T = 8;
n = 1;
t = 0:0.1:100;
y = ((t/T).^n).*exp(-t/T);
x = diff(y)./diff(t);
subplot(211)
plot(t,y); title('y(t)');
subplot(212)
plot(t(2:end),x); title('dy/dt')
Symbolically
syms t; % requires symbolic toolbox
g(t) = t/8;
h(t) = exp(-t/8);
y = g(t)*h(t);
x = diff(y,t);
subplot(211)
ezplot(y,[0 100])
subplot(212)
ezplot(x,[0 100])
0 件のコメント
Roger Stafford
2013 年 12 月 6 日
The 'diff' function serves two purposes, one to take derivatives and the other to take finite differences. To make it take derivatives you have to declare the variables involved as type 'sym'. To use the numeric form as you have as an approximation you will need to divide the 'diff' output by the length of the 't' interval which in your case is 0.1 .
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!