鱼C论坛

 找回密码
 立即注册
查看: 1439|回复: 1

[已解决]牛顿法求解

[复制链接]
发表于 2022-11-23 15:41:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
[code]#include<stdio.h>
#include<math.h>
#define PI 3.1415
double f(double x0)
{
        double x1;
        x1=(cos(x0)-x0)/(sin(x0)+1);
        x1=x1+x0;
        return x1;
}
int main()
{
        int t=0,k=100,n=0;
        double x0=PI/4,x1;
        while(n<k)
        {
                x1=f(x0);
                if(fabs(x0-x1)<0.001)
                {
                        t=1;
                        break;
                }
                else
                {
                        x0=x1;
                        n=n+1;
                }
                if(t==1)
                        printf("%10.5f",x1);
                else
                        printf("not found");
                return 0;
        }
}
这个答案是不是有问题啊,中间符号都不对
最佳答案
2022-11-23 17:11:31
本帖最后由 jackz007 于 2022-11-23 17:36 编辑

        验证计算结果是否正确的方法很简单,在输出 x1 的值后,多添加一句:
  1.         printf("%.4f\n" , x1 - cos(x1)) ;  // 看看输出是不是 0 值就可以了。
复制代码

        这是我写的代码
  1. #include <stdio.h>
  2. #include <math.h>

  3. #define PI 3.1415

  4. double f(double x)
  5. {
  6.         return x + (cos(x) - x) / (sin(x) + 1) ;
  7. }

  8. int main(void)
  9. {
  10.         int t = 0 , k = 100 , n = 0 ;
  11.         double x0 = PI / 4 , x1     ;
  12.         while(n < k) {
  13.                 x1 = f(x0)          ;
  14.                 if(fabs(x0 - x1) < 1e-14) {
  15.                         t = 1       ;
  16.                         break       ;
  17.                 } else {
  18.                         x0 = x1     ;
  19.                         n = n + 1   ;
  20.                 }
  21.         }
  22.         if(t == 1) printf("\nFengcheng geng is %10.5f\n" , x1) ;
  23.         else printf("\nSorry,not found!\n")                    ;
  24. }
复制代码

       编译运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x

  3. Fengcheng geng is    0.73909

  4. D:\[00.Exerciese.2022]\C>
复制代码
QQ图片20221123154103.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-11-23 17:11:31 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-11-23 17:36 编辑

        验证计算结果是否正确的方法很简单,在输出 x1 的值后,多添加一句:
  1.         printf("%.4f\n" , x1 - cos(x1)) ;  // 看看输出是不是 0 值就可以了。
复制代码

        这是我写的代码
  1. #include <stdio.h>
  2. #include <math.h>

  3. #define PI 3.1415

  4. double f(double x)
  5. {
  6.         return x + (cos(x) - x) / (sin(x) + 1) ;
  7. }

  8. int main(void)
  9. {
  10.         int t = 0 , k = 100 , n = 0 ;
  11.         double x0 = PI / 4 , x1     ;
  12.         while(n < k) {
  13.                 x1 = f(x0)          ;
  14.                 if(fabs(x0 - x1) < 1e-14) {
  15.                         t = 1       ;
  16.                         break       ;
  17.                 } else {
  18.                         x0 = x1     ;
  19.                         n = n + 1   ;
  20.                 }
  21.         }
  22.         if(t == 1) printf("\nFengcheng geng is %10.5f\n" , x1) ;
  23.         else printf("\nSorry,not found!\n")                    ;
  24. }
复制代码

       编译运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x

  3. Fengcheng geng is    0.73909

  4. D:\[00.Exerciese.2022]\C>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-16 02:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表