Sunday, June 27, 2021

Engineering Economy with Octave in real world

โดย ผศ.สราวุฎฐ์ วรสุมันต์ , ประยุทธ พันธุลาภ

Background


1) โรงงานแปรรูปไก่ขนาดใหญ่ที่ลดพนักงาน [video click]

2) โรงงานแปรรูปไก่ขนาดใหญ่ที่ใช้พนักงานค่อนข้างมาก [video click]

3) แปรรูปไก่ที่ใช้อุปกรณ์ไม่ซับซ้อน กำลังผลิตไม่มาก [video click]

"การลดจำนวนแรงงานต่างชาติ เป็นปัญหาใหญ่ที่เกิดใน ยุโรปมานานแล้ว จนในที่สุดต้องออกแบบเครื่องจักรอัตโนมัติมาทำงานแทน ในปัจจุบันโรคระบาดเป็นปัจจัยสำคัญที่ต้องนำมาใช้วิเคราะห์ในการลงทุนด้วย ซึ่งมีหลายประเด็นใน ศตวรรษที่ 21 วิชา Engineering Economy ในสมัยก่อนไม่ได้คำนึงถึงในโลกความจริง ทำให้นำวิชานี้ไปใช้ในโลกความจริงไม่ได้ผล"


Octave Online

website rextester

Lessons

Lesson 0 : About students
Lesson 1 : Equivalence & Interest
Lesson 2 : Cash Flows & Compound Interest
Lesson 3 : Multiple Cash Flows
Lesson 4 : Payback Period
Lesson 5 : Present - Worth Comparisons
Lesson 6 : Future - Worth & Equivalent Annual Worth Comparisons
Lesson 7 : Rate of Return Analysis
Lesson 8 : Rate Of Return Comparisons
Lesson 9 : Benefit / Cost Ratios
Lesson 10 : Comparison & Review Bonds
Lesson 11 : Basic Depreciation
Lesson 12 : MACRS Depreciation & Capital Gains
Lesson 13 : After Tax Cash Flow
Lesson 14 : Replacement Analysis
Lesson 15 : Inflation


Formulas

   A is a uniform series of equal payments at each compounding period;
   P is a present single sum of money at the time zero;
   F is a future sum of money at the end of period n. And i is the compound interest rate.
   i is the compound interest rate
   n is the period.



Introduction

Compound and Discount Concept

Compounding

In order to compare different alternatives in an economic evaluation, they should have the same base (equivalent base). 

Compound interest is a method that can help applying the time value of money. 

For example, assume you have 100 dollars now and you put it in a bank for interest rate of 3% per year. 

After one year, the bank will pay you 100+100*0.03 =$103. Then, you will put the 103 dollars in the bank again for another year. 

One year later, you will have 103+103*0.03 =$106.09. 

   P: Present single sum of money.
   F: A future single sum of money at some designated future date.
   n: The number of periods in the project evaluation life (can be year, quarter or month).
   i: The discount rate (interest rate).

F = P(1+i)n


Example 1-1:

Assume you put 20,000 dollars (principal) in a bank for the interest rate of 4%. 

How much money will the bank give you after 10 years?

F=P(1+i)n =20,000*(1+0.04)10=20,000*1.48024=29604.8


Discounting

In economic evaluations, “discounted” is equivalent to “present value” or “present worth” of money.
P = F(1+i)-n

   P: Present single sum of money.
   F: A future single sum of money at some designated future date.
   n: The number of periods in the project evaluation life (can be year, quarter or month).
   i: The discount rate (interest rate).

  
Example 1-2:

Assuming the discount rate of 10 %, present value of 100 dollars
which will be received in 5 years from now can be calculated as:
F = 100 dollars n = 5 i = 0.1 P = F[(1 + i)-n] = 100[(1 + 0.1)-5] = 62.1 You can see how time and discount rate can affect the value of money in the future. 62.1 dollars is the equivalent present sum that has the same value of 100 dollars in five years under the discount rate of 10%

Formulas in Octave

Octave Code

%============== copy Formulas =============
%============== start of library ==========
%%====================================
%% Engineering Economy Formula
%%====================================


