フィルターのクリア

How do I put annotation text pointing towards specific coordinate?

3 ビュー (過去 30 日間)
Samson David Puthenpeedika
Samson David Puthenpeedika 2021 年 11 月 9 日
編集済み: Matt J 2021 年 11 月 10 日
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
  2 件のコメント
Samson David Puthenpeedika
Samson David Puthenpeedika 2021 年 11 月 10 日
Oh thankyou ..understood now
Star Strider
Star Strider 2021 年 11 月 10 日
I like ‘fn’ and the supporting code!
+1
.

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

その他の回答 (1 件)

Sulaymon Eshkabilov
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
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
Samson David Puthenpeedika 2021 年 11 月 10 日
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 Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by