% function DynamischeAnalyse()
%-------------------------------------------------------------------------------------------------------------
%
% Matlab/Octave script written by Dipl.-Ing. Michael Jeschke
% (tested successfully with Matlab R2007b and with Octave 3.0.0)
% (Note for Octave: Use octplot instead of gnuplot (command "toggle_octplot"), because with gnuplot the
%                   figure labels are somehow messed up)
%
% Idea and methodology taken from Prof. Dr. Jürgen Kremer
%
%-------------------------------------------------------------------------------------------------------------
% * Tested to behave identically to the tool of Prof. Dr. Jürgen Kremer
%   (only for "option_distribution_of_wages = 0" (rule "3a") exactly identical behaviour!)
%
% * Java Applet Tool:
%   http://www.rheinahrcampus.de/fileadmin/prof_seiten/kremer/applets/DynamicAnalysis/AppletDynamicAnalysis.html
% 
% * Documentation:
%   http://www.rheinahrcampus.de/fileadmin/prof_seiten/kremer/masterunsichtbarehand.pdf
%   http://www.rheinahrcampus.de/fileadmin/studiengaenge/diplwima/dokumente/EineAndereUnsichtbareHand.ppt
%-------------------------------------------------------------------------------------------------------------
%
% This script simulates the behaviour and the development of a closed economy/national economy
% (german: Volkswirtschaft) over many years.
% For simplicity, the modelled economy contains only households and companies (=firms), but no state. Also
% profit-making banks are not modelled explicity, i.e. the interest rate for taking credits and for giving
% credits is the same.
%
% To go more into detail, the following is modelled:
% - Households are grouped into N household groups. Each household group thus represents 1/N-th of the total
%   number of households in the economy.
% - Each household group k (k=1..N) is associated with a yearly income h.W(k,1) (wages) and a
%   yearly consumption h.C(k,1) in the first year t=1.
% - Moreover, each household group k has a certain amount of interest-bearing assets h.A_ini(k) at the
%   beginning of the first year.
% - The economy is further characterised by a constant yearly growth rate "y" of the gross domestic product
%   GDP (german: BIP).
% - Finally, the economy is associated with a constant yearly interest rate "r". This interest rate is kept
%   constant throughout the simulation, when possible. Why "when possible"? Because, if the formulas at some
%   point in time give unreasonable results like negative incomes paid by the firms to the households, the
%   interest rate "r" is automatically adapted (i.e. reduced) by the simulator such that the simulation still
%   makes sense. Closer investigation shows that an alternative would be to increase the GDP growth rate and
%   to keep the interest rate constant in such case. This whould be another mathematical means for avoiding
%   negative wages. However, in this simulation it is decided that the GDP's growth rate "y" is constant and
%   cannot increase arbitrarily (just like in real-world).
%
% The following diagram illustrates the money-flow of the economy and the underlying basic equations and
% definitions:
%
%
% Circulation of a Closed Economy without Inclusion of the State (arrows show the flow of money):
% -----------------------------------------------------------------------------------------------
%
%       ,------------------ (Labour Market) <------------------,
%     W |                                                      | W
%       |              S                         Rf=R          |
%       |     ,----------> (Financial Market) <---------,      |
%       |     |                                         |      |
%       |     |                                         |      |
%      \|/   \|/ Rh=R                                I \|/     |
%       '     '                                         '      |
%   ,------------,                                  ,-------------,
%   | Households |                                  |    Firms    |
%   '------------'                                  '-------------'
%         |                                                ,
%         |                                               /|\
%         |       C                                 P      |
%         '--------------> (Goods & Services) -------------'
% 
% Symbols:
% --------
% S = Saving of households (normally positive, but can be negative)
% C = Consumption of households 
% I = Investment credits taken by firms (normally positive, but can be negative)
% P = Proceeds of sales of the firms (german: Verkaufserlöse), P=C
% W = Wages
% R = Interest received by households (Rh) or payed by firms (Rf), R=Rh=Rf
% 
% Households:
% -----------
% C+S = W+R = Y (=economy's GDP)
% 
% Firms:
% ------
% P+I = W+R = Y (=economy's GDP)
%
%
% Modelling Assumptions (Rules), taken from the Model of Prof. Dr. Jürgen Kremer:
% -------------------------------------------------------------------------------
% 1)  The economy's total consumption grows at the same rate as the economy's GDP (see parameter "y").
% 2)  The ratio of consumption between any two households remains the same all the time, this means that e.g.
%     if household_1 consumes 1.5 times more than household_2 in the initial state, this ratio remains 1.5
%     all the time.
% 3)  For the wages, two possibilities are implemented (one of them is to be selected):
%  a) The ratio of wages between any two households remains the same all the time, this means that e.g.
%     if household_1 consumes 1.5 times more than household_2 in the initial state, this ratio remains 1.5
%     all the time.
%  b) The wages of the strongest household grow in parallel with the GDP, while the remaining households share
%     the remaining of the total economy's income in "some way" (two "ways" implemented -> 3b1, 3b2, but both
%     are not exactly the same as the model of Prof. Dr. Kremer)
%
% This script includes some additional rules that can be activated by parameter setting:
% --------------------------------------------------------------------------------------
% 4) A minimum wage per household group can be configured, which is a "must be" contraint in each year's
%    calculation of the simulator. Activating this rules means that rules 2 and 3 can get overruled.
% 5) It can be configured that a household group's consumption may not exceed its wages
%    by a certain percentage. Activating this rules means that rules 1 can get overruled.
% 6) It can be configured that a household group's consumption may not exceed its wages plus interest flow
%    by a certain percentage. Activating this rules means that rules 1 can get overruled.
% 7) It can be configured that, no matter how low a household group's wages (or negative interest flow) may
%    ever be, a minimum consumption will alway occur for survival.
%    Activating this rules means that rules 5&6 can get overruled.
%
%-------------------------------------------------------------------------------------------------------------
%
% Simulation Results, Observations:
% ---------------------------------
% 1) The aggregated savings (i.e. assest) of the economy (here the households) exactly correspond to the
%    aggregated debts of the economy (here the firms, plus households with negative assets).
%    The fact that assets earn interest (which increases the assets) therefore entails a symmetrically
%    increasing amount of debts at the firms. Thus their interest payments increase accordingly.
% 2) If the GDP grows slower than the total intererst (in absolute figures, not in terms of the rate, see
%    top-right figure of the plots), the economy's total wages decrease over time.
%    (top-left figure) [about equilibrium with default settings and r=0.035 and y=0.012].
%    In this case a greater part of the GDP flows to the owners of the capital, while the wages decrease
%    accordingly. Therefore, efforts to limit the interest's share in the GDP require a permanent growth of
%    the GDP.
% 3) The households with lower income can less and less afford consumption as the share of interest within
%    the GDP increases. Finally they have to take debts (their assets become negative).
% 4) Even if the rate of growth of the GDP is clearly above the interest rate, it can still happen that
%    lower household groups have to take debts. This does not occur for the case that interest rate is zero
%    (even for negative GDP growth), which means that an interest-bearing economy, unlike a zero-interest
%    economy, causes these problems to the lower household groups even if the GDP growth is the same, or
%    higher, than the economy's interest rate.
%    In other words, the notion that an economy with "GDP growth rate==interest rate >0" behaves similar as an
%    economy with "GDP growth rate==interest rate==0", because the GDP growth "compensates" for the negative
%    effects of the interest rate, is simply not true. The economy's behaviour is still substantially
%    different.
% 5) Groups with few assets have few interest earnings. On the other hand, they pay interests indirectly via
%    their consumption, because the interest paid by the firms must be included in the costs of their goods
%    and services. By this, these household groups become net-payers of interest. On the other hand, groups
%    with lots of assets earn more interest than what they pay by what they consume. These groups are
%    therefore net-earners of interest.
%    By this, there is a constant stream of (interest-related)money flowing from net-payers to net-earners.
%    This transfer of interest, referring to Adam Smith, could be called another "HIDDEN HAND OF THE MARKET".
%    It is a particular means of shifting money, and this means is invisible at the first glance. As long as
%    GDP growth rates are high (compared to the interest rates), the negative consequences are hardly visible.
%    But if the growth gets lower, this mechanism becomes destructive.
% 6) If the interest rate of the economy is ZERO, the development of wages, assets and consumption remains
%    stable for all households. This is true even if the growth of the economy decreases or stops.
%    Even if the economy has negative growth, the system remains stable, and there is no hidden money flow
%    from one household group to another.
% 
% 7) If the interest rate is higher than the GDP growth rate, it can even happen on the long term that the
%    income of the households become negative in this model. This is of course not possible in reality, which
%    proves that a high interest rate at low GDP growth cannot sustain on the long term. The only solution
%    to resolve the situation (for a given GDP growth rate) is the reduction of the interest rate.
% 8) If the interest rate is higher than the GDP growth rate, it can also happen on the long term that the
%    share of interest in the consumer spendings becomes mathematically greater than 100%. This means that the
%    companies' proceeds of sales are not even sufficient to pay back their own interest, and the only way
%    they can pay them back, plus paying the salaries, is by taking on even more credits. This effectively
%    corresponds to bankruptcy of the firms.
% 9) Many other interesting observations can be made when playing with the parameters and running the
%    simulation.
%
%-------------------------------------------------------------------------------------------------------------