function v = npv (r, p, i)

  if (nargin < 2 || nargin > 3)
    print_usage ();
  endif

  if (! (isvector (p)))
    error ("npv: p has to be a vector");
  else
    n = length (p);
    p = reshape (p, 1, n);
  endif

  if (any (any (r <= -1)))
    error ("npv: all interest rates must be > -1");
  endif
  if (isscalar (r))
    d = 1 ./ (1 + r) .^ (0 : n);
  elseif (isvector (r) && (length (r) == n))
    d = [1, (1 ./ cumprod (reshape (1 + r, 1, n)))];
  else
    error ("npv: r must be a scalar or a vector of the same length as p");
  endif

  if (nargin == 3)
    if (! isscalar (i))
      error ("npv: I_0 must be a scalar");
    endif
  else
    i = 0;
  endif

  p = [i, p];
  v = sum (d .* p);

endfunction


function r = irr (p, i = 0)
  ## Check input
  if (nargin != 1 && nargin != 2)
    print_usage ();
  elseif (! (isvector (p)))
    error ("irr: p must be a vector");
  elseif (! isscalar (i))
    error ("irr: i must be a scalar");
  endif

  ## Solve system
  f = @(x) npv (x, p) - i;
  r = fsolve (f, 0);

endfunction


function p = pmt (r, n, a, l, m)

  if (nargin < 3 || nargin > 5)
    print_usage ();
  endif

  if (! (isscalar (r) && r > -1))
    error ("pmt: rate must be a scalar > -1");
  elseif (! (isscalar (n) && n > 0))
    error ("pmt: n must be a positive scalar");
  elseif (! (isscalar (a) && a > 0))
    error ("pmt: a must be a positive scalar");
  endif

  if (nargin == 5)
    if (! ischar (m))
      error ("pmt: `method' must be a string");
    endif
  elseif (nargin == 4)
    if (ischar (l))
      m = l;
      l = 0;
    else
      m = "e";
    endif
  else
    l = 0;
    m = "e";
  endif

  p = r * (a - l * (1 + r)^(-n)) / (1 - (1 + r)^(-n));

  if (strcmp (m, "b"))
    p = p / (1 + r);
  endif

endfunction



function v = pv (r, n, p, l, m)

  if (nargin < 3 || nargin > 5)
    print_usage ();
  endif

  if (! (isscalar (r) && r > -1))
    error ("pv: r must be a scalar > -1");
  elseif (! (isscalar (n) && n > 0))
    error ("pv: n must be a positive scalar");
  elseif (! isscalar (p))
    error ("pv: p must be a scalar");
  endif

  if (r != 0)
    v = p * (1 - (1 + r)^(-n)) / r;
  else
    v = p * n;
  endif

  if (nargin > 3)
    if (nargin == 5)
      if (! ischar (m))
        error ("pv: `method' must be a string");
      endif
    elseif (ischar (l))
      m = l;
      l = 0;
    else
      m = "e";
    endif
    if (strcmp (m, "b"))
      v = v * (1 + r);
    endif
    if (isscalar (l))
      v = v + pvl (r, n, l);
    else
      error ("pv: l must be a scalar");
    endif
  endif

endfunction

function v = npv (r, p, i)

  if (nargin < 2 || nargin > 3)
    print_usage ();
  endif

  if (! (isvector (p)))
    error ("npv: p has to be a vector");
  else
    n = length (p);
    p = reshape (p, 1, n);
  endif

  if (any (any (r <= -1)))
    error ("npv: all interest rates must be > -1");
  endif
  if (isscalar (r))
    d = 1 ./ (1 + r) .^ (0 : n);
  elseif (isvector (r) && (length (r) == n))
    d = [1, (1 ./ cumprod (reshape (1 + r, 1, n)))];
  else
    error ("npv: r must be a scalar or a vector of the same length as p");
  endif

  if (nargin == 3)
    if (! isscalar (i))
      error ("npv: I_0 must be a scalar");
    endif
  else
    i = 0;
  endif

  p = [i, p];
  v = sum (d .* p);

endfunction


