I cannot get fplot to plot my piecewise function past an x-axis value of 5
    4 views (last 30 days)
  
       Show older comments
    
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

0 Comments
Accepted Answer
  Torsten
      
      
 on 31 Dec 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
More Answers (2)
  Star Strider
      
      
 on 31 Dec 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
.
0 Comments
  Hassaan
      
 on 31 Dec 2023
        
      Edited: Hassaan
      
 on 31 Dec 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
0 Comments
See Also
Categories
				Find more on Calculus 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!





