From: NouveauIX on
% Function Description:
% The function takes in the coefficients vector describing a given
% polynomial curve and returns the coefficients vector of the derivative
% of the curve as well as the coefficients vector of the integral of the
% curve. For the integral curve, you may assume the constant term is 0.
%
% Test Case 1:
% [dev_coeff int_coeff] = compDiffInt([1 0 1 0]);
% dev_coeff -> [3 0 1]
% int_coeff -> [0.25 0 0.5 0 0];
%

I have written:

function [dev_coeff int_coeff]=compDiffInt(coeff)

x=0:100;
y=polyval(coeff,x);
dy=diff(y)./diff(x);
dev_coeff=polyfit(x(1:end-1),dy,length(coeff)-2);
yi=cumtrapz(y);
int_coeff=polyfit(x,yi,length(coeff));
end

But it returns:

dev_coeff =

3.0000 3.0000 2.0000


int_coeff =

0.2500 -0.0000 0.7500 -0.0000 0.0000

As you can see by comparing to the test case, only a couple of those coefficients are correct. What did I do wrong?
From: Bruno Luong on
NouveauIX <visualxd(a)gmail.com> wrote in message <920694199.8353.1258075777112.JavaMail.root(a)gallium.mathforum.org>...
> What did I do wrong?

The main cause is defining the abscissa of your numerical derivative and integration. For example who can tell the abscissa of DIFF is x(1:end-1). You might take x(2:end) as well (try it). The good abscissa in in between (see Taylor theorem, try it). This fact alone is makes the code failed.

Bruno