I'm am having trouble writing a derivative function.

6 views (last 30 days)
I have to assume that a vector vect contains nsamp samples of a function taken at a spacing of dx per sample. Write a function that will calculate the derivative of this vector from the equation f'(x_i)= (f(x_i+1) - f(x_i)) / (x_i+1 - x_i). Also the function should check to make sure that dx is greater than zero to prevent divide-by-zero errors in the function. The function must also be able to have a starting at x = 0.
Here is what I have so far:
function [ der ] = derivative( f, nsamp,x, dx )
%derivative This function solves problem 5.19 in Chapman (2008).
% x - starting value
% dx - step size
% nsamp - number of samples of the function
% f - function
n = nsamp*dx;
vect = x:dx:n-dx;
L = length(vect);
while dx > 0
for i = 0:L
der(i) = (f(i+dx)-f(i))/((i+dx)-(i));
end
end
end
And when I go to the command window I get:
>> f=@(x)sin(x);
>> derivative(f,100,0,0.05)
Attempted to access der(0); index must be a positive integer or logical.
Error in derivative (line 21)
der(i) = (f(i+dx)-f(i))/((i+dx)-(i));
I'm having trouble fixing the errors and getting the program to run. Any help will be appreciated.
  1 Comment
Jan
Jan on 13 Feb 2013
I've formatted your code as explained here. Please use meaningful tags, because they are used to classify the questions. Then "error" is not useful. Thanks.

Sign in to comment.

Accepted Answer

Jan
Jan on 13 Feb 2013
Edited: Jan on 13 Feb 2013
At first you have to consider that Matlab's indices are 1-based. So 1 is the minimal index and you'd need: for i = 0:L.
But your function contains much more problems:
  • while dx > 0 will produce an infinite loop, because dx does not change its value inside the loop.
  • ((i+dx)-(i) is exactly dx.
  • vect is not used for the calculations.
I'd restart from scratch:
  1. Redefine vect: It should start at x, have nsamp elements, and stop at x + nsamp * dx, correct?
  2. Evaluate the function in one step: y = f(vect);
  3. Use diff to calculate the difference between the elements of y with a loop. Dividing the result by dx produces the derivative already.
  4. The replied NaNs for dx==0 are ok, because this is mathematically consistent. And negative dx are ok also.
So you actually need 2 to 3 lines of code.

More Answers (2)

Youssef  Khmou
Youssef Khmou on 14 Feb 2013
Hi, try this :
function [ der ] = derivative(f,nsamp,dx)
%derivative This function solves problem 5.19 in Chapman (2008).
% x - starting value
% dx - step size
% nsamp - number of samples of the function
% f - function
n = nsamp*dx;
vect = 0:dx:n-dx;
L = length(vect);
der=zeros(1,L);
for i = 1:L-1
der(i) = (f(i+dx)-f(i))/dx;
%OR : der(i) = (f(i+dx)-f(i))/(vect(i+1)-vect(i));
end
der(end)=der(end-1);
  3 Comments
Jan
Jan on 14 Feb 2013
No, this does not work also: You do not want "f(i+dx)", where "i" is the run index of the FOR loop. The derivative should be evaluated at the points x:dx:(x+nsamp*dx).

Sign in to comment.


Youssef  Khmou
Youssef Khmou on 14 Feb 2013
Hi, Becca
The first answer has prob, here is a fast way :
function [ der ] = derivative(f,x,dx)
%derivative This function solves problem 5.19 in Chapman (2008).
% x - starting value, or vector
% dx - step size
% nsamp - number of samples of the function
% f - function
% Original function
Y=feval(f,x);
xp=x+dx;
YP=feval(f,xp);
der=(YP-Y)/dx;
if length(x)>2
figure, plot(x,Y,x,der,'r')
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!