Runge Kutta Method implementation using Perl Lamguage


#Alamgir, CSE, JUST
#------------------------------------Start from here............................
#---------Enter the value in this order............
#Enter the value of x0 :
#Enter the value of y0 :
#Inter the value of x(n) :
#If you want to enter the value of h press 1 otherwise press any key....
#If you press 1, you need to Enter the value of h :
#Otherwise it can take the value of h automatically...
#Press 2 for 2nd order, 3 for 3rd order, 4 for 4th order.....

sub func
{
    ($x,$y) = @_;
    #Enter he function{(dy/dx),y1} in here..........
    #return (($x*$x)*($y*$y));
    #return ($x*$y);
    return -$y;
   # $res = ((2*$x*$y)+exp($x))/(($x*$x)+($x*exp($x)));
    #return $res;
}

sub senond_order
{
    ($x,$y) = @_;
    $res1 = $x+$y;
    print"\n\nThe result for Second Order is : $res1\n\n";
}
sub third_order
{
    ($x,$y) = @_;
    $res2 = $x+$y;
    print"\n\nThe result for Third Order is : $res2\n\n";
}
sub fourth_order
{
    ($x,$y) = @_;
    $res3 = $x+$y;
    print"\n\nThe result for Fourth Order is : $res3\n\n";
    print "\n\n";
}


    $h = 0.0;
    print"Enter the value of x0 : \n";
    $x0 = <>;
    print"Enter the value of y0 : \n";
    $y0;
    $y0 = <>;

    print"Inter the value of x(n) : \n";
    $xn = <>;
    print"If you want to enter the value of h press 1 otherwise press any key....\n";

    $pr = <>;
    $h2;
    if($pr==1){
        print"\nEnter the value of h : \n";
        $h2 = <>;
        $h = $h2;
    }
    else{
        $h = $xn-$x0;
    }
  
    $k1,$k2;
    $k1 = $h*&func($x0,$y0);
    $k2 = $h*&func(($x0+($h/2)),($y0+($k1/2)));


    print"Press 2 for 2nd order, 3 for 3rd order, 4 for 4th order.....\n";
    $pv;
    $pv = <>;
    if($pv==2){
        &senond_order($y0,$k2);
    }
     elsif($pv==3){
         $k3 = $h*&func(($x0+$h),($y0+2*$k2-$k1));
         $dh = ($k1 + 4*$k2 + $k3)/6;
        &third_order($y0,$dh);
    }
      elsif($pv==4){
         $k3 = $h*&func(($x0+($h/2)),($y0+($k2/2)));
         $k4 = $h*&func($x0+$h,$y0+$k3);
         $dh = ($k1 + 2*$k2 + 2*$k3 + $k4)/6;

        &fourth_order($y0,$dh);
    }
    else{
        print "\n\nYou should follow/enter the correct value Because your choose number is not correct..\n\n";
    }
   

No comments:

Post a Comment