小靈通不得不學(xué)好之大二下篇課件functionhandle_第1頁
小靈通不得不學(xué)好之大二下篇課件functionhandle_第2頁
小靈通不得不學(xué)好之大二下篇課件functionhandle_第3頁
小靈通不得不學(xué)好之大二下篇課件functionhandle_第4頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、1A real example: Insuranceo Alex is offered by an Insurance company a certain product Two types of (annual) payment options: Once a year at 1905.6 for 15 times Once a year at 1578.0 for 20 timeso Q1: Which option should Alex take? If Alex were to pay itpayment, how much do you think is a fair price?

2、 This leads to another question.o Q2: Whats the implied interest rate?3Root of EquationsFruit Comes From Deep Roots - Rick Warren22Mortgage(房貸): Monthly Payment(月供金額)o 向貸L元,分Y年還清,年利率為I%,月供?o r=I/12/100: 每月利率(以小數(shù)形式,例如I=12%則月利率為1%,即r=0.01)o Y年=Y12,令n=Y*12o 等效原理 L存進(jìn),每月獲得利息,利息也轉(zhuǎn)存, Y年本金加利率總和為 1 r 月供為M元,相

3、等于每月存M元,每月也產(chǎn)生利息 M 1 r 1 r 1 1 M 1r M , 所以月供M為 5A real example: Insurancep: The present value of the producto 1905.6 *(1+r)14+(1+r)13+(1+r)1+1=p*(1+r)14o 1578*(1+r)19+(1+r)18+(1+r)1+1=p*(1+r)19o In general, if we pay $a each year for t times, then a*(1+r)t-1 +(1+r) t-2+1=p*(1+r) t-1 a1*(1+r)t1-1 +(1+

4、r) t1-2+1=p*(1+r) t1-1 a2*(1+r)t2-1 +(1+r) t2-2+1=p*(1+r) t2-1 a1a2 and a1*t1a2*t2 if t10 使得f r Eg,Y=10年,L=100萬,月供M=1萬,年利率為多少? 求出r,然后I=12*r100%64Methods for Finding the Rootso Graphicalo DirectSearch Methodo Some systematic methods9In general, how to find a root?85Graphical Methodo Plot the function

5、 and visually determine where it crosses the x axis.o Example: f(x)=exx X=linspace(0,10); Y=exp(X)X; Z=zeros(size(X); plot(X,Y); hold on; plot(X,Z); grid on;o Pros: Simple Useful for obtaining rough estimateso Cons: Lack of precision11Graphical Methodo Plot the function and visually determine where

6、it crosses the x axis.o Example: f(x)=exx X=linspace(0,10); Y=exp(X)X; Z=zeros(size(X); plot(X,Y); hold on; plot(X,Z); grid on;o (Go to code of graphical method example)106Direct-Search Methodo Initially, specify an interval for x within which the root is assumed to occur.o Divide the interval into

7、smaller, uniformly spaced subintervals. The size of these subintervals will be dictated by the required precision for the estimate of the root. For example, if the root must be estimated to within 0.001, then the subintervals for the root will be of length 0.002 Sequential look at each sub-intervals

8、 Evaluate f at both ends. Stop if with different signs, and return the left and right end point of the sub-interval as well as the mid point (as the approximate root)13Direct-Search Methodo Trial and Error f(x) is evaluated at many points over some range of x until f(x)=0 If we are very lucky to gue

9、ss the exact root of the function, this works. But it is pointless to search if we could guess the exact one Even if we have some prior knowledge, say, we know that a=x=b, there are still an infinite number of trial values to be considered. Fortunately, most problem in applications do not require an

10、 exact value for the root An estimate (within pre-specified precision) is often good enough.127Direct-Search Method: Exampleo f(x)=e-x-xo f(0)=1; f(1)=1/e-10; So there is a root in 0,1o If subinterval size=0.1 n=ceil(1-0)/0.1)+1=11;o f(0.5)*f(0.6) 0, there is a rooto How efficient is this?o If we ne

11、ed more precision, say 0.01?(Go to code with direct search example)15Gridf010.10.80480.20.61870.30.44080.40.27030.50.10650.6-0.05120.7-0.20340.8-0.35070.9-0.49341-0.6321Direct-Search Method: Algorithm%A root is assumed to exist within a, b.%pre is the required precision (max length of the sub-interv

12、al) pre=0.001;n=ceil(b-a)/pre)+1;Grid=linspace(a, b, n); %Get the discretization Found=false; root=; k=1; left=Grid(k); right=Grid(k+1); f_left=f(left); f_right=f(right); k=k+1;if(f_left*f_right=0)Found=true; root=(left+right)/2; disp(root=); disp(root)endwhile(Found & kn) %Totally n-1 subinterval t

