How to plot tangent segment?

I've plotted a simple function using a vector of nodes
I managed to compute for every nodes the tangent lines
I don't know how to plot only a tiny segment for every tangent, something like this possibly without the arrow
function[y]=tangent
nodes=[-5:5];
f=quadratic_function([-5:5]); %nodes^2
g=derivate_quadratic_function([-5:5]); %2*nodes
for k=1:length(nodes)
tang=(nodes-nodes(k))*g(k)+f(k);
plot(nodes,tang);
hold on;
end
plot(nodes,f);
hold on;
end

3 Comments

John D'Errico
John D'Errico on 3 Jan 2016
Edited: John D'Errico on 3 Jan 2016
I suppose I should post a solution on the FEX for this, since this question gets asked a lot. But there are a lot of options and possibilities that I can see. For example, just thinking out loud here...
1. How is the curve posed? As a set of points? As a function of the form y(x)? As an implicit function? (A general implicit function can be a pain in the neck to work with, so I might be reluctant to solve that one.) As a function handle, or a set of function handles, so as a parametric function? Should it allow 3-d curves, thus P(x,y,z) in some form?
2. What would be returned? Would I generate a plot? (Always?) Some users would probably want the actual slope of the tangent line, plus the point on the line. Or they might want to get a pair of points at each end of the line segment for the tangent line.
3. If a plot is generated, how long are the tangent segments? Is an arrowhead generated? Line style and color? Is the curve itself plotted?
4. Care must be taken in the numerics, to deal with infinite slopes. E.g., a curve of the form (cos(t),sin(t)), where t varies from 0 to 2*pi.
5. If the curve is posed as a list of points, should it be presumed to be a closed curve? Not hard to fix if the start and end point are not the same, but that periodic connection point will have a slope discontinuity unless care is taken with the interpolant.
6. Where should the tangent points be generated?
7. The most difficult question: what should it be named?
Lots of other ideas I am sure too. In the end, if done well, it could be an interesting and useful complement to tools like ezplot, or my own tools like interparc.
Hi Alessandro,
Below are some threads that discuss this topic and can be used as a starting point:
Hope this helps.
Thanks
Sudhanshu Bhatt
Hello,
Were you able to solve this issue?
Thanks,

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 3 Jan 2016

Commented:

on 9 Apr 2020

Community Treasure Hunt

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

Start Hunting!