clear all;

%% 0. - Parameters
T = 51; %[51] number of years to simulate
r = 0.035; %[0.035] interest rate in 100 percent (assume same for households and firms)
%          % (can be forcably changed by the simulator is otherwise minimum wages cannot be achieved)
y = 0.01; % [0.01] economic growth in 100 percent (german: "Wirtschaftswachstum")

% Other settings:
% T = 81; % number of years to simulate
% r = 0.03;
% y = 0.03;
% r = 0.01;
% y = 0.02;

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Rule 3: (default = 0 (3a))
option_distribution_of_wages = 0; % 0 = 3a:  Straight forward, wages have constant ratio
%                                 % 1 = 3b1: Empirical, strongest group's wages grow like the economy's GDP,
%                                 %          other groups share with constant ratio
%                                 % 2 = 3b2: Empirical, strongest group's wages grow like the economy's GDP,
%                                 %          next strongest group's wages do this as well if possible, etc.


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

%% 0.1 - More parameters, not contained in Prof. Kremer's Applet: (default value = behaviour of the applet)
min_wages = -inf;% default = -inf.
%                % The minimum wages that *must* be payed per household group.
consumption_wages_max_ratio = inf;% default = inf -> consumption is not limited by wages.
%                                 % If < inf, e.g. =1.03, then the consumption of a household group can be
%                                 %   at maximum equal to 1.03 times the wages.
%                                 %   (unless it gets below "min_consumption")
consumption_W_plus_R_max_ratio = inf;% default = inf -> consumption is not limited by wages or interest.
%                                 % If < inf, e.g. =1.03, then the consumption of a household group can be
%                                 %   at maximum equal to 1.03 times the sum of wages plus interest.
%                                 %   (unless it gets below "min_consumption")
min_consumption = -inf;% default = -inf.
%                      % This is the minimum "survival consumption" per household group. Even if this exceeds
%                      % the limit based on the wages and the "consumption_wages_max_ratio",
%                      % this minimum consumption is done in any case.

