Plotting functions with removable singularities

21 views (last 30 days)
Hello, I have a rather simple question about 2D plots. Is it possible to plot functions like f(x)=x if x=/=a and f(x)=b if x=a in such a way that we can avoid a 'spike' at the point of discontinuity. Specifically I would like to give a plot of f(x) where the discontinuity is emphasized in the following way:
Is it even possible to do this in a straightforward way? I came up with a rather complicated solution (basically programming everything from scratch), but the result wasn't exactly what I was looking for and was very time-consuming. I should also mention that I'm new to Matlab so I'm not familiar with the most advanced features.

Accepted Answer

John D'Errico
John D'Errico on 21 May 2020
Edited: John D'Errico on 21 May 2020
I can't really think of anything obviously easy.
I might do it like this:
F = @(x) x + 3;
fplot(F,[-3,3],'m')
hold on
H1 = plot(1,F(1),'o');
H2 = plot(1,2,'o');
H1.Color = 'm';
H1.MarkerFaceColor = 'w';
H2.MarkerFaceColor = 'm';
H0 = gca;
H0.YAxisLocation = 'origin';
That has pretty much the look of what you indicated.
I won't even try to claim how I did it was trivially easy. It was not truly difficult either. Those words apply to the eye of the person doing the work, and easy to someone is not so to someone else. It might even be what you chose to do.
  2 Comments
Modesto Rosado
Modesto Rosado on 21 May 2020
Your solution seems easy enough. I only need to figure out the commands you used. Trust me. What I was trying to do was incredibly more complicated.
John D'Errico
John D'Errico on 23 May 2020
(Smile) Ok. I was not sure.
Much/most of what I did was the use of graphics handles. So I overlaid a white circle at the point of interest on the curve, then plotted another below it in a different color. Note the use of hold on to tell MATLAB I wanted to continue plotting in the same figure.
If you are not used to the idea of how graphics handles work, you might want to just play around with them.
For example, if you return an argument from essentially any tool that generates something in a figure window, you will get a handle.
H = plot(1:5)
H =
Line with properties:
Color: [0 0 1]
LineStyle: '-'
LineWidth: 0.5
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1 2 3 4 5]
YData: [1 2 3 4 5]
ZData: [1×0 double]
Show all properties
Those are the default properties associated with what I just plotted. Now you can modify these properties by doing things as I did, and the figure immediately updates to show the change made.
H.Color = 'b';
will make the line blue.
There are also the functions gca and gcf, which will also return handles to the set of axes (gca = get current axes) and gcf (get current figure). This allows you to control aspects of the axes in a figure and the figure itself.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Identification 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!