计算方法试验

一:实验目的

  1. 理解秦九韶多项式求值算法的思想
  2. 初步熟悉用C语言编程实现该算法的过程

二:实验内容

1:秦九韶多项式求值方法

用C语言编程实现秦九韶多项式求值方法,求解 在处的值。

(1):代码

#include<stdio.h>
#include<stdlib.h>


int main() {
    // 动态数组长度,需要输入,后续分配length个空间
    int length = 0;
    printf("请输入多项式系数个数:");

    // #1:a0, a1, a2, ..., an
    scanf_s("%d", &length);
    float *array = (float *) malloc(sizeof(float) * length);
    for (int i = 0; i < length; i++) {
        printf("请输入第%d项系数:", i + 1);
        scanf_s("%f", &array[i]);
    }

    // #2:x = 初值, y = an, i = n - 1
    float x;
    float y = array[length - 1];
    printf("请输入待求式x的值:");
    scanf_s("%f", &x);

    // #3:y = y * x + ai
    for (int i = length - 1; i > 0; i--) {
        y = y * x + array[i - 1];
    }

    // #4:输出y
    printf("最终结果为:%f", y);
    free(array);
    return 0;
}

(2):运行结果

image.png

2:二分法

利用二分法,求解方程。

(1):代码

#include "stdio.h"
#include "math.h"

#define f(x) (x*(x*x-1)-1)

int main() {
    int i = 0;
    double x, a = 1, b = 1.5, y = f(a);
    if (y * f(b) >= 0) {
        printf("\nThe range is error!");
        return 0;
    } else {
        do {
            x = (a + b) / 2;
            printf("x%d = %6.4f\n", i + 1, x);
            i++;
            if (f(x) == 0) {
                break;
            } else if (y * f(x) < 0) {
                b = x;
            } else {
                a = x;
            }
        } while (fabs(b - a) > 0.0005);
        printf("\nx=%4.2f", x);
    }
}

(2):运行结果

image.png

3:迭代法

(1):求方程的一个根

a:代码
#include "stdio.h"
#include "math.h"

int main() {
    double x0, x1 = 1;
    int i = 1;
    do {
        x0 = x1;
        x1 = log10(x0 + 2);
        printf("x%d = %6.4f\n", i, x1);
        i++;
    } while (fabs(x1 - x0) >= 0.0005);
    printf("x = %6.4f\n", x1);
}
b:运行结果

image.png

(2):求方程在附近的根

a:代码
#include "stdio.h"
#include "math.h"

int main() {
    double x0, x1 = 0.5;
    int i = 1;
    do {
        x0 = x1;
        x1 = exp(-x0);
        printf("x%d = %7.5f\n", i, x1);
        i++;
    } while (fabs(x1 - x0) > 0.0005);
    printf("x = %5.3f", x1);
}
b:运行结果

image.png

4:牛顿迭代法

(1):求方程在附近的根

a:代码
#include "stdio.h"
#include "math.h"

int main() {
    double x0, x1 = 0.5;
    int i = 0;
    printf("x%d = %7.5f\n", i, x1);
    i++;
    do {
        x0 = x1;
        x1 = x0 - (x0 - exp(-x0)) / (1 + exp(-x0));
        printf("x%d = %7.5f\n", i, x1);
        i++;
    } while (fabs(x1 - x0) >= 0.0005);
    printf("x = %5.3f", x1);
}
b:运行结果

image.png

(2):求方程在中的根的近似值, 精度要求为

a:代码
#include "stdio.h"
#include "math.h"

int main() {
    double x0, x1 = 4;
    int i = 0;
    printf("x%d = %5.3f\n", i, x1);
    i++;
    do {
        x0 = x1;
        x1 = x0 - ((((x0 - 2) * x0 - 4) * x0) - 7) / ((3 * x0 - 4) * x0 - 4);
        printf("x%d = %5.3f\n", i, x1);
        i++;
    } while (fabs(x1 - x0) >= 0.01);
    printf("x = %4.2f", x1);
}
b:运行结果

image.png

(3):计算的值

a:代码
#include "stdio.h"
#include "math.h"

int main() {
    double x0, x1 = 11;
    do {
        x0 = x1;
        x1 = x0 - (x0 * x0 - 115) / (2 * x0);
    } while (fabs(x1 - x0) >= 0.0005);
    printf("x = %5.2f", x1);
}
b:运行结果

image.png

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北溪入江流

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值