How to draw tangent line at infleciton point?

Hy
I would like to draw line to curve at inflection point... the code what I use to find a the point is:
figure(1)
load('data.mat')
plot(t,y);
hold on;
ypp = diff(y,2);
% Find the root using FZERO
t_infl = fzero(@(T) interp1(t(2:end-1),ypp,T,'linear','extrap'),0)
y_infl = interp1(t,y,t_infl,'linear')
plot(t_infl,y_infl,'ro');
hold off;

2 Comments

can you share your matlab code for draw tangent at inflection point ?
Jayraj Rajput —
See my Comment.
(A Vote for my Answer would be appreciated!)

Sign in to comment.

 Accepted Answer

You have the slope at the inflection point as well as the ‘t’ and ‘y’ values at the inflection point, so all you need to do to draw the tangent line at the inflection point is to calculate the slope ‘b’, and that is straightforward.
I would use the gradient function to calculate the derivative, not diff, and the del2 function to calculate the second derivative, both because they are more accurate and because the result of each vector is the same length as the input vector.

5 Comments

I tried to do this:
n = length(t)-1
slope = (y(n+1)-y(n-1)) / (t(n+1) - t(n-1));
y1 = slope * (t - t(n)) + t(n);
plot(y1)
but I dont know this plot(y1) is good way to draw the line,and where should be put in code.
when I tried this code:
figure(1)
load('data31.mat')
hold on;
n = length(t)-1
slope = (y(n+1)-y(n-1)) / (t(n+1) - t(n-1));
y1 = slope * (t - t(n)) + t(n);
plot(y1)
plot(t,y);
ypp = diff(y,2);
% Find the root using FZERO
t_infl = fzero(@(T) interp1(t(2:end-1),ypp,T,'linear','extrap'),0)
y_infl = interp1(t,y,t_infl,'linear')
plot(t_infl,y_infl,'ro');
hold off;
get this plot
Having the data helps much!
The slope is +0.46 and the intercept is -0.10.
This is my approach:
D = load('xy data31.mat');
t = D.t;
y = D.y;
d1y = gradient(y,t); % Numerical Derivative
d2y = del2(y,t); % Numerical Second Derivative
t_infl = interp1(d1y, t, max(d1y)); % Find ‘t’ At Maximum Of First Derivative
y_infl = interp1(t, y, t_infl); % Find ‘y’ At Maximum Of first Derivative
slope = interp1(t, d1y, t_infl); % Slope Defined Here As Maximum Of First Derivative
intcpt = y_infl - slope*t_infl; % Calculate Intercept
tngt = slope*t + intcpt; % Calculate Tangent Line
figure(1)
plot(t, y)
hold on
plot(t, d1y, '-.m', t, d2y, '--c') % Plot Derivatives (Optional)
plot(t, tngt, '-r', 'LineWidth',1) % Plot Tangent Line
plot(t_infl, y_infl, 'bp') % Plot Maximum Slope
hold off
grid
legend('y(t)', 'dy/dt', 'd^2y/dt^2', 'Tangent')
axis([xlim -0.2 +0.6])
Producing this plot:
I plotted the numeric derivatives as well for clarity. You can comment that plot call out if you want, but be sure to change the legend call to exclude those entries.
I have a question on this script.. I ran it on a beach profile that I have, and an it returned an error that it was not a monotonic function. The sample curve from above is not so either, so how may I overcome this?
Thank you.
Hi, I try to run this script - I have just two columns of excel data and I get the following errors when trying to run: Error using griddedInterpolant The grid vectors must contain unique points.
Error in interp1 (line 161) F = griddedInterpolant(X,V,method);
Error in test1 (line 6) t_infl = interp1(d1y, t, max(d1y)); % Find ‘t’ At Maximum Of First Derivative
what can be causing this problem? Many thanks
Hi, I think you just need to change the line:
t_infl = interp1(d1y, t, max(d1y));
By,
t_infl = interp1(t, d1y, max(d1y));
I hope it helps.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

x y
on 14 Nov 2015

Commented:

on 12 Dec 2020

Community Treasure Hunt

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

Start Hunting!