改成VBA牛顿迭代法求方程
解决时间 2021-02-28 07:29
- 提问者网友:心牵心
- 2021-02-27 21:08
改成VBA牛顿迭代法求方程
#include
#include
main()
{
float solution(float a,float b,float c,float d);
float a,b,c,d,x;
printf("请输入方程的系数:");
scanf("%f %f %f %f",&a,&b,&c,&d);
x=solution(a,b,c,d);
printf("所示方程的根x=%f",x);
}
float solution(float a,float b,float c,float d)
{
float x0,x=1.5,f,fd,h;
do
{
x0=x;
f=a*x0*x0*x0+b*x0*x0+c*x0+d;
fd=3*a*x0*x0+2*b*x0+c;
h=f/fd;
x=x0-h;
}
while(fabs(x-x0)>=1e-5);
return x;
}
最佳答案
- 五星知识达人网友:天凉才是好个秋
- 2021-02-27 22:07
Sub main()
Dim a!, b!, c!, d!, x!
a = InputBox("a=", "请输入方程的系数:")
b = InputBox("b=", "请输入方程的系数:")
c = InputBox("c=", "请输入方程的系数:")
d = InputBox("d=", "请输入方程的系数:")
x = solution(a, b, c, d)
Debug.Print "所示方程的根x="; x
End Sub
Function solution!(ByVal a!, ByVal b!, ByVal c!, ByVal d!)
Dim x0!, x!, f!, fd!, h!
x = 1.5
Do
x0 = x
f = a * x0 * x0 * x0 + b * x0 * x0 + c * x0 + d
fd = 3 * a * x0 * x0 + 2 * b * x0 + c
h = f / fd
x = x0 - h
Loop While (Abs(x - x0) >= 0.00001)
solution = x
End Function
全部回答
- 1楼网友:低血压的长颈鹿
- 2021-02-27 23:15
用^即可表示上标,10^(-5)可以表示10的-5次方。
#include
#include
double f( double x )
{
return x * x * x + 9.2 * x * x + 16.7 * x + 4;
}
double fdx( double x )
{
return 3 * x * x + 18.4 * x + 16.7;
}
int main( )
{
int t1 = 0, t2 = 1;
double x[ 2 ], ep = 1e-5;
x[ 0 ] = 0;
do
{
t1 = 1 - t1;
t2 = 1 - t2;
x[ t1 ] = x[ t2 ] - f( x[ t2 ] ) / fdx( x[ t2 ] );
} while( fabs( x[ t1 ] - x[ t2 ] ) > ep );
printf("%lf\n", x[ t1 ]);
return 0;
}
我要举报
大家都在看
推荐资讯