0

I want to add markers at the limits of a function in matlab. I am plotting the function using fplot this is what I tried:

user_func     = '2*x-3';
user_limits   = '-2,2';
user_limits   = regexp(user_limits, '\,', 'split');
user_limit(1) = str2num(user_limits{1});
user_limit(2) = str2num(user_limits{2});
h = fplot(func,limits);

I am trying to add markers at the limits only (size 10 color 'r'). any idea on how to do that? thank you

1 Answers1

1

Not sure if this is exactly what you are trying to accomplish but I modified your code slightly so I could plot the function (using an anonymous function):

user_func     = @(x) 2*x-3;
user_limits   = '-2,2';
user_limits   = regexp(user_limits, '\,', 'split')
user_limit(1) = str2num(user_limits{1})
user_limit(2) = str2num(user_limits{2})
figure;fplot(user_func,[user_limit(1) user_limit(2)]);

Next, set the ticks at your locations and change the font size to 10 pt:

set(gca,'XTick',[user_limit(1) user_limit(2)],'FontSize',10);

Change the color of your labels to red:

set(gca, 'XColor', [1 0 0]);
set(gca, 'YColor', [1 0 0]);

Just so you can see the ticks, stretch the x-range a bit:

axis([-2.1 2.1 0 1]); axis 'auto y'


EDIT: After some additional input from the OP, the red tick markers can be plotted as shown below.

First let the x-position at the first limit be given by:

x1 = user_limit(1);

The y-value for first marker is then obtained from the anonymous function like this:

y1 = user_func(x1);
y2 = y1;

We have, y2 = y1, since you want the y-value where where your function first crosses the x-axis to be the same. Now make your plot like this (with x2 = user_limit(2)):

hold on;
plot(x1, y1, 'ro', x2, y2,'ro');
hold off;

giving a plot like:

enter image description here

Bruce Dean
  • 2,798
  • 2
  • 18
  • 30
  • thank you very much. i meant getting red circles on -2 and 2 where now the xtick are. how do i switch the xticks with 'ro' to show the function limits. i tried plotting them with 'hold on' but i dont know how to get the y values from the function (-7). thank you again – user3185970 Jan 12 '14 at 08:22
  • @user3185970, Ok I see what you mean, see my edit above, hope this helps. – Bruce Dean Jan 12 '14 at 15:42