I cannot get fplot to plot my piecewise function past an x-axis value of 5
4 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1579821/image.jpeg)
0 个评论
采纳的回答
Torsten
2023-12-31
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
更多回答(2 个)
Star Strider
2023-12-31
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 个评论
Hassaan
2023-12-31
编辑:Hassaan
2023-12-31
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!