Filter löschen
Filter löschen

Piecewise function in Simulink

42 Ansichten (letzte 30 Tage)
Yamana Uno
Yamana Uno am 21 Okt. 2023
Beantwortet: Sam Chak am 25 Okt. 2023
I am trying to implement a piecewise function in Simulink over two periods.
from 0 <= t < (pi/10) should plot the 500*sin(10t)^2 and then from (pi/10) <= t < 0.8 should plot 0.
How could I do that if I am using the MATLAB function block? Below is what I have in the function block.
function y = fcn(t)
if t < (pi/10)
y = 500*((sin(10*t)).^2)
else
y = 0;
end

Antworten (2)

Ishaan
Ishaan am 25 Okt. 2023
Hi Yamana,
I understand that you want to plot a piecewise function that takes current simulation time "t" as an input, and gives the required output using the MATLAB function block.
The "Ramp" block can be used with the default block settings in order to feed time "t" as an input to the MATLAB function block. Since the default slope for the output of "Ramp" block is 1, it essentially emulates the function "y = t", so it can act as a source of the current time "t".
Creating a simulink model as shown in the below image will help you model the desired function:
Within the MATLAB function block, make sure the function definition contains the following code:
function y = fcn(t)
if t < (pi/10)
y = 500*(sin(10*t).^2);
else
y = 0;
end
end
Below is the output from the scope block when the simulation stop sime is set to 1 second.
The following MathWorks documentation page provides more details about the usage of "Ramp" block in Simulink:
Hope this helps,
Ishaan Mehta

Sam Chak
Sam Chak am 25 Okt. 2023
Since the piecewise signal is generated through an If-Else conditional statement in the coding mode, @Ishaan's approach should suffice. Alternatively, you can create the piecewise signal from the ground up by implementing a logic switch (turning on or off). This concept draws from switching theory and Boolean logic design. I'll explain this using MATLAB code, but you can also build the signal using the basic blocks as demonstrated in the Simulink model.
t = linspace(0, 0.8, 80001);
y1 = 500*(sin(10*t).^2); % Sinusoidal signal
Ts = pi/10; % Switch trigger time (0.31416 sec)
y2 = 1 - heaviside(t - Ts); % Logic switch (use Step block)
y = y1.*y2; % Desired signal
subplot(311), plot(t, y1, 'linewidth', 2, 'Color', [102/255, 194/255, 165/255]), grid on, ylabel('Sinusoid', 'fontsize', 12)
subplot(312), plot(t, y2, 'linewidth', 2, 'Color', [252/255, 141/255, 98/255]), grid on, ylabel('Logic switch', 'fontsize', 12), ylim([-0.2 1.2])
subplot(313), plot(t, y, 'linewidth', 2, 'Color', [141/255, 160/255, 203/255]), grid on, ylabel({'$y(t)$'}, 'interpreter', 'latex', 'fontsize', 14), xlabel({'$t$'}, 'interpreter', 'latex', 'fontsize', 14)
Simulink model:
Scope:

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by