// 4.13.js // created on 2024-09-06 // $Id: 4.13.js 1.1 2024/11/02 08:42:59 s Exp $ 'use strict' const math = require("./math.js"); // see below. //# input const s_percent = [7.67, 8.27, 8.81, 9.31, 9.75, 10.16, 10.52, 10.85]; const x = [ 500, 900, 600, 500, 100, 100, 100, 50]; const p1 = 65.95; const d1 = 7.07; const p2 = 101.66; const d2 = 3.80; // calculation const s = s_percent.map(x => x / 100); const n = x.length; const t = x.map((_, i) => i + 1); const a = x.map((_, i) => (1 + s[i]) ** t[i]); const df = a.map((x) => 1 / x); const a2 = s.map((_, i) => (1 + s[i]) ** (t[i] + 1)); const df2 = a2.map((x) => 1 / x); const dfx = df.map((_, i) => df[i] * x[i]); const txdf2 = t.map((_, i) => t[i] * x[i] * df2[i]); const sumtxdf2 = txdf2.reduce((x, y) => x + y); const pv = dfx.reduce((x, y) => x + y); const duration = sumtxdf2 / pv; const A = [[p1, p2], [ p1 * d1, p2 * d2]]; const b = [pv, pv * duration]; // x1, x2 = A \ b ;const [x1, x2] = math.lusolve(A, b); // see below. const xx = math.lusolve(A, b); // see below. // output console.log("4.13.js"); console.log('t'.padStart(7), 's'.padStart(7), 'df'.padStart(7), 'x'.padStart(7), 'dfx'.padStart(7), 'txdf2'.padStart(7)); console.log("-".repeat(7*6+5)); for(let i = 0; i < n; i++){ console.log(t[i].toFixed(0).padStart(7), s[i].toFixed(3).padStart(7), df[i].toFixed(3).padStart(7), x[i].toFixed(0).padStart(7), dfx[i].toFixed(0).padStart(7), txdf2[i].toFixed(0).padStart(7)); }; console.log("-".repeat(7*6+5)); console.log('PV= ', Math.round(pv)); console.log('sumtxdf2=', Math.round(sumtxdf2)); console.log('D=', duration.toFixed(2)); console.log('p1= ', p1); console.log('p2= ', p2); console.log('d1= ', d1); console.log('d2= ', d2); console.log("Ax = b"); console.log("A "); console.log(A[0]); console.log(A[1]); console.log("b "); console.log(b) console.log("x") console.log(xx.flat()); // end // references // David Flanagan著,村上列(訳)『JavaScript 第7版』オライリー・ジャパン,2021. // ** : p.80. // => : p.202. // console.log() : p.630. // const : p.57. // length : p.176. // let : p.57. // map() : p.182. // padStart() : p.39. // reduce() : p.184. // repeat() : p.39. // toFixed() : p.51. // use strict : p.134. // // math.js : https://mathjs.org/ // Math.js is an extensive math library for JavaScript and Node.js. // lusolve() : Solves the linear system A * x = b // where A is an [n x n] matrix and b is a [n] column vector. // eof