% Other settings:
if 0,
    min_wages = 8;
    %consumption_wages_max_ratio = 1.03;
    consumption_W_plus_R_max_ratio = 1.03;
    min_consumption = 7;
end

% ------------------------------------------------------------------------------------------------------------

%% 0.2 - Initial Values for Household Data (yearly) for 1st year:

% Important note for parameter configuration:
%    The households in this list must be ordered in ascending order of their income (wages),
%    i.e. lowest wages first ... highest wages last !!!

if 0,
    N = 3; % number of households (more precisely: household *groups*) in the simulated economy
    plotVector = [1:N]; % list of household groups (indices) to be plotted

    h.W(1,1)   =  10; % Wages (german: Einkommen, Gehalt)
    h.C(1,1)   =  10; % Consumption (german: Konsum)
    h.A_ini(1) =   0; % Assets (german: Vermögen)

    h.W(2,1)   =  50;
    h.C(2,1)   =  30;
    h.A_ini(2) = 100;

    h.W(3,1)   = 100;
    h.C(3,1)   =  50;
    h.A_ini(3) = 500;
end

if 1,
    N = 10; % number of households (more precisely: household *groups*) in the simulated economy
    plotVector = [1:N]; % list of household groups (indices) to be plotted

    h.W(1,1)   =  10; % Wages (german: Einkommen, Gehalt)
    h.C(1,1)   =  10; % Consumption (german: Konsum)
    h.A_ini(1) =  10; % Assets (german: Vermögen)

    h.W(2,1)   =  18;
    h.C(2,1)   =  18;
    h.A_ini(2) =  17;

    h.W(3,1)   =  22;
    h.C(3,1)   =  22;
    h.A_ini(3) =  46;

    h.W(4,1)   =  26;
    h.C(4,1)   =  26;
    h.A_ini(4) =  61;

    h.W(5,1)   =  29;
    h.C(5,1)   =  27;
    h.A_ini(5) =  83;

    h.W(6,1)   =  33;
    h.C(6,1)   =  29;
    h.A_ini(6) = 114;

    h.W(7,1)   =  38;
    h.C(7,1)   =  33;
    h.A_ini(7) = 172;

    h.W(8,1)   =  44;
    h.C(8,1)   =  37;
    h.A_ini(8) = 245;

    h.W(9,1)   =  53;
    h.C(9,1)   =  40;
    h.A_ini(9) = 451;

    h.W(10,1)  =  76;
    h.C(10,1)  =  45;
    h.A_ini(10)=1220;
