I cannot get fplot to plot my piecewise function past an x-axis value of 5

4 vues (au cours des 30 derniers jours)
David Cole
David Cole le 31 Déc 2023
Modifié(e) : Hassaan le 31 Déc 2023
Here is my code. The axis goes to 7 but the data does not go beyond 5.
syms t %declare a symbol
voltage_values = piecewise(0<=t<=3,25,3<=t<=6,0,6<=t<=7,-75);
fplot(t,voltage_values)
xlim([0 7]) %set the x-axis to 7
title('Inductor voltage')
xlabel('time (\mus)') %\mu adds the micro symbol to the x-axis label
ylabel('voltage (V)')
grid on

Réponse acceptée

Torsten
Torsten le 31 Déc 2023
syms t %declare a symbol
voltage_values = piecewise(0<=t<=3,25,3<t<=6,0,6<t<=7,-75);
fplot(t,voltage_values,[0 7])
%xlim([0 7]) %set the x-axis to 7
title('Inductor voltage')
xlabel('time (\mus)') %\mu adds the micro symbol to the x-axis label
ylabel('voltage (V)')
grid on

Plus de réponses (2)

Star Strider
Star Strider le 31 Déc 2023
The default independent variable limits for fplot are (-5,5). If you want a larger or different independent variable value, tell it using the second argument.
Try this —
syms t %declare a symbol
voltage_values = piecewise(0<=t<=3,25,3<=t<=6,0,6<=t<=7,-75);
figure
fplot(voltage_values, [0 7])
xlim([0 7]) %set the x-axis to 7
title('Inductor voltage')
xlabel('time (\mus)') %\mu adds the micro symbol to the x-axis label
ylabel('voltage (V)')
grid on
.

Hassaan
Hassaan le 31 Déc 2023
Modifié(e) : Hassaan le 31 Déc 2023
I will assume that the piecewise function should actually have three conditions: up to 3, between 3 and 5, and then from 5 to 7. Here's how you could write it.
syms t % Declare a symbol
% Define the voltage_values as a piecewise function
voltage_values = piecewise(0<=t<3, 25, 3<=t<5, 0, 5<=t<=7, -75);
% Plot the function
fplot(t, voltage_values, [0, 7]); % Set the range of t from 0 to 7
% Set the title and labels with proper units
title('Inductor voltage');
xlabel('time (\mus)'); % \mu adds the micro symbol to the x-axis label
ylabel('Voltage (V)');
grid on
With the range [0, 7] provided to fplot, it will plot the function across the entire x-axis range you've specified. The piecewise function is corrected to reflect three ranges, which should now correctly show values up to t = 7. Ensure that the range of t you plot (using fplot) and the conditions in the piecewise function match to display the data correctly.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by