%
% HW2_7.m
% EGR 390, Spring 2009
%
% Author: J Cardell
% Date: Feb 2009
%
% Homework 2, problem 7: National economy
% Note that the 'pause' statements pause program execution.  To continue
% simply press the <space> key
%
clf

a = 0.9;         % marginal propensity to consume, a < 1
b = 0.5;         % constant of adjustment, b > 0
y(1) = 1;       % initial conditions
y(2) = 1.1;

% plot national income for 30 years, using values provided in problem
% statement
for k = 1:28
    y(k+2) = a*(1+b)*y(k+1) - a*b*y(k) + 1;
end
t = [1:1:30];
plot (t, y, 'r')
title ('National Income Over 30 Years')
ylabel ('National Income, $T')
xlabel ('Year')
axis ([0 30 0 10])
pause

% Sensitivity to initial conditions
a = 0.9;         % marginal propensity to consume, a < 1
b = 0.5;         % constant of adjustment, b > 0
% plot national income for 30 years, using values provided in problem
% statement
y1a(1) = 1;       % initial conditions
y1a(2) = 1.1;
for k = 1:28
    y1a(k+2) = a*(1+b)*y1a(k+1) - a*b*y1a(k) + 1;
end
y1b(1) = 1;       % initial conditions
y1b(2) = 0;
for k = 1:28
    y1b(k+2) = a*(1+b)*y1b(k+1) - a*b*y1b(k) + 1;
end
y1c(1) = 0;       % initial conditions
y1c(2) = 1;
for k = 1:28
    y1c(k+2) = a*(1+b)*y1c(k+1) - a*b*y1c(k) + 1;
end
y1d(1) = 2;       % initial conditions
y1d(2) = 3;
for k = 1:28
    y1d(k+2) = a*(1+b)*y1d(k+1) - a*b*y1d(k) + 1;
end
y1e(1) = 5;       % initial conditions
y1e(2) = 7;
for k = 1:28
    y1e(k+2) = a*(1+b)*y1e(k+1) - a*b*y1e(k) + 1;
end
t = [1:1:30];
plot (t, y1a, 'r-', t, y1b, 'b:', t, y1c, 'g', t, y1d, 'c--', t, y1e, 'm')
title ('National Income Over 30 Years: Sensitivity to Initial Conditions')
ylabel ('National Income, $T')
xlabel ('Year')

pause

% Sensitivity to 'a' - the marginal propensity to consume;  a < 1
b = 0.5;         % constant of adjustment, b > 0
y2a(1) = 2;       % initial conditions
y2a(2) = 3;
y2b(1) = 2;       % initial conditions
y2b(2) = 3;
y2c(1) = 2;       % initial conditions
y2c(2) = 3;
y2d(1) = 2;       % initial conditions
y2d(2) = 3;
y2e(1) = 2;       % initial conditions
y2e(2) = 3;

% plot national income for 30 years, using values provided in problem
% statement
a = 0.1;         % marginal propensity to consume, a < 1
for k = 1:28
    y2a(k+2) = a*(1+b)*y2a(k+1) - a*b*y2a(k) + 1;
end
a = 0.25;         % marginal propensity to consume, a < 1
for k = 1:28
    y2b(k+2) = a*(1+b)*y2b(k+1) - a*b*y2b(k) + 1;
end
a = 0.5;         % marginal propensity to consume, a < 1
for k = 1:28
    y2c(k+2) = a*(1+b)*y2c(k+1) - a*b*y2c(k) + 1;
end
a = 0.8;         % marginal propensity to consume, a < 1
for k = 1:28
    y2d(k+2) = a*(1+b)*y2d(k+1) - a*b*y2d(k) + 1;
end
a = 0.98;         % marginal propensity to consume, a < 1
for k = 1:28
    y2e(k+2) = a*(1+b)*y2e(k+1) - a*b*y2e(k) + 1;
end
t = [1:1:30];
plot (t, y2a, 'r-', t, y2b, 'b:', t, y2c, 'g', t, y2d, 'c--')
title ('National Income Over 30 Years: Sensitivity to a')
ylabel ('National Income, $T')
xlabel ('Year')
pause
hold on
plot (t, y2e, 'm')
title ('National Income Over 30 Years: Sensitivity to a')
ylabel ('National Income, $T')
xlabel ('Year')

pause

% Sensitivity to 'b' - the constant of adjustment that describes how the
% rate of private investment is affected by changes in consumer spending;
% b > 0
clf
clear 
a = 0.9;         % marginal propensity to consume, a < 1
y3a(1) = 1;       % initial conditions
y3a(2) = 1.1;
y3b(1) = 1;       % initial conditions
y3b(2) = 1.1;
y3c(1) = 1;       % initial conditions
y3c(2) = 1.1;
y3d(1) = 1;       % initial conditions
y3d(2) = 1.1;
y3e(1) = 1;       % initial conditions
y3e(2) = 1.1;

% plot national income for 30 years, using values provided in problem
% statement
b = 0.5;         % constant of adjustment, b > 0
for k = 1:28
    y3a(k+2) = a*(1+b)*y3a(k+1) - a*b*y3a(k) + 1;
end
b = 0.75;         % constant of adjustment, b > 0
for k = 1:28
    y3b(k+2) = a*(1+b)*y3b(k+1) - a*b*y3b(k) + 1;
end
b = 0.9;         % constant of adjustment, b > 0
for k = 1:28
    y3c(k+2) = a*(1+b)*y3c(k+1) - a*b*y3c(k) + 1;
end
b = 1.2;         % constant of adjustment, b > 0
for k = 1:28
    y3d(k+2) = a*(1+b)*y3d(k+1) - a*b*y3d(k) + 1;
end
b = 1.8;         % constant of adjustment, b > 0
for k = 1:28
    y3e(k+2) = a*(1+b)*y3e(k+1) - a*b*y3e(k) + 1;
end
t = [1:1:30];
plot (t, y3a, 'r-', t, y3b, 'b:', t, y3c, 'g', t, y3d, 'c--')
title ('National Income Over 30 Years: Sensitivity to b')
ylabel ('National Income, $T')
xlabel ('Year')
hold on
pause
plot (t, y3e, 'm')
title ('National Income Over 30 Years: Sensitivity to b')
ylabel ('National Income, $T')
xlabel ('Year')