end

% ------------------------------------------------------------------------------------------------------------
% ------------------------------------------------------------------------------------------------------------
% ------------------------------------------------------------------------------------------------------------

%% 1. - Main Part

%% 1.1 - Initializations for all household groups, for year 1:
Y(1) = 0; % init the GDP
for k = 1:N, % household loop
    h.Rh(k,1) = r * h.A_ini(k);
    h.S(k,1) = h.W(k,1) + h.Rh(k,1) - h.C(k,1); % savings in year 1
    h.A(k,1) = h.A_ini(k) + h.S(k,1); % <--- This is the assest at the end of year 1.
end
R(1) = sum(h.Rh(1:N,1));
S(1) = sum(h.S(1:N,1));

% Init Wages Ratios between households:
W(1) = sum(h.W(1:N,1));
W_ratio_alpha = h.W(1:N,1) / W(1);

% Init Consumption Ratios between households:
C(1) = sum(h.C(1:N,1));
C_ratio_beta = h.C(1:N,1) / C(1);

% Init others:
Y(1) = W(1) + R(1); % Y(1) is the GDP of year 1.
A(1) = sum(h.A(1:N,1));
for k = 1:N,
    h.Rc(k,1) = C_ratio_beta(k) * R(1);
end
Rc(1) = sum(h.Rc(1:N,1));

r_actual(1) = r;% actual interest rate can become lower at later times, by forces of the system.
r_act_reduced(1) = NaN;

