当前位置:首页 » 编程语言 » c语言迭代法求立方根
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言迭代法求立方根

发布时间: 2023-02-24 16:20:35

① C++用迭代法求立方根

我找了一段正确的程序,如下:
注意看,它的判断条件跟你的是不同的,还有就是你的代码,一开始x1=0的话,x2=2/3*x1+a/(3*x1*x1);
分母为0了,当然出错了。
求立方根的牛顿法基于如下事实,如果y是x的立方根的一个近似值,那么下式将给出一个更好的近似值:
(x/y2+2y)/3
代码:
#include<stdio.h>
#include
<math.h>
float
fun(float
guess,float
x)
{
if(abs(guess*guess*guess-x)<0.0000001)
return
guess;
else
return
fun((x/guess/guess+2*guess)/3,x);
}
int
main()
{
float
a,b;
while(scanf("%f%f",&a,&b))
printf("%f\n",fun(a,b));
}
//a表示你猜测b的立方根大概等于几.

c语言迭代算法,已知x使用公式计算x的立方根

#include <stdio.h>
int main()
{
float x = 1.0;
float a;
float xtmp = 0.0;

printf("\nInput a :\t");
scanf("%f",&a);
while((x-xtmp >1e-5) || (x-xtmp < -1e-5))
{
xtmp = x;
x = (2*xtmp/3 + a/(3*xtmp*xtmp));
}

printf("\nx = %f",x);
return 0;
}

③ c语言怎么求立方根

第一,初值怎么给都无所谓只是迭代的问题,只要符合条件是非负数就行。第二,假如要求立方根只不过是求导数时不一样。

④ c语言 用迭代法求解

#include
<stdio.h>
#include
<math.h>
int
main(void)
{//本程序已测试,完全可用
double
a;
printf("input
a:");
scanf("%lf",&a);
if(fabs(a)<1e-13){//需要注意为0的情况,double双精度精确到15位,这里设置下
printf("0");
return
0;
}
double
x,y;
x=a;
y=(2*x/3)+(a/(3*x*x));
while(fabs(y-x)>0.000001){
x
=
y;
y=(2*x/3)+(a/(3*x*x));
printf("%.6f
%.6f\n",x,x*x*x);
}
}
第一个回答的同志有两个错误
1,倒数第三行while(fabs(xk2-temp)<1e-6)中的<应该改为>才对
2,没有考虑输入的数很小为0的情况

⑤ C语言:要求用递归方法编程,用迭代法求x=a的立方根

floatGetCubeRoot(floatx,floata)//调用的时候直接GetCubeRoot(a,a),a为要开根的数
{
floatx1=2.0/3*x+a/(3*x*x);
if(x-x1>-0.00001&&x-x1<0.00001)
returnx1;
else
return GetCubeRoot(x1,a);
}

⑥ c++迭代法求立方根 怎么做!!


#include<iostream>
#include<cmath>
usingnamespacestd;

intmain()
{

doublex1,x2,a;
cout<<"inputx1=";
cin>>a;
x1=2;x2=0.0;
//while(abs(x1-x2)<1e-5)
while((x1-x2>1e-5)||(x1-x2<-1e-5))
{
x2=x1;
x1=2*x2/3.0+a/(3*x2*x2);

}
cout<<x1<<endl;

return0;
}

⑦ 用c语言迭代法实现x=a的立方

import java.util.*;
public class T {
void show(int x){
System.out.println("输出立方:"+Math.pow(x, 3));
}
public static void main(String[] args){
T t=new T();
Scanner sc=new Scanner(System.in);
System.out.println("请输入你自定义的数x:");
int x=sc.nextInt();
t.show(x);

}
}
运行结果如下:
请输入你自定义的数x:
3
输出立方:27.0

这是我用java代码给你写的,不过注意:我并没有用迭代法,等一会我给你发过迭代法的做法;