Borrar filtros
Borrar filtros

How do I put annotation text pointing towards specific coordinate?

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Matt J
Matt J el 9 de Nov. de 2021
Editada: Matt J el 10 de Nov. de 2021
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

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 9 de Nov. de 2021
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 comentarios
Matt J
Matt J el 9 de Nov. de 2021
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 el 10 de Nov. de 2021
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

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by