13、o search left=right; right=Grid(k+1); f_left=f_right; f_right=f(right); if(f_left*f_right=0)Found=true; root=(left+right)/2; disp(root=); disp(root)end k=k+1;end if(Found)disp(No root found!)end148Any better idea?17About the Direct Searcho For a high degree of precision, the direct search may be tim

14、e consuming. To check a large number of subintervals of very small size.169Bisection Method: Algorithmo Given a continuous function f(x), and if we know that at two points a and b, f(a)*f(b)=0, then we could (approximately) get the root of f in this way: step 0) left=a; right=b; step 1) mid=(left+ri

15、ght)/2; step 2) If f(left)*f(mid)=0, then set right=mid; otherwise set left=mid; step 3) Back to step 1) until we get the desired accuracy.19Bisection methodo It works when f is a continuous function and it requires previous knowledge of two initial guesses, a and b, such that f(a) and f(b) have opp

16、osite signs.o Suppose you know that f(a)*f(b)0 and f is continuous, then you know that there is an x in a, b such that f(x)=0.o We can cut it into two sub-intervals, and determine which half contains a root.o Question: how to know a and b in advance?1810A More detailed description: algorithmFound=fa

17、lse; left=a; right=b; gap=right-left; while(Found=false)mid=(left+right)/2;if (gap=pre_interval) | (abs(f(mid)=pre_f) rt=mid; f_rt=f(mid); Found=true;elseif f(left)*f(mid)=0 right=midelseleft=mid; endend21A More detailed description: requiremento Input: a, b, pre_interval and pre_f, and f(a)*f(b) is

18、 required to be 0. pre_interval is the desired interval precision while pre_f is the desired function precision.o Output: rt and f_rt. rt is the approximated root, and f_rt is f(rt). Ideally f_rt should be close to zero.o Termination: it stops if the subintervals length is = pre_interval or if a poi

19、nt rt is found such that|f(rt)|=pre_f2011One subtle issueo f is a function. How to input f to the BiSection function?o One way: Write one different f in f.m every time.o E.g, in the same m file:function rt, f_rt=BiSection (a,b,pre_interval, pre_f)%this is in BiSection m return;function F=f(x)%this i

20、s in f m F=exp(-x)-x; return;o Whats wrong with that?o Any alternative?23Illustration: f(x)=exp(-x)-xo f(0)=1, and f(1)=1/e-10. So, a root in mid, right. New interval: 0.5,1, new left=0.5o mid=(0.5+1)/2=0.75, f(mid)=f(0.75)=-0.2776.f(left)*f(mid)=f(0.5)*f(0.75)=0, a root in 0.5, 0.75. So let right=0

21、.75o mid=(0.5+0.75)/2=0.625. f(mid)=f(0.625)=-0.0897o f(left)* f(mid)=f(0.5)*f(0.625)=0, a root in 0.5, 0.625o let right=0.625, and mid=0.5625, f(mid)=0.0073pre_f, stop.o The bestcould get is 0.567143290409784 (we could try it )2212More about Function handleo A function handle is “function_name”o function_name could be the name of a builtin function or a userdefined f

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論