Looking for TVM contributions
|
06-18-2024, 06:15 PM
(This post was last modified: 06-18-2024 11:47 PM by Albert Chan.)
Post: #63
|
|||
|
|||
RE: Looking for TVM contributions
Hi, robve
I was looking at your test samples pdf. #23 just quit, returned NaN Fix is simple, have Secant's initial 2 points guaranteed one-sided convergence. Bonus: this allowed for simple no solution test, just like Newton's method. 1st point = edge (X = -PV or FV) --> edge rate = PMT/X I recently showed edge rate is really result of Newton's from *extreme* rates, -1 or ∞ Turns out, whatever edge X we pick, slope (from extreme) is -X ! 2nd point = PMT/X + f(PMT/X) / X // not as good as Newton on PMT/X, but good enough Just as Newton's final half-correction, we do the same for Secants. 10 INPUT "B,N,P,M,F? ";B,N,P,M,F 20 IF B THEN P=P+M @ F=F-M ! end mode 30 IF ABS(P)>ABS(F) THEN X=F @ F=-P @ P=-X @ N=-N ! time reversed 40 DEF FNF(I) @ IF N*I*I+1-1 THEN FNF=((P+F)/EXPM1(LOGP1(I)*N)+P)*I+M @ END 50 Z=P+F @ FNF=(Z+N*M+I*((Z+N*(P-F))/2+I*Z*(N*N-1)/12))/N @ END DEF 60 X=F @ IF M/X<=-1 THEN X=-P ! guess = edge X 70 I0=M/X @ Y0=FNF(I0) @ I=I0+Y0/X @ Y=FNF(I) @ DISP 100*I0,Y0 80 DISP 100*I,Y @ Z=I-I0 @ I0=I @ I=I-Y*Z/(Y-Y0) @ Y0=Y @ Y=FNF(I) 90 IF SGN(Y)=SGN(Y0) AND ABS(Y)<ABS(Y0) THEN 80 100 DISP 100*I,Y @ IF Y-Y0 THEN J=I-Y*(I-I0)/(Y-Y0)/2 @ DISP 100*J Code use small edge (LINE 60), but we can use the other, as long as edge rate is valid. >run ! sample #23 B,N,P,M,F? 0,10,50,-30,80 -37.5 .4474481846 -36.9406897692 .0348105026 -36.8935058326 .0000999938 -36.8933699054 .0000000229 -36.8933698743 .0000000002 -36.893369874 0 -36.893369874 >X=-P @ run 70 ! small P, big edge 60 .7159170954 58.5681658092 .0493058621 58.4622602744 .0001415644 58.4619553284 .0000000289 58.4619552661 -.0000000001 58.4619552662 Although -X slope proof assumed positive N, time-symmetry make it valid for negative N too Example, if we swap (P, F), code will do time-reversal to keep size of P small. >run B,N,P,M,F? 0,10,80,-30,50 37.5 2.1052759718 34.8684050352 .291631744 34.4452478265 .0037390629 34.4397519889 .0000071623 34.4397414412 0 34.4397414412 >X=-P @ run 70 ! small P, big edge -60 .0081797505 -59.983640499 .0000312799 -59.983577699 .0000000002 -59.9835776986 0 -59.9835776986 Lets test tiny rate branch (LINE 50) >run ! sample #21 B,N,P,M,F? 0,480,100000,-208.333333,0 .208333333 121.444894064 .086888438936 46.544537395 1.14202213657E-2 5.774169845 7.319118817E-4 .366932959 6.607422444E-6 .003310945 3.19310587E-9 1.93321242436E-6 -6.652600197E-10 1.03444729167E-11 -6.65280665633E-10 -0 -6.65280665633E-10 What about no solution case? Y get worse no sign change, and I not converging. B,N,P,M,F? 0,10,1000,-287,2000 -14.35 116.137711015 -8.54311444925 61.534445887 -1.99911961932 20.995382798 1.39004791475 8.609614417 3.74593127745 3.290957549 5.20365121116 1.292347407 6.14624653622 .507826981 6.75639679154 .207532994 7.17807126641 .093719606 7.52529900771 .056815067 8.05986109467 .099136777 6.80767250381 Update: I made a stupid mistake, using X for everything. Now X refer only to edge, and I use Z for temp variable. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)