Subject: | Issue captured in Math::Derivative |
Date: | Sun, 18 Jun 2017 11:52:35 +0530 |
To: | bug-Math-Derivative [...] rt.cpan.org |
From: | Maitraya Baliyan <maitrayabaliyan [...] gmail.com> |
Dear Concern,
The first derivative subroutine is given below is from Math::Derivative
(from CPAN)
sub Derivative1
{
my ($x, $y) = @_;
my @y2;
my $n = $#{$x};
croak "X and Y array lengths don't match." unless ($n == $#{$y});
$y2[0] = ($y->[1] - $y->[0])/($x->[1] - $x->[0]);
$y2[$n] = ($y->[$n] - $y->[$n-1])/($x->[$n] - $x->[$n-1]);
for (my $i=1; $i<$n; $i++)
{
$y2[$i] = ($y->[$i+1] - $y->[$i-1])/($x->[$i+1] - $x->[$i-1]); #
<<< This statement is wrong.
}
return @y2;
}
*Issue:: Calculated derivative is incorrect.Observations:: $y2[$i] =
($y->[$i+1] - $y->[$i-1])/($x->[$i+1] - $x->[$i-1]); # <<< This
statement is wrong.As we know the derivative is calculated as dx/dy = (x2 -
x1) / (y2 - y1);*
*From next value ( $i + 1 ) we have to subtract the current value( $i ),
but in the above subroutine you have subtracted previous value( $i-1 ) from
the next value ($i + 1). *
*Solution:: $y2[$i] = ($y->[$i+1] - $y->[$i])/($x->[$i+1] - $x->[$i]);*
*Corrected Subroutine ::*
sub Derivative1
{
my ($x, $y) = @_;
my @y2 = ();
my $n = $#{$x};
print "X and Y array lengths don't match." unless ($n == $#{$y});
$y2[0] = ($y->[1] - $y->[0])/($x->[1] - $x->[0]);
$y2[$n] = ($y->[$n] - $y->[$n-1])/($x->[$n] - $x->[$n-1]);
for (my $i=1; $i<$n; $i++)
{
$y2[$i] = ($y->[$i+1] - $y->[$i])/($x->[$i+1] - $x->[$i]);
}
return @y2;
}
--
*Thanks and Regards*
* Maitraya Baliyan +91 9871019064*
* <https://www.linkedin.com/in/maitraya-baliyan-75585640>*