%% 1.2 - Loop, starting from year t=2:
for t = 2:T, % time loop
    %% 1.2.1 - New GDP for Year t, acc. to the design parameter "y":
    Y(t) = (1+y) * Y(t-1);

    %% 1.2.2 - New Interest Flow Rh:
    for k = 1:N, % household loop
        h.Rh(k,t) = r * h.A(k,t-1);%                                 <--- Interest flow per household group
    end
    R(t) = sum(h.Rh(1:N,t));
    r_actual(t) = r;
    r_act_reduced(t) = NaN;
    
    % Check if minimum household wages can be payed with this interest rate. If not, interest rate has to be
    % forcably reduced until enough is left for the wages, by the formula W = Y - R:
    W_tot_tmp = Y(t) - R(t);
    W_tot_min = N*min_wages;
    if W_tot_tmp < W_tot_min,
        R(t) = Y(t) - W_tot_min;
        % Corrected calculation per household:
        r_actual(t) = NaN;
        r_act_reduced(t) = R(t)/A(t-1);
        for k = 1:N,
            h.Rh(k,t) = r_act_reduced(t) * h.A(k,t-1);
        end
    end

    %% 1.2.3 - New Wages:
    % Total income in the economy in year t:
    W(t) = Y(t) - R(t);
    if option_distribution_of_wages == 0, % Rule (3a)                <--- Wages per household group, Rule 3a
        W_rest = W(t);
        for k = 1:N, % start loop with poorest household group to guarantee minimum wages
            h.W(k,t) = W_ratio_alpha(k)/(1-sum(W_ratio_alpha(1:k-1))) * W_rest;
            if h.W(k,t) < min_wages,
                h.W(k,t) = min_wages;% guarantee minimum income (wages)
            end
            W_rest = W_rest - h.W(k,t);
        end
    elseif option_distribution_of_wages == 1, % Rule (3b1)           <--- Wages per household group, Rule 3b1
        % Highest income group grows like GDP, but not above the total income minus a reserve for min. wages:
        h.W(N,t) = min(W(t)-max(0,(N-1)*min_wages), h.W(N,t-1)*(1+y));
        W_rest = W(t) - h.W(N,t); % total wages minus the wages from the richest household group
        for k = 1:N-1, % start loop with poorest household group to guarantee minimum wages
            h.W(k,t) = W_ratio_alpha(k)/(1-sum(W_ratio_alpha(1:k-1))-W_ratio_alpha(N)) * W_rest;
            if h.W(k,t) < min_wages,
                h.W(k,t) = min_wages;% guarantee minimum income (wages)
            end
            W_rest = W_rest - h.W(k,t);
        end
    elseif option_distribution_of_wages == 2, % Rule (3b2)           <--- Wages per household group, Rule 3b2
        W_rest = W(t);
        for k = N:-1:1, % Household loop, starting with highest income group to give preference to a growth
            % like the GDP.
            % Grows like GDP, but not above the total income minus a reserve for min. wages for lower groups:
            h.W(k,t) = min(W_rest-max(0,(k-1)*min_wages), h.W(k,t-1)*(1+y));
            W_rest = W_rest - h.W(k,t); % total wages minus the wages from this household group
        end
    else
        disp(['ERROR: Invalid value for "option_distribution_of_wages"!']);
    end
    % Error check (should never happen):
    if abs(1 - sum(h.W(1:N,t))/W(t)) > sqrt(eps),
        disp(['ERROR (t=' num2str(t) '): Sum of wages not equal to total wages!!!']);
        sum_of_wages = sum(h.W(1:N,t))
        total = W(t)
        delta_sum_minus_total = sum_of_wages - total
    end

    %% 1.2.4 - New Consumption:
    for k = 1:N, % household loop
        h.C(k,t) = (1+y) * h.C(k,t-1);% (Rule 1 & 2)                 <--- Consumption per household group
        if consumption_wages_max_ratio < inf,% limit consumption acc. to wages
            h.C(k,t) = min(h.C(k,t), consumption_wages_max_ratio * h.W(k,t));
        end
        if consumption_W_plus_R_max_ratio < inf,% limit consumption acc. to wages plus interest
            h.C(k,t) = min(h.C(k,t), consumption_W_plus_R_max_ratio * (h.W(k,t) + h.Rh(k,t)));
            h.C(k,t) = max(h.C(k,t), 0);
        end
        h.C(k,t) = max(h.C(k,t), min_consumption);
    end
    C(t) = sum(h.C(1:N,t));

    %% 1.2.5 - New Assets & Savings:
    for k = 1:N, % household loop                                    <--- Savings & Assets per household group
        h.S(k,t) = h.W(k,t) + h.Rh(k,t) - h.C(k,t);
        h.A(k,t) = h.A(k,t-1) + h.S(k,t);
    end
    A(t) = sum(h.A(1:N,t));
    S(t) = sum(h.S(1:N,t));
    
    %% 1.2.6 - New Interest contained in the consumption:
    for k = 1:N,
        h.Rc(k,t) = h.C(k,t) / C(t) * R(t); % The interest R to be paid by the firms is included in the
    end                                     % prices C of the consumed goods.
    Rc(t) = sum(h.Rc(1:N,t));
