How do I put annotation text pointing towards specific coordinate?

3 次查看(过去 30 天)
Below is my code , I have tried to put annotation text pointing towards a coordinate(3.4, 4.8248) using trial and error method . What is the proper way ?
x=[1 2 2.5 3 4 5]
x = 1×6
1.0000 2.0000 2.5000 3.0000 4.0000 5.0000
y=[0 5 7 6.5 2 0]
y = 1×6
0 5.0000 7.0000 6.5000 2.0000 0
A=3.4;Lagrange(x,y,A)
A = 3.4000
s = 0
c = 1×2 cell array
{[3.4000]} {[4.8248]}
ans = 4.8248
function yint=Lagrange(x,y,xx)
A=3.4
n=length(x);
s=0
for i=1:n
product=y(i);
for j=1:n
if i~=j
product=product*(xx-x(j))/(x(i)-x(j));
end
end
s=s+product;
end
yint=s;
figure()
plot(x,y)
hold on
scatter(A,yint,"x","b")
scatter(x,y,"o")
title('Lagrange Interpolation'); xlabel('x'); ylabel('f(x)');
c={A yint}
annotation('textarrow',[0.7 0.6],[0.7 0.64],'String',c)
hold off
legend('Interpolated Function','Result Point f(3.4)','Given Data');
hold off
end

采纳的回答

Matt J
Matt J 2021-11-9
编辑:Matt J 2021-11-10
x=[1 2 2.5 3 4 5];
y=[0 5 7 6.5 2 0];
A=3.4;Lagrange(x,y,A);
function yint=Lagrange(x,y,xx)
A=3.4;
n=length(x);
s=0;
for i=1:n
product=y(i);
for j=1:n
if i~=j
product=product*(xx-x(j))/(x(i)-x(j));
end
end
s=s+product;
end
yint=s;
figure()
plot(x,y)
hold on
scatter(A,yint,"x","b")
scatter(x,y,"o")
title('Lagrange Interpolation'); xlabel('x'); ylabel('f(x)');
c={A yint};
%%%%%Matt J added
ax=gca;
xl=xlim;
yl=ylim;
xy0=[xl(1),yl(1)];
Dxy=[diff(xl),diff(yl)];
fn=@(xy) (xy(:)'-xy0)./Dxy.*ax.InnerPosition([3,4])+ax.InnerPosition([1,2]);
p=fn([3.4, 4.8248]);
%%%%%%
annotation('textarrow',[0.7 p(1)],[0.7 p(2)],'String',c)
hold off
legend('Interpolated Function','Result Point f(3.4)','Given Data');
hold off
end

更多回答(1 个)

Sulaymon Eshkabilov
Here you can use the values of c to place where you want your annotation to be displayed along with the arrow. E.g.:
...
c={A yint}
annotation('textarrow',[c{1,1}, c{1,1}-0.07*max(x)]/max(x),[c{1,2},c{1,2}-0.07*max(y)]/max(y),'String',c)
...
  3 个评论
Matt J
Matt J 2021-11-9
Sulaymon Eshkabilov's comment moved here
WIth the proposed solution, you don't need to find the coordinates by a trial and error. It takes up the found values of c. In other words, the annotation placement changes with respect to the calculated values of c.
Samson David Puthenpeedika
How to use values of c in the above code? When i tried values of 'c' it was giving error that values should be between 0-1

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by