Plotting the heaviside discontinuity function

12 views (last 30 days)
The plot generated by the following code is not always the same and sometimes somewhat inaccurate. For example, there should be a vertical line at x = 0 from y = 0 to y = 8 but the line sometimes has a slight slope and the y values don't always exactly match the correct values.
syms x
f = 8*heaviside(x) - 8*(heaviside(x).*x-heaviside(x-4).*(x-4).^1)+40*(heaviside(x-4).*(x-4).^0)-16*heaviside(x-6).*(x-6).^0;
fplot(f,[0 6])
How can one generate a precise plot rather than one that is based on approximations?

Accepted Answer

Walter Roberson
Walter Roberson on 13 Oct 2020
Start the plot slightly below 0 so as to give it a chance to see the full discontinuity at 0.
syms x
f = 8*heaviside(x) - 8*(heaviside(x).*x-heaviside(x-4).*(x-4).^1)+40*(heaviside(x-4).*(x-4).^0)-16*heaviside(x-6).*(x-6).^0;
xlim([-1, 7]); ylim([-5 20]); hold on
fplot(f,[-1 6])

More Answers (0)

Community Treasure Hunt

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

Start Hunting!