end


%% 2. - Show Results
figure
% 1st subplot:
subplot(2,2,1);
for k=plotVector,
    hd_1 = plot([0:T-1], h.W(k,:), 'b'); hold on;
    hd_2 = plot([0:T-1], h.C(k,:), 'r-.');
end
hd_z = plot([0:0], W(1), 'k.');
hd_y = plot([0:T-1], Y, 'k');
plot([0:T-1], W, 'b.');
plot([0:T-1], C, 'r.');
if consumption_wages_max_ratio < inf || consumption_W_plus_R_max_ratio < inf,
    xlabel(['household consumption <',num2str(consumption_wages_max_ratio),'*wages, and <', ...
        num2str(consumption_W_plus_R_max_ratio),'*(wages+interest).']);
else
    xlabel(['time in years']);
end
ylabel(['money']);
if ~exist('__nargin__','var'),% Matlab only:
    legend([hd_1, hd_2, hd_z, hd_y], 'Wages', 'Consumption', '...sum all, respectivly', 'GDP', ...
        'location', 'northwest')
else%if exist('__nargin__','var'),% Octave only:
    hh=get(gca,'children');
    L = length(plotVector);
    idx = [2*L-1, 2*L, [2*L+1,2*L+2], [1:2*(L-1)], [2*L+3:2*L+4]]; idx = length(idx)+1-idx;
    hh = flipud(hh(idx));
    set(gca,'children',hh);
    legend('Wages', 'Consumption', '...sum all, respectivly', 'GDP', ...
        'location', 'northwest')
end
grid on;
tmp=axis; plot([tmp(1),tmp(2)],[0,0],'k');% Plot horizontal line, x-axis.
title(['Yearly Wages and Consumption per Houshold Group, and the sum. Min.Wages=',num2str(min_wages), ...
    ', Min.Consump.=',num2str(min_consumption),'.'])

% 2nd subplot:
subplot(2,2,2);
for k=plotVector,
    hd_1 = plot([0:T-1], h.Rh(k,:), 'b'); hold on;
    hd_3 = plot([0:T-1], h.Rh(k,:) - h.Rc(k,:), 'm');
    hd_2 = plot([0:T-1], h.Rc(k,:), 'r-.');
end
hd_r = plot([0:T-1], R, 'b.');
hd_y = plot([0:T-1], Y, 'k');
xlabel(['time in years']);
ylabel(['money']);
if ~exist('__nargin__','var'),% Matlab only:
    legend([hd_1, hd_2, hd_3, hd_r, hd_y], 'Interest gained', 'Interest in consumption', ...
        'Interest flow, net', 'Interest gained, sum all', 'GDP', 'location', 'northwest')
else%if exist('__nargin__','var'),% Octave only:
    hh=get(gca,'children');
    L = length(plotVector);
    idx = [3*L-2, 3*L, 3*L-1, [3*L+1, 3*L+2], [1:3*(L-1)]]; idx = length(idx)+1-idx;
    hh = flipud(hh(idx));
    set(gca,'children',hh);
    legend('Interest gained', 'Interest in consumption', ...
        'Interest flow, net', 'Interest gained, sum all', 'GDP', 'location', 'northwest')
end
grid on;
tmp=axis; plot([tmp(1),tmp(2)],[0,0],'k');% Plot horizontal line, x-axis.
title('Interests in the Household Groups')

