Develop a Matlab program to obtain firstderivative estimates

Develop a Matlab program to obtain first-derivative estimates for unequally spaced data. Test it with the following data: where f(x) = 5e^-2x x. Compare your results with the true derivatives. First Derivative f\'(x_i) = f(x_i + 1) - f(x_i)/h f\'(x_i) = -f(x_i + 2)+4f(x_i +1) - 3f(x_i)/2h Backward difference First Derivative f\'(x_i) = f(x_i) - f(x_i-1)/h f\'(x_i) = 3f(x_i) - 4f(x_i -1) + f(x_i-2)/2h Centered difference First Derivative f\'(x_i) = f(x_i + 1) - f(x_i-1)/2h f\'(x_i) = -f(x_i + 2) + 8f(x_i + 1) - 8f(x_i - 1) + f(x_i-2)/12h

Solution

Solution:

% function to test first derivative of unequally spaced data

function dyval = fdervunequ(xval,yval,varargin)

[hval,dyval,Nval,permval] = parse_inputs(xval,yval,varargin);

     if isempty(dyval)

          return

     end

     n1val = size(hval,1);

     i1val = 1:n1val-1;

     i2val = 2:n1val;

     for iterval = 1:Nval

          vval = diff(dyval)./hval;

          if n1val>1

              dyval(i2val,:) = (hval(i1val,:).*vval(i2val,:)

+hval(i2val,:).*vval(i1val,:))./(hval(i1val,:)+hval(i2val,:));

              dyval(1,:) = 2*vval(1,:) - dyval(2,:);

              dyval(n1val+1,:) = 2*vval(n1val,:) –

dyval(n1val,:);

          else

              dyval(1,:) = vval(1,:);

              dyval(n1val+1,:) = dyval(1,:);

          end

     end

     dyval = ipermute(dyval,permval);

% local function

function [hval,dyval,Nval,permval] = parse_inputs(xval,yval,vval)

     numvarargsval = length(vval);

     if numvarargsval > 2

          error(\'Requires at most 2 optional inputs\');

     end

     hval = [];

     Nval = [];

     permval = [];

     % derivative for first non-singleton dimension

     dimval = find(size(yval)>1);

    

     if isempty(dimval)

          dyval = [];

          return

     end

     dimval = dimval(1);

     % optional arguments

     optargsval = {dimval 1};

     newval = ~cellfun(\'isempty\', vval);

     optargsval(newval) = vval(newval);

     [dimval, Nval] = optargsval{:};

     % Error check

if dimval<1 || dimval>ndims(yval) || dimval~=fix(dimval) || ~isreal(dimval)

          error(\'InvalidOptionalArg, value must be specified as

a non-negative integer\')

     end

     if Nval~=fix(Nval) || ~isreal(Nval)

          error(\'InvalidOptionalArg, value must be an integer\')

     end

     % permutation for target dimension

     permval = 1:length(size(yval));

     permval(dimval) = [];

     permval = [dimval permval];

     dyval = permute(yval,permval);

     if length(xval)==1

          sizehval = size(dyval);

          sizehval(1) = sizehval(1) - 1;

          hval = repmat(xval,sizehval);

     elseif ndims(xval)==2 && any(size(xval)==1)

          if length(xval)~=size(dyval,1)

error(\'X and Y is missmtch, length of vector must match\')

          end

          xval = xval(:);

          sizehval = size(dyval);

          sizehval(1) = 1;

          hval = repmat(diff(xval),sizehval);

     else

          if size(yval) ~= size(xval)

              error(\'X and Y is missmtch in sizes of

arrays \');

          end

         

          hval = diff(permute(xval,permval),[],1);

     end

    

    

xval=[1 1.5 1.6 2.5 3.5]

yval=[.6767 0.3734 0.3261 0.08422 0.01596]

dy=fdervunequ(x,y,varargin)

 Develop a Matlab program to obtain first-derivative estimates for unequally spaced data. Test it with the following data: where f(x) = 5e^-2x x. Compare your r
 Develop a Matlab program to obtain first-derivative estimates for unequally spaced data. Test it with the following data: where f(x) = 5e^-2x x. Compare your r
 Develop a Matlab program to obtain first-derivative estimates for unequally spaced data. Test it with the following data: where f(x) = 5e^-2x x. Compare your r

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site