目录

C++数值逼近-迭代法Aikten法以及牛顿法求解线性方程根通用程序

目录

数值逼近

公元 1225 年,比萨的数学家 Leonardo(即 Fibonacci(斐波那契)),1170-1250)研究了方程

x^3+2*x^2+10*x-20=0

得到一个根 x* = 1.368 808 107,没有人知道他用什么方法得到这个值。对于这个方程,分别用下列方法: (1)迭代法式1 ; (2)迭代法式2 ; (3)对(1)的 Aitken 加速方法; (4)对(2)的 Aitken 加速方法; (5)Newton 法 。 求方程的根(可取 x0 = 1 ),计算到 Leonardo 所得到的准确度。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<stdio.h>
#include<math.h>
//显示中文
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>//用于函数SetConsoleOutputCP(65001);更改cmd编码为utf8
#include<iostream>
using namespace std;

#define phi01(x)   20/((x)*(x)+2*(x)+10)//迭代函数

#define phi02(x) (20-2*((x)*(x))-((x)*(x)*(x)))/10

#define f(x)   ((x)*(x)*(x))+2*((x)*(x))+10*(x)-20

#define f1(x)  3*((x)*(x))+4*(x)+10

void test01()
{
    int n=1,N;
    double x,x0,del;
    printf("x0 = 1");
    //scanf("%f",&x0);
    x0 = 1;
    printf("\ndel = 0.000000001");
    //scanf("%f",&del);
    del = 0.000000001;
    printf("\nN = 100");
    //scanf("%d",&N);
    N = 100;
    printf("\n k     x(k)");
    printf("\n  %2d  %f",0,x0);
    while (n<N)
    {
        x = phi01(x0);
        if(fabs(x-x0)<del)
        {
            printf("\n\n 迭代法式1的近似解 = %.9lf \n",x);
            return;
        }
        printf("\n %2d   %.9lf",n,x0);
        n = n+1;
        x0 = x;
    }
    printf("\n\n%d次迭代后未达到精度要求.\n",N);
}

void test02()
{
    int n=1,N;
    double x,x0,del;
    printf("x0 = 1");
    //scanf("%f",&x0);
    x0 = 1;
    printf("\ndel = 0.000000001");
    //scanf("%f",&del);
    del = 0.000000001;
    printf("\nN = 20");
    //scanf("%d",&N);
    N = 20;
    printf("\n k     x(k)");
    printf("\n  %2d  %f",0,x0);
    while (n<N)
    {
        x = phi02(x0);
        if(fabs(x-x0)<del)
        {
            printf("\n\n 迭代法式2的近似解 = %.9lf \n",x);
            return;
        }
        printf("\n %2d   %.9lf",n,x0);
        n = n+1;
        x0 = x;
    }
    printf("\n\n%d次迭代后未达到精度要求.\n",N);
}

void Aitken01()
{
    int n=1,N;
    double x0,x1,x2,x3,del;
    printf("x0 = 1");
    //scanf("%f",&x0);
    x0 = 1;
    printf("\ndel = 0.000000001");
    //scanf("%f",&del);
    del = 0.000000001;
    printf("\nN = 100");
    //scanf("%d",&N);
    N = 100;
    printf("\n k     x(k)");
    printf("\n  %2d  %f",0,x0);
    while (n<N)
    {
        x1 = phi01(x0);
        x2 = phi01(x1);
        x3 = x2-(((x2-x1)*(x2-x1))/(x2-2*x1+x0));
        if(fabs(x3-x0)<del)
        {
            printf("\n\n 迭代法式1的Aitken近似解 = %.9lf \n",x3);
            return;
        }
        printf("\n n =   %2d  x1 =  %.9lf",n,x3);
        n = n+1;
        x0 = x3;
    }
    printf("\n\n%d次迭代后未达到精度要求.\n",N);
}

void Aitken02()
{
    int n=1,N;
    double x0,x1,x2,x3,del;
    printf("x0 = 1");
    //scanf("%f",&x0);
    x0 = 1;
    printf("\ndel = 0.000000001");
    //scanf("%f",&del);
    del = 0.000000001;
    printf("\nN = 100");
    //scanf("%d",&N);
    N = 100;
    printf("\n k     x(k)");
    printf("\n  %2d  %f",0,x0);
    while (n<N)
    {
        x1 = phi02(x0);
        x2 = phi02(x1);
        x3 = x2-(((x2-x1)*(x2-x1))/(x2-2*x1+x0));
        if(fabs(x3-x0)<del)
        {
            printf("\n\n 迭代法式2的Aitken近似解 = %.9lf \n",x3);
            return;
        }
        printf("\n n =   %2d  x1 =  %.9lf",n,x3);
        n = n+1;
        x0 = x3;
    }
    printf("\n\n%d次迭代后未达到精度要求.\n",N);
}

void Newton()
{
    int n=1,N;
    double x,x0,del;
    printf("x0 = 1");
    //scanf("%f",&x0);
    x0 = 1;
    printf("\ndel = 0.000000001");
    //scanf("%f",&del);
    del = 0.000000001;
    printf("\nN = 100");
    //scanf("%d",&N);
    N = 100;
    printf("\n k     x(k)");
    printf("\n  %2d  %f",0,x0);
    while (n<N)
    {
        float f_1 = f(x0);
        float f_2 = f1(x0);
        float d = f_1/f_2;
        x =x0 - d;
        if(fabs(x-x0)<del)
        {
            printf("\n\n Newton迭代法的近似解 = %.9lf \n",x);
            return;
        }
        printf("\n %2d   %.9lf",n,x);
        n = n+1;
        x0 = x;
    }
    printf("\n\n%d次迭代后未达到精度要求.\n",N);
}

int main()
{
    //显示中文
    SetConsoleOutputCP(65001);

    test01();
    cout<<endl;
    
    test02();
    cout<<endl;

    Aitken01();
    cout<<endl;

    Aitken02();
    cout<<endl;

    Newton();
    cout<<endl;

    system("pause");
    return 0;
}