# Plotting the derivative of an "switch-funktion"

1 view (last 30 days)
Jann B on 6 Apr 2020
Commented: Jann B on 16 Apr 2020
Hello,
we got some switch funktions which i plotted with the code shown below.
The only thing i need to fix, is the plot of the derivative of these funktion.
Where the dirac should be shown (just a peak), nothing appears.
Can someone give me a hint?
%% ÜA Karlsruhe
% 2
% 2.1
clc, clear, clf
set(0,'DefaultFigureWindowStyle','docked')
% Signal A
figure('Name', 'RT_Karlsruhe 2.1', 'NumberTitle', 'off')
subplot(4, 1, 1)
hold on, grid on, axis equal
start = -2;
ende = 10;
t = linspace(start,ende,5000);
a = @(t) (t-2).*(heaviside(t-2) - heaviside(t-4)) + (t-6).*(heaviside(t-4) - heaviside(t-6));
A = a(t);
a_derivative = @(t) heaviside(t-2) - 4*dirac(t-4) - heaviside(t-6);
A_derivative = a_derivative(t);
syms x
diff_test = diff((x-2).*(heaviside(x-2) - heaviside(x-4)) + (x-6).*(heaviside(x-4) - heaviside(x-6)));
% x = t;
%
% Diff_test = diff_test(x);
plot(t,A, 'g', 'LineWidth', 2)
plot(t,A_derivative, 'r--', 'LineWidth', 2)
fplot(diff_test, [-2 10], 'bo', 'LineWidth', 2)
title('Signal A')
xlabel('Zeit "t"')
ylabel('a(t) und a_derivative(t)')
%% Signal B
subplot(4,1,2)
hold on, grid on, axis equal
% Beide Funktionen für Signal B funktionieren
b = @(t) heaviside(t-2) - heaviside(t-8) + heaviside(t-4) - heaviside(t-6);
% b = @(t) heaviside(t-2) - heaviside(t-4) + 2*(heaviside(t-4) - heaviside(t-6)) ...
% + heaviside(t-6) - heaviside(t-8);
B = b(t);
b_derivative = @(t) dirac(t-2) - dirac(t-8) + dirac(t-4) - dirac(t-6);
B_derivative = b_derivative(t);
plot(t,B, 'g', 'LineWidth', 2)
plot(t,B_derivative, 'r--', 'LineWidth', 2)
title('Signal B')
xlabel('Zeit "t"')
ylabel('b(t) und b_derivative(t)')
%% Signal C
subplot(4,1,3)
hold on, grid on, axis equal
c = @(t) -2*heaviside(t-1) + 2*heaviside(t-3) + (t-4).*heaviside(t-4) - (t-4).*heaviside(t-6);
C = c(t);
c_derivative = @(t) -2*dirac(t-1) + 2*dirac(t-3) + heaviside(t-4) - heaviside(t-6);
C_derivative = c_derivative(t);
plot(t,C, 'g', 'LineWidth', 2)
plot(t,C_derivative, 'r--', 'LineWidth', 2)
title('Signal C')
xlabel('Zeit "t"')
ylabel('b(t) und c_derivative(t)')
%% Signal D
subplot(4,1,4)
hold on, grid on, axis equal
% d = @(t) 2*t.*(heaviside(t+1) - heaviside(t-1)) + (3-t).*(heaviside(t-1) - heaviside(t-3));
d = @(t) (2*t).*heaviside(t+1) - (3*t-3).*heaviside(t-1) - (3-t).*heaviside(t-3);
D = d(t);
d_derivative = @(t) 2*heaviside(t+1) + (2*t).*dirac(t+1) - 3*heaviside(t-1) ...
+ heaviside(t-3);
D_derivative = d_derivative(t);
plot(t,D, 'g', 'LineWidth', 2)
plot(t, D_derivative, 'r--', 'LineWidth', 2)
xticks(start:1:ende)
title('Signal D')
xlabel('Zeit "t"')
ylabel('d(t) und d_derivative(t)')%% ÜA Karlsruhe
% 2
% 2.1
clc, clear, clf
set(0,'DefaultFigureWindowStyle','docked')
% Signal A
figure('Name', 'RT_Karlsruhe 2.1', 'NumberTitle', 'off')
subplot(4, 1, 1)
hold on, grid on, axis equal
start = -2;
ende = 10;
t = linspace(start,ende,5000);
a = @(t) (t-2).*(heaviside(t-2) - heaviside(t-4)) + (t-6).*(heaviside(t-4) - heaviside(t-6));
A = a(t);
a_derivative = @(t) heaviside(t-2) - 4*dirac(t-4) - heaviside(t-6);
A_derivative = a_derivative(t);
syms x
diff_test = diff((x-2).*(heaviside(x-2) - heaviside(x-4)) + (x-6).*(heaviside(x-4) - heaviside(x-6)));
% x = t;
%
% Diff_test = diff_test(x);
plot(t,A, 'g', 'LineWidth', 2)
plot(t,A_derivative, 'r--', 'LineWidth', 2)
fplot(diff_test, [-2 10], 'bo', 'LineWidth', 2)
title('Signal A')
xlabel('Zeit "t"')
ylabel('a(t) und a_derivative(t)')
%% Signal B
subplot(4,1,2)
hold on, grid on, axis equal
% Beide Funktionen für Signal B funktionieren
b = @(t) heaviside(t-2) - heaviside(t-8) + heaviside(t-4) - heaviside(t-6);
% b = @(t) heaviside(t-2) - heaviside(t-4) + 2*(heaviside(t-4) - heaviside(t-6)) ...
% + heaviside(t-6) - heaviside(t-8);
B = b(t);
b_derivative = @(t) dirac(t-2) - dirac(t-8) + dirac(t-4) - dirac(t-6);
B_derivative = b_derivative(t);
plot(t,B, 'g', 'LineWidth', 2)
plot(t,B_derivative, 'r--', 'LineWidth', 2)
title('Signal B')
xlabel('Zeit "t"')
ylabel('b(t) und b_derivative(t)')
%% Signal C
subplot(4,1,3)
hold on, grid on, axis equal
c = @(t) -2*heaviside(t-1) + 2*heaviside(t-3) + (t-4).*heaviside(t-4) - (t-4).*heaviside(t-6);
C = c(t);
c_derivative = @(t) -2*dirac(t-1) + 2*dirac(t-3) + heaviside(t-4) - heaviside(t-6);
C_derivative = c_derivative(t);
plot(t,C, 'g', 'LineWidth', 2)
plot(t,C_derivative, 'r--', 'LineWidth', 2)
title('Signal C')
xlabel('Zeit "t"')
ylabel('b(t) und c_derivative(t)')
%% Signal D
subplot(4,1,4)
hold on, grid on, axis equal
% d = @(t) 2*t.*(heaviside(t+1) - heaviside(t-1)) + (3-t).*(heaviside(t-1) - heaviside(t-3));
d = @(t) (2*t).*heaviside(t+1) - (3*t-3).*heaviside(t-1) - (3-t).*heaviside(t-3);
D = d(t);
d_derivative = @(t) 2*heaviside(t+1) + (2*t).*dirac(t+1) - 3*heaviside(t-1) ...
+ heaviside(t-3);
D_derivative = d_derivative(t);
plot(t,D, 'g', 'LineWidth', 2)
plot(t, D_derivative, 'r--', 'LineWidth', 2)
xticks(start:1:ende)
title('Signal D')
xlabel('Zeit "t"')
ylabel('d(t) und d_derivative(t)')

Birdman on 6 Apr 2020
You may use Symbolic Toolbox and its beauties for this case :) Here is the code:
syms y1(t) y2(t) y3(t) y4(t)
y1(t)=piecewise(2<=t<=4,t-2,4<t<6,t-6,0);
Dy1(t)=diff(y1);
y2(t)=piecewise(2<=t<=4,2,4<t<6,3,6<=t<=8,1,0);
Dy2(t)=diff(y2);
y3(t)=piecewise(1<=t<=3,-2,4<=t<=6,t-4,0);
Dy3(t)=diff(y3);
y4(t)=piecewise(-1<=t<=1,2*t,1<=t<=3,-t+3,0);
Dy4(t)=diff(y4);
t=-2:0.001:9;
subplot(4,1,1);plot(t,y1(t),t,Dy1(t));
subplot(4,1,2);plot(t,y2(t),t,Dy2(t));
subplot(4,1,3);plot(t,y3(t),t,Dy3(t));
subplot(4,1,4);plot(t,y4(t),t,Dy4(t));
Observe the results and let me know if it works.
Jann B on 16 Apr 2020
Thank you for the quick response.
It's not quite perfect yet.
I think i need to figure out how to set the impulse of the dirac-funktion to 1 before plotting it.

### Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by