How to set a marker at a desired location on a plot

12 ビュー (過去 30 日間)
absomnia48
absomnia48 2016 年 12 月 25 日
編集済み: Star Strider 2016 年 12 月 26 日
Hi,I'm trying to set a marker at integer values of x.I've used index method(x(3)) but couldn't do it.Because I used linspace function.How do I mark my graph when x is equal to an integer number?Thanks.
x=linspace(-4,4);
y=x.*exp(-x.^2);
plot(x,y)
end

採用された回答

Star Strider
Star Strider 2016 年 12 月 25 日
The easiest way is to define a second vector with integer values, since by inspection only the endpoints of ‘x’ are integers:
x = linspace(-4,4);
xi = min(x) : max(x); % Integer ‘x’ Vector
y = x.*exp(-x.^2);
yi = xi.*exp(-xi.^2);
plot(x,y)
hold on
plot(xi, yi, 'rp', 'MarkerFaceColor','g')
hold off
grid

その他の回答 (2 件)

John BG
John BG 2016 年 12 月 25 日
use a vector index, let it be k, instead of the x reference of the plot.
1.
plot and add a marker
x=linspace(-4,4);
y=x.*exp(-x.^2);
plot(x,y)
hold on
p = plot(x(1),y(1),'o','MarkerFaceColor','red');
2.
now you can simply place the marker on one of the plotted points of y by varying k between 1 and numel(y), for instance:
k=20;
p.XData = x(k);
p.YData = y(k);
or
hold on;
p = plot(x(k),y(k),'o','MarkerFaceColor','red');
hold off;
the figure window also has a marker called Data Cursor.
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help, please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

absomnia48
absomnia48 2016 年 12 月 25 日
Hey,another question here.I'm supposed to find the minimum and maximum value on the graph and draw a line until the axis.I have to do it with 'fminbnd' function.I'm writing this;
[-0.6869,-0.4285]=fminbnd(@xx.*exp(-x.^2),-0.6,0.6)
%or this one
fminbnd(x.*exp(-x.^2),-4,4)
but didn't work.Matlab tells me this 'Error: An array for multiple LHS assignment cannot contain expressions.'Thank you for the help
  2 件のコメント
Star Strider
Star Strider 2016 年 12 月 25 日
編集済み: Star Strider 2016 年 12 月 26 日
You are almost there with the ‘%or this one’ version. You only need to add a function handle, here that would be ‘@(x)’:
fx = @(x) x.*exp(-x.^2);
Ymin = fx(fminbnd(fx, -4, 4))
Ymax = fx(fminbnd(@(x) -fx(x), -4, 4))
Ymin =
-428.8819e-003
Ymax =
428.8819e-003
Note that to get the maximum, take the minimum of the negative of the funciton.
John BG
John BG 2016 年 12 月 25 日
編集済み: John BG 2016 年 12 月 25 日
Start Strider, Y maxmin are
Ymax=0.42
Ymin=-0.42
while
Xmin=-0.707
Xmax=0.707
because
x=[-4:.00001:4]; y=x.*exp(-x.^2);
min(y)
=
-0.428881942471466
max(y)
ans =
0.428881942471466
find(y==min(y))
=
329290
x(find(y==min(y)))
=
-0.707110000000000

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Exploration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by