![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1507319/image.png)
Drawing a turning tangent on fitted curve
2 views (last 30 days)
Show older comments
Hello Community,
i have following data
xvalue = 20:1:30
yvalue = [56.7 59.8 64.8 66.7 66.2 65.2 64.2 63.5 63.0 62.7 62.5]
This is the step response of a system I am trying to implement a PID-Controller to.
My plan was as following:
1.fit a function to the data
2. get the first derivative
3. find for the maximum of the darivative and get the index of it
4. get the x and y value with the index of the derivative
5. put that in y = m*x+n to calculate n
6. somehow draw that to the plot of the fitted data
7. draw ylines at the to resting values of the step response
8. get the time constants from the intersection of the tangent and the ylines
unfortunally I am to stupid to get it to work. maybe one of you has a idea on how to solve that problem.
Kind regards
1 Comment
Harald
on 10 Oct 2023
Hi,
I'd suggest to tackle this step by step. If we can help you with the first one or two steps, you can perhaps go from there.
First, you need to come up with a model function, ideally from the theory behind your application. For example from x = 24 onwards, this looks like
, but you will of course need a function that accounts for the first part as well.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1507319/image.png)
Functions like lsqcurvefit or lsqnonlin can then be used to fit that model to your data.
To get the derivatives, you can use diff, either symbolically or on points sampled on the curve.
Best wishes,
Harald
Accepted Answer
Star Strider
on 10 Oct 2023
Perhaps this —
xvalue = 20:1:30
yvalue = [56.7 59.8 64.8 66.7 66.2 65.2 64.2 63.5 63.0 62.7 62.5]
p = polyfit(xvalue, yvalue, 4);
yfit = polyval(p, xvalue)
dydx = gradient(yvalue) ./ gradient(xvalue);
[dydxmax,idx] = max(dydx)
xq = xvalue(idx)
yq = yvalue(idx)
n = yvalue(idx) - dydxmax*xvalue(idx)
figure
yyaxis left
plot(xvalue, yvalue, 'DisplayName','Data')
hold on
plot(xvalue, yfit, 'DisplayName','Polynomial Fit')
hold off
yyaxis right
plot(xvalue, dydx, 'DisplayName','First Derivative')
legend('Location','best')
grid
xlabel('xvalue')
.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!