Clear Filters
Clear Filters

Grabbing number from array to calculate the numerical derivative

1 view (last 30 days)
Hello,
I ma looking for solution that will allow me to calculate a derative (in numerical way) from data from array.
I have an array caled DataOutput it's defined as:
DataOutput = zeros(length(phiAStart:phiAStep:phiAEnd),30);
where :
phiAStart:phiAStep:phiAEnd
are defined by a user (as input)
In this array, I have two columns that I would like to further work with. In column 1 I have "x" and in column 26 I have a "y"... where y=f(x) and x value is incremented by loop function:
for x=phiAStart:phiAStep:phiAEnd
...
end
Now what I would like to do is calculate a derivative and store it in another array (or even in the same if its possible). For calculation I would like to use simple formula:
f'(n)=((y(n+1))-(y(n-1)))/((x(n+1))-(x(n-1)))
For clarification this is how it's done in excel sheet:
I am assuming that I will need another for loop outside of the previous one but I am not sure how I should define a formula for this sort of equation (especially how to define new vectors).
I would be thankful for any idea.

Accepted Answer

James Tursa
James Tursa on 17 Feb 2020
Edited: James Tursa on 17 Feb 2020
For the version you post, see the diff( ) function along with element-wise divide. E.g.,
diff(y) ./ diff(x)
If that doesn't work well enough for you, you could explore central difference formula versions, or perhaps some type of curve fit.
  2 Comments
Mateusz Brzezinski
Mateusz Brzezinski on 18 Feb 2020
Thanks for the answer but It does not work as it should.
is than any option to do it "manually" as second order approximation?
Mateusz Brzezinski
Mateusz Brzezinski on 18 Feb 2020
Forget what I have said.... I had diffrent inputs now they match... Sorry and thanks a lot.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!