Legend & Labeling points in a plot
7 次查看(过去 30 天)
显示 更早的评论
I'd like to have a legend that shows all tangent lines and curves of my graph, also label points if possible either in the legend or right next to the points themselves. My code doesn't do either currently:
syms a x
y=1/(x-1); m=-1; dy=diff(y);
x1_=subs(dy,a); eq=x1_==m; x1=solve(eq); y1=subs(y,x1);
line=m*(x-x1)+y1;
disp('The tangent line(s) are:')
for k=1:length(x1)
fprintf('y = %s\n',char(line(k)))
f=plot(x1,y1,'.', 'color', 'black','markersize',15); hold on
g=fplot(line); set(g,'color','black','LineStyle', '--'); hold on;
end
h=fplot(y); set(h,'color','blue','Linewidth', 1);
j=xline(0); set(j,'color','black','Linewidth', 1.5)
k=yline(0); set(k,'color','black','Linewidth', 1.5)
xlabel('x'); ylabel('y'); title('Curve & tangent line & points')
legend([g h],{'Tangent line', 'Curve'})
Thanks.
0 个评论
采纳的回答
Matt J
2025-5-28
编辑:Matt J
2025-5-28
syms a x
y=1/(x-1); m=-1; dy=diff(y);
x1_=subs(dy,a); eq=x1_==m; x1=solve(eq); y1=subs(y,x1);
line=m*(x-x1)+y1;
disp('The tangent line(s) are:')
for k=1:length(x1)
fprintf('y = %s\n',char(line(k)))
f(k)=plot(x1(k),y1(k),'.', 'MarkerSize',15); hold on
g(k)=fplot(line(k),'--');
end
h=fplot(y,'color','blue','Linewidth', 1);
xline(0,'color','black','Linewidth', 1.5);
yline(0,'color','black','Linewidth', 1.5);
xlabel('x'); ylabel('y'); title('Curve & tangent line & points')
legend([g(:); f(:); h],{'Tangent line 1','Tangent line 2' ,'Point 1','Point 2', 'Curve'})
col={'red','green'};
[f.Color]=deal(col{:});
[g.Color]=deal(col{:});
hold off
6 个评论
Matt J
2025-5-28
Yes, you can provide whatever labels you want. From the help doc,
% Hout=scatlabel(H,labels) %Provided labels can be string or numeric
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!