Differentiate and store results in matrix

I have two matrices. The first matrix is Density and it 311x1. The second matrix is Temperature and it is 311x21. The column in density matrix corresponds to each distinct column in temperature matrix.
So if I want to plot the first line I could just do:
plot(Density, Temperature(:,1))
and second line would just be:
plot(Density, Temperature(:,2))
How can I create a new matrix that takes the derivative of each temperature line (isotherm) with respect to density, and put this in a new matrix, such as diffTemperature?

 Risposta accettata

Try this:
Density = (0:310)'; % Create Data
Temperature = sin((0:310)'*(0:20)*2*pi/20); % Create Data
dTdD = gradient(Temperature, mean(diff(Density))); % Equal Spacing
dTdD = gradient(Temperature) ./ gradient(Density); % Unequal Spacing
figure
plot(Density, Temperature(:,5))
hold on
plot(Density, dTdD(:,5))
hold off
grid
title('Example')
legend('Temperature', 'Derivative')
xlim([0 30])

8 Commenti

Benjamin
Benjamin il 5 Nov 2018
Modificato: Benjamin il 5 Nov 2018
What do you mean between equal vs. unequal spacing? Also, the result seems off. The derivative is much different than it should be. If I replace gradient with diff, why is the produced derivative so much different?
The gradient (link) function calculates the numerical derivative of a vector or matrix. The output is the same size as the argument vector (or matrix). For a matrix, you must request two outputs, the first is the horizontal gradient along the rows, and the second is the vertical gradient down the columns.
If you want to calculate it as:
dTdD = gradient(Temperature(:,1))
that is not an issue.
It requires that the vector elements are equally spaced with respect to time (or other independent variable). You can get around this by dividing it by the gradient of the independent variable.
The gradient function is more accurate than diff with respect to taking the derivative, because of the central difference method it uses to estimate the derivative.
Benjamin
Benjamin il 5 Nov 2018
Modificato: Benjamin il 5 Nov 2018
Thanks for your help, I just am hoping to get another follow-up here. I think I am plotting the derivatives incorrectly because gradient and diff are looking like night and day compared to each other.
If I have x data and y data in a matrix. So I have density and pressure, let' say. Density = x, pressure = y. If I want to differentiate pressure wrt to density, would I use:
dPdD = gradient(Pressure(:,1))
or would I use:
dPdD = gradient(Pressure(:,1)) / gradient(Density)
Basically, I have x data and y data and I just want to plot the first and second derivatives.
Use:
dPdD = gradient(Pressure(:,1)) / gradient(Density)
To calculate the second derivative, use the gradient function on ‘dPdD’.
Benjamin
Benjamin il 5 Nov 2018
Modificato: Benjamin il 5 Nov 2018
I want to calculate the derivative on all columns of data, not just the 1st though. Pressure is 310x21. Each column I need to take derivative of. Currently, the equation you provided creates a 310x310 matrix with all zeros except the last column
Try this:
[~,dTdD] = gradient(Temperature, mean(diff(Density))); % Equal Spacing
or:
[~,dTdD] = gradient(Temperature);
dTdD = bsxfun(@rdivide, dTdD , gradient(Density)); % Unequal Spacing
Those should work.
You can use the ‘Unequal Spacing’ version for both. It is slightly less efficient if you have equal spacing, because it requires two gradient calls, and one bsxfun call.
Benjamin
Benjamin il 5 Nov 2018
Modificato: Benjamin il 5 Nov 2018
Wow, hey that works! What would it be to take 2nd derivative?
[~,ddPddD] = gradient(dPdD, mean(diff(density)));
Star Strider
Star Strider il 5 Nov 2018
Modificato: Star Strider il 5 Nov 2018
Great!
Yes. That will work for equally-spaced data.
EDIT An alternative could also be the del2 (link) function on ‘Temperature’ (or any other dependent variable), not the derivative, (although you may need to multiply the result by 4). The syntax is essentially the same.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by