当前位置: 首页 > news >正文

python牛顿法算立方根_牛顿迭代法求a的立方根的C语言程序?

2011-09-21 回答

/* header files */

#include

#include

#include

/* functions */

double cbrt_simple(double x);

double cbrt_newton(double a, double x);

/* main */

int main(void) {

double x2 = 2.0 * 2.0 * 2.0;

double x3 = 3.0 * 3.0 * 3.0;

double x4 = 4.0 * 4.0 * 4.0;

double x5 = 5.0 * 5.0 * 5.0;

printf("%.1fの立方根 (真値 = 2.0)\n", x2);

printf("cbrt: %.16f\n", cbrt(x2));

printf("1/3: %.16f\n", cbrt_simple(x2));

printf("Newton: %.16f\n", cbrt_newton(x2, 10.0));

printf("\n%.1fの立方根 (真値 = 3.0)\n", x3);

printf("cbrt: %.16f\n", cbrt(x3));

printf("1/3: %.16f\n", cbrt_simple(x3));

printf("Newton: %.16f\n", cbrt_newton(x3, 10.0));

printf("\n%.1fの立方根 (真値 = 4.0)\n", x4);

printf("cbrt: %.16f\n", cbrt(x4));

printf("1/3: %.16f\n", cbrt_simple(x4));

printf("Newton: %.16f\n", cbrt_newton(x4, 10.0));

printf("\n%.1fの立方根 (真値 = 5.0)\n", x5);

printf("cbrt: %.16f\n", cbrt(x5));

printf("1/3: %.16f\n", cbrt_simple(x5));

printf("Newton: %.16f\n", cbrt_newton(x5, 10.0));

return EXIT_SUCCESS;

}

/**

* 1/3乗をして立方根を求める

* @param[in] x 実数

* @return x の立方根

*/

double cbrt_simple(double x) {

return pow(x, 1.0 / 3.0);

}

/**

* ニュートン法で立方根を近似する

* @param[in] a 実数

* @param[in] x 解に近そうな値

* @return a の立方根

*/

double cbrt_newton(double a, double x) {

double e;

do {

e = (x * x * x - a) / (3.0 * x * x);

x = x - e;

} while ( fabs(e) > 1.0e-16 );

return x;

}

--------

実行結果:

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

[wtopia]$ gcc -Wall -O2 -o cbrt cbrt.c [~/src_c]

[wtopia]$ ./cbrt [~/src_c]

8.0の立方根 (真値 = 2.0)

cbrt: 2.0000000000000000

1/3: 2.0000000000000000

Newton: 2.0000000000000000

27.0の立方根 (真値 = 3.0)

cbrt: 3.0000000000000000

1/3: 3.0000000000000000

Newton: 3.0000000000000000

64.0の立方根 (真値 = 4.0)

cbrt: 4.0000000000000000

1/3: 3.9999999999999996

Newton: 4.0000000000000000

125.0の立方根 (真値 = 5.0)

cbrt: 5.0000000000000000

1/3: 4.9999999999999991

Newton: 5.0000000000000000

补充:

没有想到从 Terminal 下面复制到这里面效果这么差劲, 国内的系统果然够烂.

相关文章:

  • 搭上Windows 7快车,Windows Embedded Standard 2011更名为Windows Embedded Standard 7并发布RC版...
  • sqlserver大数据量update_选择多≠随便选,数据分析软件9大软件评测教你怎么选!...
  • Windows Phone 7 Series 概述、亮点以及。。。。。。
  • c++矩阵转置_MATLAB的矩阵运算与重构
  • rabbitmq 多个消费者消费一个队列_RabbitMQ——消费端限流、TTL、死信队列
  • DX9中如何模拟DX10/11里的ConstantBuffer
  • python怎么除去列表l中所有是x的元素_Python学习教程(Python学习路线):Python3之递归函数简单示例...
  • Windows phone 7 Series发布!
  • json_extract提取复杂json_4个小窍门,让你在Python中高效使用JSON
  • 关于Windows phone 7 series开发方面的传言
  • centos找不到apt-get命令_Centos 使用 pyinstaller 打包踩坑分享
  • python cv2 imshow_Python-OpenCV 2. 图像基本操作
  • wild magic3 渲染状态更新和scene绘制
  • 跨域获取后台数据undefined_拨号跨域问题
  • 软件文档归类汇总
  • css选择器
  • exports和module.exports
  • Spark RDD学习: aggregate函数
  • SpringBoot几种定时任务的实现方式
  • STAR法则
  • 从tcpdump抓包看TCP/IP协议
  • 从零开始学习部署
  • 电商搜索引擎的架构设计和性能优化
  • 高度不固定时垂直居中
  • 三栏布局总结
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 问题之ssh中Host key verification failed的解决
  • 延迟脚本的方式
  • 一些关于Rust在2019年的思考
  • No resource identifier found for attribute,RxJava之zip操作符
  • 积累各种好的链接
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • (1)Nginx简介和安装教程
  • (13)Hive调优——动态分区导致的小文件问题
  • (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)...
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (全注解开发)学习Spring-MVC的第三天
  • (算法)Travel Information Center
  • (转载)VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .NET Core工程编译事件$(TargetDir)变量为空引发的思考
  • .Net Core和.Net Standard直观理解
  • .NET/C# 中设置当发生某个特定异常时进入断点(不借助 Visual Studio 的纯代码实现)
  • .netcore 获取appsettings
  • .net中我喜欢的两种验证码
  • .pings勒索病毒的威胁:如何应对.pings勒索病毒的突袭?
  • /proc/vmstat 详解
  • @private @protected @public
  • [20171106]配置客户端连接注意.txt
  • [Android] Implementation vs API dependency
  • [DL]深度学习_Feature Pyramid Network
  • [flume$2]记录一个写自定义Flume拦截器遇到的错误
  • [gdc19]《战神4》中的全局光照技术