% 3rd subplot:
subplot(2,2,3);
for k=plotVector,
    hd_3 = plot([0:T-1], h.A(k,:), 'g'); hold on;
    hd_4 = plot([0:T-1], h.S(k,:), 'c');
end
hd_y = plot([0:T-1], Y, 'k');
hd_z = plot([0:0], A(1), 'k.');
plot([0:T-1], A, 'g.');
plot([0:T-1], S, 'c.');
xlabel(['time in years']);
ylabel(['money']);
if ~exist('__nargin__','var'),% Matlab only:
    legend([hd_3, hd_4, hd_z, hd_y], 'Assets', 'Savings', '...sum all, respectivly', 'GDP', 'location', 'northwest')
else%if exist('__nargin__','var'),% Octave only:
    hh=get(gca,'children');
    L = length(plotVector);
    idx = [2*L-1, 2*L, [2*L+2,2*L+1], [1:2*(L-1)], [2*L+3:2*L+4]]; idx = length(idx)+1-idx;
    hh = flipud(hh(idx));
    set(gca,'children',hh);
    legend('Assets', 'Savings', '...sum all, respectivly', 'GDP', 'location', 'northwest')
end
grid on;
tmp=axis; plot([tmp(1),tmp(2)],[0,0],'k');% Plot horizontal line, x-axis.
title('Savings and Assets per Household Group, and the sum')

% 4th subplot:
subplot(2,2,4);
hd_y = plot([0:T-1], 100*y*ones(1,T), 'k-.'); hold on;
hd_a = plot([0:T-1], 100*Rc./C, 'r.'); hold on;
hd_r = plot([0:T-1], 100*r_actual, 'k.');
hd_w = plot([0:T-1], 100*W./Y, 'b');
hd_i = plot([0:T-1], 100*Rc./Y, 'b-.');
if sum(isnan(r_actual)) > 0,% "r" was reduced at least once
    hd_rr= plot([0:T-1], 100*r_act_reduced, 'c.');
end
xlabel(['time in years']);
ylabel(['percent']);
if ~exist('__nargin__','var'),% Matlab only:
    if sum(isnan(r_actual)) > 0,% "r" was reduced at least once
        legend([hd_a, hd_r, hd_rr, hd_w, hd_i, hd_y], 'Interest contained in consumption', ...
            'Economy''s Interest Rate "r"', ...
            'Economy''s Interest Rate "r", reduced', 'Wages-to-GDP-ratio', 'Interest-to-GDP-ratio', ...
            'GDP growth rate', 'location', 'northwest')
    else
        legend([hd_a, hd_r, hd_w, hd_i, hd_y], 'Interest contained in consumption', ...
            'Economy''s Interest Rate "r"', 'Wages-to-GDP-ratio', 'Interest-to-GDP-ratio', ...
            'GDP growth rate', 'location', 'northwest')
    end
else%if exist('__nargin__','var'),% Octave only:
    if sum(isnan(r_actual)) > 0,% "r" was reduced at least once
        hh=get(gca,'children');
        hh = hh([6,2,3,1,4,5]);
        set(gca,'children',hh);
        legend('Interest contained in consumption', 'Economy''s Interest Rate "r"', ...
            'Economy''s Interest Rate "r", reduced', 'Wages-to-GDP-ratio', 'Interest-to-GDP-ratio', ...
            'GDP growth rate', 'location', 'northwest')
    else
        hh=get(gca,'children');
        hh = hh([5,1,2,3,4]);
        set(gca,'children',hh);
        legend('Interest contained in consumption', 'Economy''s Interest Rate "r"', ...
            'Wages-to-GDP-ratio', 'Interest-to-GDP-ratio', ...
            'GDP growth rate', 'location', 'northwest')
    end
end
grid on;
tmp=axis; plot([tmp(1),tmp(2)],[0,0],'k');% Plot horizontal line, x-axis.
title(['Global Economy''s Data for interest rate r=',num2str(100*r),'% and GDP growth y=',num2str(100*y),'%.'])

