Clear Filters
Clear Filters

Drawing a turning tangent on fitted curve

3 views (last 30 days)
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
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.
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

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 10 Oct 2023
Perhaps this —
xvalue = 20:1:30
xvalue = 1×11
20 21 22 23 24 25 26 27 28 29 30
yvalue = [56.7 59.8 64.8 66.7 66.2 65.2 64.2 63.5 63.0 62.7 62.5]
yvalue = 1×11
56.7000 59.8000 64.8000 66.7000 66.2000 65.2000 64.2000 63.5000 63.0000 62.7000 62.5000
p = polyfit(xvalue, yvalue, 4);
yfit = polyval(p, xvalue)
yfit = 1×11
56.1252 61.2552 64.3359 65.8121 66.1009 65.5914 64.6445 63.5935 62.7434 62.3713 62.7266
dydx = gradient(yvalue) ./ gradient(xvalue);
[dydxmax,idx] = max(dydx)
dydxmax = 4.0500
idx = 2
xq = xvalue(idx)
xq = 21
yq = yvalue(idx)
yq = 59.8000
n = yvalue(idx) - dydxmax*xvalue(idx)
n = -25.2500
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')
.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!