#!/usr/bin/perl -w

use 5.034;

my @cases = 'A'..'F';

my $DEBUG = 0;

my ($capital, $interest_rate, $mpc, $mpi, $mps, $output, $iter);
for my $i (@cases){    
    if ($i eq 'A'){
	($capital, $interest_rate, $mpc, $mpi, $mps, $output) = (1500, 0.05, 0.7, 0.2, 0.1, 1700);
    }elsif ($i eq 'B'){
	($capital, $interest_rate, $mpc, $mpi, $mps, $output) = (1500, 0.02, 0.8, 0.1, 0.1, 1700);
    }elsif ($i eq 'C'){
	($capital, $interest_rate, $mpc, $mpi, $mps, $output) = (1500, 0.08, 0.6, 0.3, 0.1, 1700);
    }elsif ($i eq 'D'){
	($capital, $interest_rate, $mpc, $mpi, $mps, $output) = (1500, 0.05, 0.6, 0.2, 0.2, 1700);
    }elsif ($i eq 'E'){
	($capital, $interest_rate, $mpc, $mpi, $mps, $output) = (1500, 0.02, 0.6, 0.2, 0.2, 1700);
    }elsif ($i eq 'F'){
	($capital, $interest_rate, $mpc, $mpi, $mps, $output) = (1500, 0.05, 0.7, 0.2, 0.2, 1700);
    }else{ next; }	
    
# Solve the model equations
    my ($equilibrium_output, $consumption, $investment, $savings, $new_interest_rate) =
      solve_model($capital, $interest_rate, $mpc, $mpi, $mps, $output);

# Display the results

    say "Initial capital = $capital";
    say "Interest = $interest_rate";
    say "Marginal Propension to Consum = $mpc";
    say "Marginal Propension to Invest = $mpi";
    say "Marginal Propension to Save = $mps";
    say "Target output level = $output";
    
    say "";
    
    say "Case $i) iter = $iter years";
    say "Output (Y): $equilibrium_output";
    say "Consumption (C): $consumption";
    say "Investment (I): $investment";
    say "Savings accumulated (S): $savings";
#    say "New Interest Rate (R): $new_interest_rate";
    say "-" x 60;
}
    

exit 2;


# Function to solve the model equations
sub solve_model {
    my ($k, $r, $c, $i, $s, $y_t) = @_;

    my $tolerance = 0.01;   # Tolerance for convergence
    my $max_iter = 5;      # Maximum number of iterations
    my ($C, $I, $S);
    $iter = 0;
    my $diff = 1.0;

    my $y = $k;
    
    while ($diff > $tolerance && ++$iter < $max_iter) {

        my $y_prev = $y;

        # Solve for equilibrium consumption (C)
        $C = $y * $c;   # $y;

        # Solve for equilibrium investment (I)
        $I += $i * $y;    # $y

	# Savings
	$S += $y * $s;
	
        # Solve for equilibrium output (Y)
        $y = $C + $I + $r * $S;

        # Update interest rate (R)
#       if ( ($y - $y_prev)/ $y_prev < $interest_rate ){
#	    $r = ($y - $y_prev) / $y_prev;
#	    if ($r < 0) {
#		$r = $interest_rate;
#	    }
#	}
	
	say "Year $iter  Consumption $C  Investments $I   Savings $S   Output $y" if $DEBUG; 
	
        $diff = $y_t - $y;

    }

    return ($y, $C, $I, $S, $r);
}



__END__

This is a unorthodox semi-right wing model since $y > $C + $I
  thus, modifyng the Muller-Fleming model to escape of Cosumption
  magnification. "Savings worth nothing" worth nothing.

A second issue is about using real interest rates. 0.08 seems very
  hihgh.
  
Third issue is about indexation. The consumer/investment ratio is 
  constant and does not fluctuate.


if (0){
# Get user input for model parameters
print "Enter the initial capital stock (K): ";
my $capital = <STDIN>;
chomp $capital;

print "Enter the initial interest rate (R): ";
my $interest_rate = <STDIN>;
chomp $interest_rate;

print "Enter the marginal propensity to consume (c): ";
my $mpc = <STDIN>;
chomp $mpc;

print "Enter the marginal propensity to save (s): ";
my $mps = <STDIN>;
chomp $mps;

print "Enter the initial output (Y): ";
my $output = <STDIN>;
chomp $output;
}


Example 1:

mathematica

Enter the initial capital stock (K): 100
Enter the initial interest rate (R): 0.05
Enter the marginal propensity to consume (c): 0.75
Enter the marginal propensity to save (s): 0.25
Enter the initial output (Y): 200

Example 2:

mathematica

Enter the initial capital stock (K): 50
Enter the initial interest rate (R): 0.02
Enter the marginal propensity to consume (c): 0.8
Enter the marginal propensity to save (s): 0.2
Enter the initial output (Y): 150

Example 3:

mathematica

Enter the initial capital stock (K): 200
Enter the initial interest rate (R): 0.08
Enter the marginal propensity to consume (c): 0.6
Enter the marginal propensity to save (s): 0.4
Enter the initial output (Y): 300

Output final model
