In this code, I want to find out slope at every value of x?

9 views (last 30 days)
clear all
clc
x=[10.33 10.331 10.332 10.333 10.334 10.335 10.336 10.337 10.338 10.339 10.34 10.341 10.342 10.343 10.344 10.345 10.346 10.347 10.348 10.349 10.35];
y=[78.6900000000000 78.9200000000000 79.1600000000000 79.4000000000000 79.6500000000000 79.9000000000000 80.1500000000000 80.4100000000000 80.6800000000000 80.9500000000000 81.2300000000000 81.5200000000000 81.8200000000000 82.1200000000000 82.4300000000000 82.7500000000000 83.0900000000000 83.4300000000000 83.7900000000000 84.1600000000000 84.5400000000000];
plot(x,y,'*')

Answers (4)

Adithya
Adithya on 1 Mar 2023
To find the slope at every value of x, you can use the diff() function to calculate the difference between consecutive y values, and then divide it by the difference between consecutive x values. Here's the modified code:
clear all
clc
x=
[10.33 10.331 10.332 10.333 10.334 10.335 10.336 10.337 10.338 10.339 10.34 10.341 10.342 10.343 10.344 10.345 10.346 10.347 10.348 10.349 10.35];
y=
[78.6900000000000 78.9200000000000 79.1600000000000 79.4000000000000 79.6500000000000 79.9000000000000 80.1500000000000 80.4100000000000 80.6800000000000 80.9500000000000 81.2300000000000 81.5200000000000 81.8200000000000 82.1200000000000 82.4300000000000 82.7500000000000 83.0900000000000 83.4300000000000 83.7900000000000 84.1600000000000 84.5400000000000];
dy_dx =
diff(y) ./ diff(x);
The resulting dy_dx array will contain the slope at every value of x, except for the last value in the x array, since there is no corresponding difference for it. You can plot the slopes against the x values using the plot() function:
plot(x(1:end-1), dy_dx, '*');
Note that we are using x(1:end-1) instead of x to exclude the last value in the x array from the plot.
The reason we exclude the last value of x is because the diff() function calculates the difference between consecutive elements in an array. Since there is no element after the last element in an array, the diff() function can't calculate the difference for the last element. Therefore, the resulting dy_dx array will have one less element than the x and y arrays.
If we plot dy_dx against x without excluding the last value of x, we will get a "dimension mismatch" error because the two arrays will have different lengths. To avoid this error, we exclude the last value of x to make sure that the x and dy_dx arrays have the same length.
Below is the plot:
H
Hope it helps.

Davide Masiello
Davide Masiello on 1 Mar 2023
You could use the gradient function provided by MatLab.
x=[10.33 10.331 10.332 10.333 10.334 10.335 10.336 10.337 10.338 10.339 10.34 10.341 10.342 10.343 10.344 10.345 10.346 10.347 10.348 10.349 10.35];
y=[78.6900000000000 78.9200000000000 79.1600000000000 79.4000000000000 79.6500000000000 79.9000000000000 80.1500000000000 80.4100000000000 80.6800000000000 80.9500000000000 81.2300000000000 81.5200000000000 81.8200000000000 82.1200000000000 82.4300000000000 82.7500000000000 83.0900000000000 83.4300000000000 83.7900000000000 84.1600000000000 84.5400000000000];
dydx = gradient(y,x);
plot(x,y,x,dydx)
legend('y(x)','y''(x)','Location','best')

Star Strider
Star Strider on 1 Mar 2023
Try something like this —
x=[10.33 10.331 10.332 10.333 10.334 10.335 10.336 10.337 10.338 10.339 10.34 10.341 10.342 10.343 10.344 10.345 10.346 10.347 10.348 10.349 10.35];
y=[78.6900000000000 78.9200000000000 79.1600000000000 79.4000000000000 79.6500000000000 79.9000000000000 80.1500000000000 80.4100000000000 80.6800000000000 80.9500000000000 81.2300000000000 81.5200000000000 81.8200000000000 82.1200000000000 82.4300000000000 82.7500000000000 83.0900000000000 83.4300000000000 83.7900000000000 84.1600000000000 84.5400000000000];
dydx = gradient(y) ./ gradient(x);
figure
yyaxis left
plot(x,y,'*')
ylabel('$y(x)$', 'Interpreter','latex')
yyaxis right
plot(x, dydx)
ylabel('$\frac{dy}{dx}$', 'Interpreter','latex')
xlabel('x')
grid
.

Torsten
Torsten on 1 Mar 2023
The answer doesn't change because of new x/y data:
clear all
clc
x=[10.33 10.331 10.332 10.333 10.334 10.335 10.336 10.337 10.338 10.339 10.34 10.341 10.342 10.343 10.344 10.345 10.346 10.347 10.348 10.349 10.35];
y=[78.6900000000000 78.9200000000000 79.1600000000000 79.4000000000000 79.6500000000000 79.9000000000000 80.1500000000000 80.4100000000000 80.6800000000000 80.9500000000000 81.2300000000000 81.5200000000000 81.8200000000000 82.1200000000000 82.4300000000000 82.7500000000000 83.0900000000000 83.4300000000000 83.7900000000000 84.1600000000000 84.5400000000000];
% Approximate dy/dx
dy = gradient(y,0.001);
hold on
yyaxis left
plot(x,y)
ylabel('y vs. x')
yyaxis right
plot(x,dy)
ylabel('dy/dx vs. x')
hold off
grid on
% interpolate y and dy/dx at x=x0
x0 = 10.3355;
y0 = interp1(x,y,x0)
y0 = 80.0250
dy0 = interp1(x,dy,x0)
dy0 = 252.5000

Community Treasure Hunt

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

Start Hunting!