How to find tangent line of curve
30 views (last 30 days)
Hi guys, I'm new to matlab and I was trying to create a code that generates a graph with the tangent line of temperature by time. The data is a file with the temperature values and the transfer function. If anyone can help me that would be great.
I would have to generate a graph similar to this one:
% FOPDT (First order lag plus time delay)
y=yy; % temperature values y(t)
Image Analyst on 18 Nov 2022
Edited: Image Analyst on 18 Nov 2022
Do you have any data? What I'd do is to scan your data vector fitting the values within a certain window width to a quadratic. From the coefficients of the quadratic you can get the slope at the point at the center of the sliding window. Then knowing the slope you can use the point-slope formula for a line, and the desired length of the line to get the endpoints of a line segment tangent to your curve at that point. Upload a vector of your data values in a .mat or text file if you need more help.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
[EDIT] Seems like you're having trouble, so here is a demo:
fontSize = 12
x = linspace(0, 12, 1000);
deltax = x(2) - x(1)
period = 3;
y = sin(2 * pi * x / period);
plot(x, y, 'b-', 'LineWidth', 4);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
windowWidth = 51; % An odd number
halfWindowWidth = floor(windowWidth/2)
for k = halfWindowWidth + 1 : 66 : numel(x) - halfWindowWidth
thisSectionX = x(k-halfWindowWidth : k + halfWindowWidth);
thisSectionY = y(k-halfWindowWidth : k + halfWindowWidth);
% Fit to a quadratic or cubic. y = c(1) * x^2 + c(2) * x + c(3)
coefficients = polyfit(thisSectionX, thisSectionY, 2);
xCenter = thisSectionX(halfWindowWidth + 1);
yCenter = thisSectionY(halfWindowWidth + 1);
slope = 2 * coefficients(1) * xCenter + coefficients(2);
% (y - yCenter) = (x - xCenter) * slope.
% y = (x - xCenter) * slope + yCenter.
segmentLength = 2;
% Get the endpoints of the tangent line segment.
x1 = xCenter - halfWindowWidth * deltax;
x2 = xCenter + halfWindowWidth * deltax;
y1 = (x1 - xCenter) * slope + yCenter;
y2 = (x2 - xCenter) * slope + yCenter;
% Plot line segment in red.
plot([x1, x2], [y1, y2], 'r.-', 'LineWidth', 2, 'MarkerSize', 20);
% Plot center point of line segment at a cyan dot.
plot(xCenter, yCenter, 'c.', 'MarkerSize', 20);
Star Strider on 18 Nov 2022
That plot actually looks familiar!
See: How to draw a tangent line on a curve for one approach. (That result used the inflection point of the curve to draw the tangent, so the code is slightly diffenent than the code here.)
I chose a ‘yy’ value of 50 here simply to demonstrate the code. Change the value of ‘select_yy’ to choose a different point. (There are no inflection points here that I can see, although I did not analyse the data carefully to see if one exists.) There is also no associated time vector, so I created one.
Try this —
LD = load(websave('Equipe2','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1197593/Equipe2.mat'));
yy = LD.yy;
Fs = 1; % Sampling Frequency
L = numel(yy);
t = linspace(0, L-1, L).'/Fs; % Time Vector
select_yy = 50; % Delect 'yy' Value At Which To Draw Tangent
idxr = find(diff(sign(yy-select_yy)))+(-1:1); % Index Range
t_yy50 = interp1(yy(idxr), t(idxr), 50) % Associated Time Value
dyydt = gradient(yy) ./ gradient(t); % Numerical Derivative
dyydt50 = interp1(t, dyydt, t_yy50) % Numerical Derivative At Associated Time
y_intcpt = select_yy - dyydt50*t_yy50 % Line Y-Intercept
plot(t, yy, 'DisplayName','Data')
yl = ylim;
plot(t_yy50, select_yy, '.r', 'DisplayName','Tangent Point')
plot(t, dyydt50*t+y_intcpt, '-r', 'DisplayName','Tangent Line')
Make appropriate changes to get the desired result.