例子 1:
use the bisection method to find p_3 for f(x)=x^3+4x^2-10 on [1,2].
首先說明其有根:
因為函數 f 在 [1,2] 連續,且 f(1)f(2)<0,由 IVT 可知函數 f 在 [1,2] 中有根。
接者尋找 p_3 :
1. a_1=1, b_1=2, 則 p_1=\frac{1}{2}(1+2)=\frac{3}{2}.
2. a_2=1, b_2=p_1=\frac{3}{2}, 則 p_2=\frac{5}{4}.
3. a_3=p_2=\frac{5}{4}, b_3=\frac{3}{2}, 則 p_3=\frac{1}{2}(\frac{5}{4}+\frac{3}{2}).
use the bisection method to find p_3 for f(x)=x^3+4x^2-10 on [1,2].
首先說明其有根:
因為函數 f 在 [1,2] 連續,且 f(1)f(2)<0,由 IVT 可知函數 f 在 [1,2] 中有根。
接者尋找 p_3 :
1. a_1=1, b_1=2, 則 p_1=\frac{1}{2}(1+2)=\frac{3}{2}.
2. a_2=1, b_2=p_1=\frac{3}{2}, 則 p_2=\frac{5}{4}.
3. a_3=p_2=\frac{5}{4}, b_3=\frac{3}{2}, 則 p_3=\frac{1}{2}(\frac{5}{4}+\frac{3}{2}).
using c language:
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h> /**/
- float f(float u);
- int sig(float v);
- int main(void)
- {
- int i=0,N=10000;
- float a=1.0,b=2.0,p=1.5,FA=0.0,FP=0.0;
- float TOL=0.0000001;
- printf("n an bn pn f(pn)\n");
- /*Step1*/
- i=1;
- FA=f(a);
- /*Step2*/
- while(i<=N)
- { /*Step3*/
- p=a+(b-a)/2;
- FP=f(p);
- printf("%2d %.9f %.9f %.9f %+.9f\n",i,a,b,p,f(p));
- /*Step4*/
- if(FP==0 || (b-a)/2<TOL)
- {printf("n=%d, p=%+f\n",i,p);break;}
- /*Step5*/
- i=i+1;
- /*Step6*/
- if(sig(FA)*sig(FP)>0){a=p;FA=FP;}
- else b=p;
- }
- printf("method failed after N=%d iterations.",N);
- system("PAUSE");
- return 0;
- }
- float f(float u)
- {
- return u*u*u+4*u*u-10;
- }
- int sig(float u)
- {
- if(u>0){return 1;}
- else if(u<0){return -1;}
- else 0;
- }
其執行結果:
當 n=3, 則 p_3=1.375.
沒有留言:
張貼留言