You are on page 1of 5

# 數值分析

Secant Method

#include <stdio.h>
#include <math.h>

double f(double x) //說明(1)
{
return (-x*x*x)+x+1;
}

double SecantMethod(double xn_1, double xn, double e, int m) //說明(2)
{
int n;
double d;
for (n = 1; n <= m; n++) //說明(3)
{
d = (xn - xn_1) / (f(xn) - f(xn_1)) * f(xn); //說明(4)
if (fabs(d) < e)
return xn;
xn_1 = xn;
xn = xn - d;
printf("\n %15.15f, %15.15f ", xn,f(xn));
}
return xn;
}

int main(void) //說明(5)
{
double xm_1,xm,E;
int M;
printf("\n Enter 1st initial guess x0:"); //說明(6)
scanf("%lf", &xm_1);

printf(" Enter 2nd initial guess x1:");
scanf("%lf", &xm);

printf(" Enter Max iteration number:"); //說明(7)
scanf("%d", &M);

printf(" Enter tolerance:"); //說明(8)
scanf("%lf", &E);
printf("\n\n %15.15f \n", SecantMethod(xm_1, xm, E, M)); //說明(9)
return 0;
}

Secant Method 解法方程式如下圖(圖 1)所式：

(圖 1)
Secant Method 持續執行直到以下兩個條件其中之一發生：
1. | xn + 1 − xn | < e
2. n > m

[ 情況一 ：改變 x0,x1 的相隔區間，固定最大疊代數為 50 及最小容忍值為 0.00001 ]

(1) x0：1.1 x1：1.2

Enter 1st initial guess x0:1.1
Enter 2nd initial guess x1:1.2
Enter Max iteration number:50
Enter tolerance:0.00001

1.358922558922559, -0.150559671163600
1.320488768042503, 0.017964933592940
1.324585858208551, 0.000563284561564
1.324718479412076, -0.000002226853110

1.324718479412076

(2) x0：1.1 x1：50

Enter 1st initial guess x0:1.1
Enter 2nd initial guess x1:50
Enter Max iteration number:50
Enter tolerance:0.00001

1.100300953737658, 0.768208192751298
1.100601595747396, 0.767416608639056
1.392064837568101, -0.305540368252373
1.309066368705039, 0.065778557861840
1.323769415607835, 0.004041607148503
1.324731950018435, -0.000059674822552
1.324717944868202, 0.000000052781416

1.324717944868202

(3) x0：1.001 x1：1.002

Enter 1st initial guess x0:1.001
Enter 2nd initial guess x1:1.002
Enter Max iteration number:50
Enter tolerance:0.00001

1.497761334828597, -0.862150216342966
1.267734989017586, 0.230292162931523
1.316225660665854, 0.035930528277030
1.325189854921204, -0.002013355502200
1.324714202246012, 0.000016013635477

1.324714202246012

[ 情況二 ：改變最小容忍值，固定最大疊代數為 50 及 x0,x1 的值各別為 1.1,1.2 ]
(4) 容忍值：0.01

Enter 1st initial guess x0:1.1
Enter 2nd initial guess x1:1.2
Enter Max iteration number:50
Enter tolerance:0.01

1.358922558922559, -0.150559671163600
1.320488768042503, 0.017964933592940

1.320488768042503

(5) 容忍值：0.000002

Enter 1st initial guess x0:1.1
Enter 2nd initial guess x1:1.2
Enter Max iteration number:50
Enter tolerance:0.000002

1.358922558922559, -0.150559671163600
1.320488768042503, 0.017964933592940
1.324585858208551, 0.000563284561564
1.324718479412076, -0.000002226853110

1.324718479412076

(5) 容忍值：0.00000000000001

Enter 1st initial guess x0:1.1
Enter 2nd initial guess x1:1.2
Enter Max iteration number:50
Enter tolerance:0.00000000000001

1.358922558922559, -0.150559671163600
1.320488768042503, 0.017964933592940
1.324585858208551, 0.000563284561564
1.324718479412076, -0.000002226853110
1.324717957180461, 0.000000000274153
1.324717957244746, -0.000000000000000

1.324717957244746

～完～感謝閱讀～