Drawing a turning tangent on fitted curve
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Harald
2023-10-10
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
采纳的回答
Star Strider
2023-10-10
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!