GHMON has asked for the wisdom of the Perl Monks concerning the following question:

Hi

i want to calculate the mean and variance of a collect of numbers and take gaussian function then draw a chart but i couldn't show a chart that create by my code !!!!!

#Hi First practice for PR class use warnings ; use strict ; use GD::Graph::linespoints ; #use DBI ; #use utf8 ; #use Encode ; #my $sfile = '/root/source.txt' ; print 'First Number > ' ; chomp(my $A = <STDIN>) ; print 'Second Number > ' ; chomp(my $B = <STDIN>) ; #my @range = ($A..$B) ; #my $rndn = $range[int (rand(@range))] ; my @points = () ; my $counter = 0 ; while ($counter <= 999) { my $rndn = $A + (int rand($B - $A + 1)); push (@points,$rndn) ; $counter++ ; } #Calculate the Mean and Variance my $running_sum = 0; my $meansum ; my $vari ; my $element1 ; my $element2 ; foreach $element1 (@points) { $meansum += $element1; } my $mean = $meansum/1000 ; print "MEAN = $mean" , "\n" ; foreach $element2 (@points) { $vari += (($meansum - $element2)^2) ; } my $variance = ($vari/1000) ; print "Variance = $variance" , "\n" ; print 'Second Number > ' ; chomp(my $xi = <STDIN>) ; my $Gs1 = (1/sqrt(2*3.14*$variance)) ; my $Gs2 = 2.718^(-(($xi - $mean)^2)/(2*$variance)) ; my $Go = $Gs1 * $Gs2 ; print "$Gs1" , "\n" ; print "$Gs2" , "\n" ; print "$Go" , "\n" ; my $graph = new GD::Graph::linespoints(2000 , 2000) ; $graph->set( x_label => 'Points' , x_label_skip => 1 , y_label => 'Number' , y_label_skip => 1 , title => 'Time Vs Fee') or warn $graph->error ; $graph->plot(\@points); open OUT,'>','FirstPracticeTest.jpeg' or die "$!"; binmode OUT; print OUT $graph->gd->jpeg;

Replies are listed 'Best First'.
Re: Problem in make function and draw chart
by poj (Abbot) on Dec 10, 2018 at 10:47 UTC

    You need the x-axis values

    my @points = () ; my @axis = (); my $counter = 0 ; while ($counter <= 999) my $rndn = $A + (int rand($B - $A + 1)); push @points,$rndn ; $counter++ ; push @axis,$counter; } . . . $graph->plot([\@axis,\@points]);
    poj
Re: Problem in make function and draw chart
by kcott (Archbishop) on Dec 10, 2018 at 11:03 UTC

    G'day GHMON,

    I added this to the top of your posted code (and saved as 'pm_1227027_orig.pl'):

    #!/usr/bin/env perl

    I also needed to install GD::Graph:

    $ cpan ... cpan[1]> install GD::Graph ... RUZ/GDGraph-1.54.tar.gz /usr/bin/make install -- OK cpan[2]> q

    I checked the syntax:

    $ perl -c pm_1227027_orig.pl pm_1227027_orig.pl syntax OK

    Running the program showed that "Second Number" was requested twice: you should look into that. Here's an example run:

    $ pm_1227027_orig.pl First Number > 1 Second Number > 2 MEAN = 1.517 Variance = 1515.415 Second Number > 2 0.0102507203875771 2 0.0205014407751541

    Checking for the output:

    $ ls -al FirstPracticeTest.jpeg -rw-r--r-- 1 ken staff 63191 Dec 10 21:20 FirstPracticeTest.jpeg

    And a quick check on what that file actually is:

    $ file FirstPracticeTest.jpeg FirstPracticeTest.jpeg: JPEG image data, JFIF standard 1.01, aspect ra +tio, density 1x1, segment length 16, comment: "CREATOR: gd-jpeg v1.0 +(using IJG JPEG v80), default quality", baseline, precision 8, 2000x2 +000, frames 3
    "... but i couldn't show a chart that create by my code ..."

    I don't really know what that means. Obviously, the file is there, it contains data, it has read permissions for all, and is recognised as JPEG. If you had difficulty opening FirstPracticeTest.jpeg in some image viewer, let us know which one; also try others to see if the problem persists. If there any messages — for example, indicating corrupt data — you should post those as well.

    You could also check for the version of GD::Graph you have installed. Perhaps an older versions had bugs. Consider upgrading if you don't have the latest.

    There's a few other things that would improve your code:

    • Use lexical filehandles in the smallest scope possible. See open.
    • Indirect object syntax is, in nearly all cases, strongly discouraged. See "perlobj: Indirect Object Syntax".
    • Use meaningful names throughout. $A, $B, OUT, and so on, are all poor choices.

    I don't believe any of that will change how your short, example script works. They are, however, all good habits to get into. If this script is extended, embedded within some other script, or used as the logic for a module, benefits will accrue: for example, code is less error-prone, easier to debug if errors do occur, and easier to maintain.

    — Ken