This chapter mainly introduces the two-and three-point strategies we use in numerical analysis.
First, two points are introduced.
In fact, the idea of two points is very good understanding and the author in a number of previous articles have also been infiltrated, for two functions even the unit higher function 0 point solution, line segment tree and "algorithm puzzle" in the "cutting steel bar" problem, are based on the two-point idea.
In the following, we apply the two-dimensional numerical analysis strategy through the specific problem.
EX1: Mortgage Loan
After borrowing N yuan at the annual interest rate of p%, in M months, we repay the loan in a monthly C-dollar way. The loan balance is calculated as follows in the term of the loan.
(1) The loan balance starts from the balance N yuan.
(2) in the United States one months, the loan balance will increase the interest of one months, the monthly interest rate is (P/12)%.
(3) After the interest is increased, the amount of the current month will be deducted from the loan balance.
Analysis: We build a mathematical model from a more abstract point of view, this problem m,p,n,c input data equivalent to an independent variable, and the repayment process is equivalent to a function mechanism, m months after the loan balance is the dependent variable.
For these three elements of the function, we all know, then we can transform the problem into the ≤0 of the dependent variable to solve the minimum value of C.
If we want to try to use mathematical methods, we will find that although we understand the function mechanism, but it is difficult to list its expression, so here we are based on the solution interval of the independent variable C, a binary search, and this process is based on the function mechanism and the dependent variable (function balance) range constraints.
The simple reference code is as follows:
#include <cstdio>using namespacestd;DoubleBalanceDoubleAmount,intDuration,DoubleRates,DoubleMonthlypayment)//solve the monthly repayment monthlypayment yuan, duration months after the loan balance. { DoubleBalance = amount;//Loan Balance Initialization for(inti =0; I < duration;i++) {Balance*= (1.0+ (rates/12.0)/100.0);//This involves a simple repayment mechanism, which we understand to be repaid at the end of each month.Balance-=monthlypayment; } returnbalance;} DoublePaymentDoubleAmount,intDuration,Doublerates) { DoubleL =0;//The interval of the monthly repayment feasible solution, based on the special situation of this problem (the interval right end point is feasible, which corresponds to the variable of the last return of this function), the solution interval distribution is (,] DoubleR = amount* (1.0+ (rates/12.0)/100.0); Doublemid; for(inti =0; I < -; i++) {Mid= (R + L)/2.0; if(Balance (amount, duration, rates, mid) <=0) R=mid; ElseL=mid; } returnR;}intMain () {Doubleamount, rates; intduration; printf ("Input amount,rates and Duration:"); scanf ("%lf%lf%d",&amount,&rates,&duration); printf ("The lowest monthlypayment:%LLF", Payment (amount,duration,rates));}
-chaper13-Numerical analysis of the practical strategy of algorithm problem