How to differentiate a piecewise function?

26 ビュー (過去 30 日間)
ahed salman
ahed salman 2018 年 7 月 7 日
コメント済み: ahed salman 2018 年 7 月 7 日
Hi to all. I have a piecewise function and I want to differentiate it but the derivative will not exist at endpoints. I tried to interpolate it such that the edges are more smooth and the derivative is continuous, but when I plot it, I still get the harsh edges. What to do now? This is my code:
t=0:60;
L =(15-t/2).*(t>=0 & t<=20)+...
(5).*(t>20 & t<=40 )+(-15+t/2).*(t>40 & t<=60 );
P=interp1(t,L,'pchip');
hold on
plot (t,P,'linewidth',4)
xlim([0 70])
ylim([0 20])

採用された回答

Image Analyst
Image Analyst 2018 年 7 月 7 日
Since you know the formula, you have an advantage - you can just use the known derivative:
t=0:60;
L =(15-t/2).*(t>=0 & t<=20)+...
(5).*(t>20 & t<=40 )+(-15+t/2).*(t>40 & t<=60 );
P=interp1(t,L,'pchip');
% Plot L vs. t
subplot(3, 1, 1);
plot (t, L, 'LineWidth', 4)
xlim([0 70])
ylim([0 20])
grid on;
xlabel('t', 'FontSize', 20);
ylabel('L', 'FontSize', 20);
% Plot P vs. t
subplot(3, 1, 2);
plot (t, P, 'LineWidth', 4)
xlim([0 70])
ylim([0 20])
grid on;
xlabel('t', 'FontSize', 20);
ylabel('P', 'FontSize', 20);
% Since we know the formula and when it starts and stops each piece
% we can compute the derivative analytically:
dLdt = zeros(1,length(L));
range1 = t>=0 & t<=20;
dLdt(range1) = -0.5;
range2 = t>40 & t<=60;
dLdt(range2) = 0.5;
% Plot dLdt vs. t
subplot(3, 1, 3);
plot (t, dLdt, 'b^-', 'LineWidth', 2)
xlim([0 70])
grid on;
xlabel('t', 'FontSize', 20);
ylabel('dLdt', 'FontSize', 20);
ax = gca;
ax.XAxisLocation = 'origin';
  1 件のコメント
ahed salman
ahed salman 2018 年 7 月 7 日
thank you very much

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by