有戏

 找回密码
 立即注册
简历下载
搜索
热搜: 活动 交友 discuz
查看: 677|回复: 0

6. 用牛顿迭代法求根号n :x2 = (x1 + n/x1) / 2 ; 运算到前后两个x2,x1 的差的绝...

[复制链接]

1000

主题

1002

帖子

25万

积分

论坛元老

Rank: 8Rank: 8

积分
251951
发表于 2015-1-17 14:22:27 | 显示全部楼层 |阅读模式
6. 用牛顿迭代法求根号n :x2 = (x1 + n/x1) / 2 ; 运算到前后两个x2,x1 的差的绝对值小雨1e-6为止。


---------------------

#include <stdio.h>
#include <math.h>
#define JD 1e-6
/*用牛顿迭代法求根号n :x2 = (x1 + n/x1) / 2 ; 运算到前后两个x2,x1 的差的绝对值小雨1e-6为止。
*/
int main()
{
float x1=1;
float x2=2;
int n;
printf("input n:  ");
scanf("%d",&n);
while ( fabs(x2-x1)>JD )
{
    x2 = (x1 + n/x1) / 2 ;
    x1=x2;
}

printf("%f",x2);
return 0;
}


大家好
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|有戏 粤ICP备2020111303号

GMT+8, 2025-12-6 16:44 , Processed in 0.081312 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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