鱼C论坛

 找回密码
 立即注册
查看: 1615|回复: 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 的值后,多添加一句:
        printf("%.4f\n" , x1 - cos(x1)) ;  // 看看输出是不是 0 值就可以了。
        这是我写的代码
#include <stdio.h>
#include <math.h>

#define PI 3.1415

double f(double x)
{
        return x + (cos(x) - x) / (sin(x) + 1) ;
}

int main(void)
{
        int t = 0 , k = 100 , n = 0 ;
        double x0 = PI / 4 , x1     ;
        while(n < k) {
                x1 = f(x0)          ;
                if(fabs(x0 - x1) < 1e-14) {
                        t = 1       ;
                        break       ; 
                } else {
                        x0 = x1     ;
                        n = n + 1   ;
                }
        }
        if(t == 1) printf("\nFengcheng geng is %10.5f\n" , x1) ;
        else printf("\nSorry,not found!\n")                    ;
}
       编译运行实况:
D:\[00.Exerciese.2022]\C>g++ -o x x.c

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

Fengcheng geng is    0.73909

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

使用道具 举报

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

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

#define PI 3.1415

double f(double x)
{
        return x + (cos(x) - x) / (sin(x) + 1) ;
}

int main(void)
{
        int t = 0 , k = 100 , n = 0 ;
        double x0 = PI / 4 , x1     ;
        while(n < k) {
                x1 = f(x0)          ;
                if(fabs(x0 - x1) < 1e-14) {
                        t = 1       ;
                        break       ; 
                } else {
                        x0 = x1     ;
                        n = n + 1   ;
                }
        }
        if(t == 1) printf("\nFengcheng geng is %10.5f\n" , x1) ;
        else printf("\nSorry,not found!\n")                    ;
}
       编译运行实况:
D:\[00.Exerciese.2022]\C>g++ -o x x.c

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

Fengcheng geng is    0.73909

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-20 18:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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