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]
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}
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
0 个评论
采纳的回答
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
2 个评论
更多回答(1 个)
Sulaymon Eshkabilov
2021-11-9
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!