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:')
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
y = - x - 1 y = 3 - x
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'})
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Thanks.

 채택된 답변

Matt J
Matt J 2025년 5월 28일
편집: Matt J 2025년 5월 28일

1 개 추천

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:')
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
y = - x - 1 y = 3 - x
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

rezheen
rezheen 2025년 5월 28일
편집: rezheen 2025년 5월 28일
Can I state the tangent lines once in the legend since they are the same color?
Also, labeling the points (numerically) would be nice. Thanks for the reply.
Matt J
Matt J 2025년 5월 28일
You can make them different colors if you want. See revised version.
rezheen
rezheen 2025년 5월 28일
편집: rezheen 2025년 5월 28일
But the code would be messy say if I changed the curve to:
y=piecewise(-4*pi<=x<=4*pi,tan(x)); m=1;
Too many tangent lines could pop up. I'd run out of colors lol.
Matt J
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:')
The tangent line(s) are:
disp(char(compose('y = %s',string(line))))
y = - x - 1 y = 3 - x
scatlabel( scatter(x1,y1,'k.', 'SizeData',150) ); hold on
g=fplot(line,'k--');
h=fplot(y,'color','blue','Linewidth', 1); hold off
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(1); h],{'Tangent line', 'Curve'})
function varargout=scatlabel(H,labels)
%Add labels to the (XData,YData,ZData) coordinates in a plot pointed to by a
%graphics handle object H.
%
% Hout=scatlabel(H) %Labels will be simple enumeration
% Hout=scatlabel(H,labels) %Provided labels can be string or numeric
%
%Hout is a handle to a GraphPlot object containing the labels.
ax=H(1).Parent;
S.XData=[H.XData];
S.YData=[H.YData];
for i=numel(H):-1:1
if isprop(H(i),'ZData') && ~isempty(H(i).ZData),
ZData{i}=[H(i).ZData];
else
ZData{i}=zeros(1,numel(H(i).XData));
end
end
S.ZData=cell2mat(ZData);
N=numel(S.XData);
if nargin<2, labels=1:N; end
assert(numel(labels)==N,'Number of labels doesn''t match number of data points');
tf=ishold;
if ~tf
hold(ax,'on')
end
args={'Marker','none','NodeLabel',labels,'NodeFontSize',12,'HitTest','off',...
'XData', S.XData, 'YData', S.YData};
if isfield(S,'ZData'),
args=[args,{'ZData',S.ZData}];
end
H=plot(ax,graph( speye(N),'omit' ),args{:});
if ~tf
hold(ax,'off')
end
if nargout, varargout={H}; end
end
rezheen
rezheen 2025년 5월 28일
That's great! But instead of labeling the points '1', '2', could it label what they actually are?
In this case (0, -1), (2, 1)
Yes, you can provide whatever labels you want. From the help doc,
% Hout=scatlabel(H,labels) %Provided labels can be string or numeric

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2022a

질문:

2025년 5월 28일

댓글:

2025년 5월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by