求用割线法求方程x的3次方-2x-5=0在x0=2附近的根,取x0=2,x1-=2.2,计算到4位有效数字的C语言程序
展开全部
#include <stdio.h>
#include <math.h>
float ff(float x)
{
return x*(x*x-2)-5;
}
float Secant(float x0,float x1)
{
return (x1 - (ff(x1)*(x1-x0))/(ff(x1)-ff(x0)));
}
void main()
{
int number,k=2;
float x0=2,x1=2.2,x2;
printf("x[0] = %.4f, x[1] = %.4f, ",x0,x1);
while (1)
{
if (fabs(x0-x1)<1e-4) break;
x2 = Secant(x0,x1);
x0 = x1;
x1 = x2;
printf("x[%d] = %.4f, ",k,x2);
k++;
if (k%3==0) printf("\n");
}
if (k%3!=0) printf("\n");
printf("iteration times: %d, roots: %.4f\n ",k-2,x1);
return;
}
#include <math.h>
float ff(float x)
{
return x*(x*x-2)-5;
}
float Secant(float x0,float x1)
{
return (x1 - (ff(x1)*(x1-x0))/(ff(x1)-ff(x0)));
}
void main()
{
int number,k=2;
float x0=2,x1=2.2,x2;
printf("x[0] = %.4f, x[1] = %.4f, ",x0,x1);
while (1)
{
if (fabs(x0-x1)<1e-4) break;
x2 = Secant(x0,x1);
x0 = x1;
x1 = x2;
printf("x[%d] = %.4f, ",k,x2);
k++;
if (k%3==0) printf("\n");
}
if (k%3!=0) printf("\n");
printf("iteration times: %d, roots: %.4f\n ",k-2,x1);
return;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |