C語言例題2-9 利用定點迴路法Fixed point method 求 x^4-3x^2 - 3 = 0
x^4= 3x^2+3
x = pow ( (3x^2+3) , (1/4))
f(x)=x , g(x)= pow ( (3x^2+3) , (1/4))
程式
#include <stdio.h>
#include <math.h>
double f(double x)
{
return x*x*x*x-3*x*x-3; //change equation for each problem
}
double g(double x)
{
return pow((3*x*x+3),.25); //change equation for each problem
}
int main()
{
double p, p0, Tol;
int i=1;
int No;
printf("Enter approximate p: ");
scanf ("%lf", &p0);
printf("Desired Tolerance: ");
scanf ("%lf", &Tol);
printf("Maximum Iterations: ");
scanf ("%d", &No);
printf("\n\n");
while (i<=No)
{
p = g(p0);
if((fabs(p-p0))<Tol)
{
//printf("%lf", &p);
break;
}
printf("Iteration %d: Current value = %lf , the root value = %lf\n", i, p , f(p));
i++; //i=i+1
p0=p;
if (i>No)
{
printf("Method Failed after %d", No);
printf(" iterations");
}
}
}
STDIN輸入資料
1
0.0001
20
輸出畫面
Enter approximate p: Desired Tolerance: Maximum Iterations:
Iteration 1: Current value = 1.565085 , the root value = -4.348469
Iteration 2: Current value = 1.793573 , the root value = -2.302242
Iteration 3: Current value = 1.885944 , the root value = -1.019640
Iteration 4: Current value = 1.922848 , the root value = -0.421680
Iteration 5: Current value = 1.937508 , the root value = -0.169775
Iteration 6: Current value = 1.943317 , the root value = -0.067636
Iteration 7: Current value = 1.945617 , the root value = -0.026833
Iteration 8: Current value = 1.946527 , the root value = -0.010628
Iteration 9: Current value = 1.946887 , the root value = -0.004207
Iteration 10: Current value = 1.947030 , the root value = -0.001665
訂閱:
張貼留言 (Atom)
Messaging API作為替代方案
LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言