Finding slope of a curve at some specific points

118 views (last 30 days)
Hi.
I did the sediment settling experiment and obtained the experimental data. My task is to calculate the average particle velocity. To compute this, I must calculate the average gradient of multiple gradients at different points. Can I know how to do this?

Accepted Answer

Star Strider
Star Strider on 17 Apr 2023
Probably the easiest way to calculate the instantaneous slope is:
dydx = gradient(y) ./ gradient(x);
All that is necessary after that is to index into it by using the index of the appropriate ‘x’ values if those are known.
If the gradient is monotonic (either increasing or decreasing), it is straightforward to get the slope at any ‘x’ value using the interp1 function:
slope_at_xval = interp1(x, dydx, xval);
If the slopes are not monotonic, this is still possible, however the code becomes a bit more complicated.
.
  6 Comments
Sein Lim
Sein Lim on 17 Apr 2023
I am sorry, but can u create code base on my data?
Star Strider
Star Strider on 17 Apr 2023
Sure!
It took a bit to adapt my previous code (using row vectors) to yours (using column vectors). This illustrates the derivatives by plotting tangents to the curves at random points —
% x = linspace(0, 4500, 1E+4);
% y = 85 - [80; 70; 60].*(1-exp(-[3;2;1]*0.001*x));
T1 = readtable('Data.xlsx', 'VariableNamingRule','preserve')
T1 = 146×4 table
Var1 40g 60g 80g ____ ____ ____ ____ 0 84.8 86.4 86.7 30 78 81.5 83 60 69 76 79 90 60.5 71 76 120 52.5 66 72.5 150 44 61.5 69.5 180 36 56 66 210 27 51.7 64 240 20 44.7 61.8 270 18 43.5 59.8 300 16.6 40.5 58.1 330 15.5 38.5 56.3 360 14.8 36.5 54.7 390 14.1 34.8 53 420 13.6 33.2 51.5 450 13.1 31.6 50
x = T1.Var1;
y = T1{:,2:4};
VN = T1.Properties.VariableNames;
[~,dy] = gradient(y); % Gradient Of 'y' Values
dydx = dy ./ gradient(x); % Derivative
xval = rand(1,3)*min(max(x)) % Choose 'x' Values (Can Be Any Appropriate Value)
xval = 1×3
1.0e+03 * 0.8746 4.0382 1.4755
for k = 1:size(y,2)
dydx_at_xval(:,k) = interp1(x, dydx(:,k), xval); % Calculate Derivatives AT 'xval'
y_at_xval(:,k) = interp1(x, y(:,k), xval); % Calculate 'y' Values At 'xval'
end
% dydx_at_xval
% y_at_xval
figure
plot(x, y)
hold on
for k1 = 1:size(y,2)
for k2 = 1:numel(xval)
b = y_at_xval(k2,k1)-dydx_at_xval(k2,k1)*xval(k2); % Calculate Tangent Line Y-Intercept
xrng = [-250 250]+xval(k2); % Tangent Line 'x' Range
tangent = xrng*dydx_at_xval(k2,k1) + b; % 'y' Values For Tangent Line
plot(xval(k2), y_at_xval(k2,k1), 'ms') % Plot Specific '(x,y)' Pair For Tangent Line
plot(xrng, tangent, '-r') % Plot Tangent Line
end
end
hold off
grid
xlabel('X')
ylabel('Y')
legend(VN{2:end}, 'Location','best')
I am not certain what you want with respect to the derivatives, however the data appear smooth so there are essentially no problems with noise. This illustrates the derivates as tangents to the curves at various points on the x-axis. If noise were a problem, the data would need to be smoothed first.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!