Plot the periodic function.
44 views (last 30 days)
Show older comments
Answers (2)
recent works
on 1 Jan 2024
import matplotlib.pyplot as plt
# Define the periodic function
def f(x):
if x % 4 < 2:
return 1/2
else:
return -1/2
# Generate x-axis values
x = range(0, 16)
# Calculate y-axis values
y = [f(i) for i in x]
# Plot the function
plt.plot(x, y)
# Set plot labels and title
plt.xlabel("x")
plt.ylabel("f(x)")
plt.title("Periodic Function")
# Set axis limits
plt.xlim(0, 16)
plt.ylim(-1/2, 1/2)
# Show the plot
plt.grid(True)
plt.show()
Key points:
- Period: The function repeats every 4 units along the x-axis.
- Amplitude: The function oscillates between 1/2 and -1/2.
- Shape: The function has a rectangular shape within each period.
- Discontinuities: The function has jumps at x = 2 and x = 4, where it changes value abruptly.
1 Comment
Walter Roberson
on 1 Jan 2024
This appears to be python code; the stated requirements are that MATLAB code must be used.
Ayush
on 1 Jan 2024
Hi @NIBISHA
I understand that you want to get the Laplace Transform of your equation and verify the results through MATLAB code. Here is the MATLAB code for that:
syms t s
% Define one period of the piecewise function using heaviside functions
f_t_period = 0.5 * (heaviside(t) - heaviside(t - 2)) - 0.5 * (heaviside(t - 2) - heaviside(t - 4));
% Define the periodic extension of the function
f_t = f_t_period - subs(f_t_period, t, t - 4);
% Define the period
T = 4;
% Compute the Laplace transform of one period of the function
F_s = (1 - exp(-s*T))^(-1) * int(f_t_period * exp(-s*t), t, 0, T);
% Plot the original piecewise function over multiple periods
num_periods = 3; % Number of periods to plot
t_vals = linspace(0, num_periods*T, 1000); % Define time values for plotting
f_vals = double(subs(f_t, t, mod(t_vals, T))); % Evaluate f(t) at the time values with modulo for periodicity
figure;
subplot(2, 1, 1);
plot(t_vals, f_vals, 'LineWidth', 2);
title('Original Periodic Function f(t)');
xlabel('Time t');
ylabel('f(t)');
axis([0 num_periods*T -1 1]);
grid on;
% Plot the real part of the Laplace transform over a range of s values
% We avoid s = 0 to prevent division by zero
s_vals = linspace(0.01, 10, 200); % Define s values for plotting, starting just above zero
F_vals = double(subs(F_s, s, s_vals)); % Evaluate F(s) at the s values
subplot(2, 1, 2);
plot(s_vals, real(F_vals), 'LineWidth', 2); % Plot only the real part
title('Laplace Transform F(s)');
xlabel('s');
ylabel('Real part of F(s)');
grid on;
Thanks,
Ayush
0 Comments
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!