function v = fv (r, n, p, l, m)

  if (nargin < 3 || nargin > 5)
    print_usage ();
  endif

  if (! (isscalar (r) && r > -1))
    error ("fv: r must be a scalar > -1");
  elseif (! (isscalar (n) && n > 0))
    error ("fv: n must be a positive scalar");
  elseif (! isscalar (p))
    error ("fv: p must be a scalar");
  endif

  if (r != 0)
    v = p * ((1 + r)^n - 1) / r;
  else
    v = p * n;
  endif

  if (nargin > 3)
    if (nargin == 5)
      if (! ischar (m))
        error ("fv: `method' must be a string");
      endif
    elseif ischar (l)
      m = l;
      l = 0;
    else
      m = "e";
    endif
    if strcmp (m, "b")
      v = v * (1 + r);
    endif
    if isscalar (l)
      v = v + fvl (r, n, l);
    else
      error ("fv: l must be a scalar");
    endif
  endif

endfunction

%%====================================
%% Engineering Economy Basic Formulas
%%====================================
%P/F
function p = f_p(i ,n)
   p = ((1 + i)^n );
endfunction

%F/P
function f = p_f(i ,n)
   f = ((1 + i)^-n );
endfunction

%F/A
function f = f_a(i ,n)
   v = ((1 + i)^n );
   f = (v - 1) / i;
endfunction

%A/F
function a = a_f(i ,n)
   a = 1 / f_a(i ,n);
endfunction


%P/A
function v = p_a(i ,n)
   v = ((1 + i)^n );
   v = (v - 1) / i;
   v = v / ((1 + i)^n );
endfunction

%A/P
function a = a_p(i ,n)
   a = 1 / p_a(i ,n);
endfunction


%============== end of library ==========

%========================================




Week#1

%%==================================== %% Engineering Economy Basic Formulas %%==================================== %P/F function p = f_p(i ,n) p = ((1 + i)^n ); endfunction %F/P function f = p_f(i ,n) f = ((1 + i)^-n ); endfunction %F/A function f = f_a(i ,n) v = ((1 + i)^n ); f = (v - 1) / i; endfunction %A/F function a = a_f(i ,n) a = 1 / f_a(i ,n); endfunction %P/A function v = p_a(i ,n) v = ((1 + i)^n ); v = (v - 1) / i; v = v / ((1 + i)^n ); endfunction %A/P function a = a_p(i ,n) a = 1 / p_a(i ,n); endfunction %============start of Examples=========== %=========Example#1====================== %%https://www.e-education.psu.edu/eme460/node/654 % A is a uniform series of equal payments at each compounding period; % P is a present single sum of money at the time zero; % F is a future sum of money at the end of period n. And i is the compound interest rate. % i is the compound interest rate % n is the period. disp("=========Example 1-1=========") %Assume you put 20,000 dollars (principal) in a bank for the interest rate of 4%. %How much money will the bank give you after 10 years? n = 10 i = 4/100 p = 20000 f = p * f_p(i ,n); printf("F = %10.2f\n",f) %disp("===================") disp("=========Example 1-2=========") % Assuming the discount rate of 10 %, present value of 100 dollars
% which will be received in 5 years from now can be calculated as: n = 5 i = 10/100 f = 100 p = f * p_f(i ,n); printf("P = %10.2f\n",p) %disp("===================") disp("=========Example 1-2-1=========") % Assuming the interest rate of 10 %, present value of 10,000 dollars % How many years from now it will be double in value: i = 10/100 p = 10000 for n = 1:10 f = p * f_p(i ,n); printf("n= %d : F = %10.2f\n",n,f) if f > (2*p) break endif endfor %disp("===================") disp("=========Example 1-3=========") n = 20 i = 6/100 a = 4000 f = a * f_a(i ,n); printf("F = %10.2f\n",f) %disp("===================") disp("=========Example 1-4=========") n = 20 i = 6/100 f = 200000 a = f * a_f(i ,n); printf("A = %10.2f\n",a) %disp("===================") disp("=========Example 1-5=========") n = 10 i = 12/100 a = 2000 p = a * p_a(i ,n); printf("P = %10.2f\n",p) %disp("===================") disp("=========Example 1-6=========") p = 25000 i =4/100 n = 5 A = p * a_p(i,n); printf("A = %10.2f\n",A) %disp("===================") disp("=========Example 1-7=========") i = 8/100 P = 1000 * p_f(i,1)+ 1500 * p_f(i,2)+ 1800 * p_f(i,3)+ 1200 * p_f(i,4)+ 2000 * p_f(i,5) disp("===================")