Skip Menu |

This queue is for tickets about the Math-Derivative CPAN distribution.

Report information
The Basics
Id: 122126
Status: resolved
Priority: 0/
Queue: Math-Derivative

People
Owner: jgamble [...] cpan.org
Requestors: maitrayabaliyan [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 1.00



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>*
Thank you. I'll look into it. Did this come up when using it with your own code? If so, can you provide me with a stripped-down example, and an expected result? As you may have seen, I acquired the module as-is, with nothing in the way of test cases. Thanks again.
Subject: Re: [rt.cpan.org #122126] Issue captured in Math::Derivative
Date: Wed, 21 Jun 2017 23:36:19 +0530
To: bug-Math-Derivative [...] rt.cpan.org
From: Maitraya Baliyan <maitrayabaliyan [...] gmail.com>
Dear John M. Gamble, Thanks for the response. *Yes, i have faced the issue while embedding it into my own code.* Kindly find below the snippet below #!/usr/bin/perl use strict; use warnings; use utf8; use Math::Derivative qw(Derivative1); my @temperature = ( 16.4, 16.46, 16.52, 16.58, 16.65, 16.71, 16.79, 16.87, 16.94 ); my @height = ( 5000.4, 5007.2, 5014, 5021.7, 5029.8, 5037.9, 5046.2, 5053.7, 5061.7); my @first_derivative = Derivative1( \@height, \@temperature); $, = "\n"; print @first_derivative; * Output:* *first_derivative :* Output Expected 0.008823529412 0.008823529 0.008823529412 0.008823529 0.008275862069 0.007792208 0.008227848101 0.008641975 0.008024691358 0.007407407 0.008536585366 0.009638554 0.01012658228 0.010666667 0.009677419355 0.00875 0.00875 0.009345794 *The above two rows under the column "Expected" are giving the correct result where as following from third row, the result is incorrect.* On Tue, Jun 20, 2017 at 2:10 AM, John M. Gamble via RT < bug-Math-Derivative@rt.cpan.org> wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=122126 > > > Thank you. I'll look into it. > > Did this come up when using it with your own code? If so, can you provide > me with a stripped-down example, and an expected result? As you may have > seen, I acquired the module as-is, with nothing in the way of test cases. > > Thanks again. >
-- *Thanks and Regards* * Maitraya Baliyan +91 9871019064* * <https://www.linkedin.com/in/maitraya-baliyan-75585640>*
Okay, I've checked into this a little and it turns out 1) the function isn't wrong, but 2) it may not be what you want, so I should add that. There's more than one way to calculate the derivative from data, and since the documentation of Math::Derivative is sparse (to say the least), it didn't tell you which method it was using. Derivative1() is using the central difference formula, while you (and probably other people as well) were expecting a forward difference formula. There's a good description at <http://www.robots.ox.ac.uk/~sjrob/Teaching/EngComp/ecl6.pdf>. So, I plan to add a function that performs as you expect, with documentation to explain which is which and what to expect. Sorry for the problems it caused. -john
Sorry for the delay. I have no real excuse except for other demands on my time. In any event, a new version of Math::Derivative (version 1.00) is going up on CPAN as I type. Thank you for the report and the information.
Added new forward difference function and documented the difference.