How to differentiate elements in a matrix by each other

12 views (last 30 days)
Hi,
Suppose I have a matrix with 2 columns and an abritrary number of rows, and that the columns represent two variables (magnetic field strength B and magnetoresistance M_r, respectively). How do I go about differentiating this matrix so that I can plot d(M_r)/dB against B?
I am pretty new to MATLAB so I have no idea how to approach this myself.
Thanks

Accepted Answer

Jon
Jon on 19 Aug 2019
Edited: Jon on 19 Aug 2019
Say your matrix is called M, you could do something like
% find incremental change from one row to the next
delta = diff(M);
% compute approximate derivatives
dM_rdB = delta(:,2)./delta(:,1) % note ./ does division element by element
% plot results
plot(M(2:end),dM_rdB) % M has one less row than the derivative, start at second element
  3 Comments
Jon
Jon on 19 Aug 2019
Glad to hear that helped. To obtain, perhaps better accuracy, you might want to estimate the derivatives using central difference approximations for the interior (ones other than the first and last) points. You could then use forward differences and backward differences for the endpoints. If your magnetic field strength (first column in your matrix) values are equally spaced, you can do this using the gradient function. Enter the following on the command line and you will get the documentation for that.
doc gradient
One nice thing about the gradient function is that it returns a derivative for every point, rather than just for 2:end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!