作者:乔山办公网日期:
返回目录:excel表格制作
Private Function NewTon(n As Double, eps As Double) As Double
Dim a(100) As Double
Dim x As Double
Dim y As Double
Dim k As Double
a(0) = 0
For i = 0 To 99
x = a(i)
y = x ^zd 3 - 2 * x ^ 2 + 4 * x + 1
k = 3 * x ^ 2 - 4 * x + 4
a(i + 1) = x - y / k
Print a(i + 1)
If Abs(y) < esp Then
NewTon = a(i + 1)
Exit Function
Else
NewTon = 0
End If
Next i
End Function
用单变量求解。
如果要批量的话,录制个宏,改成循环就行了。
#include<stdio.h>
#include<math.h>
int a,b,c,d;
float f(float x)
{ float y;
y=((a*x+b)*x+c)*x+d;
return(y);
}
float f1(float x)
{ float y;
y=(3*a*x+2*b)*x+c;
return(y);
}
void main()
{ float x0=1.0,x1;
printf("请输入a,b,c,d的值:\n");
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
x1=1;
do
{
x0=x1;
x1=x0-f(x0)/f1(x0);
}
while(fabs(x1-x0) >=0.00001);
printf("%f",x1);
}
在excel中无度解。不过可知以提供C语言代码供参考:
double func(double x) //函数道
{
return x*x*x*x-3*x*x*x+1.5*x*x-4.0;
}
double func1(double x) //导函数
{
return 4*x*x*x-9*x*x+3*x;
}
int Newton(double *x,double precision,int maxcyc) //迭代次数
{
double x1,x0;
int k;
x0=*x;
for(k=0;k<maxcyc;k++)
{
if(func1(x0)==0.0)//若通回过初值,函数返回答值为0
{
printf(