用弦截法求解一元三次方程的根(利用c语言实现)

来源:百度知道 编辑:UC知道 时间:2024/07/07 19:26:30
#include<stdio.h>
#include<math.h>
float f(x)
{
float a,b,c,d;
float y;
printf("Please input a,b,c,d:\n");
scanf("%f,%f,%f,%f",&d,&d,&d,&d);
y=((a*x+b)*x+c)*x+b;
return (y);
}
float xpoint(x1,x2)
{
float y,x;
y=(x1*f(x2)-x2*f(x1))/(f(x)-f(x1));
return (y);
}
float x1,x2;
{
int i;
float x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if(y*y1>0)
{
y1=y;
x1=x;
}
else
x2=x;
}
while(fabs(y)>=0.000001);
rerurn(x);
}
main()
{
float x1,x2,f1,f2,x;
do
{
printf("Please input x1,x2:\n");
scanf("%f,%f",&x1,&x2);
f1=f(x1);
f1=f(x2);

试下这个,我是直接给abcd指定值了,
如果要传a b c d 要改f()和xpoint()的参数
float f(float x,float a,float b,float c,float,d)
float xpoint(float x1,floatx2,float a,float b,float c,float,d)
#include <math.h>
main()
{
float root(float x1, float x2);
float f(float x);
float xpoint(float x1,float x2);
float x1,x2,f1,f2,x;
do
{
printf("input x1,x2:\n");
scanf("%f,%f",&x1,&x2);
f1=f(x1);
f2=f(x2);
}while(f1*f2>=0);
x=root(x1,x2);
printf("A root of equation is %8.4f\n",x);
getch();
}
float root(float x1, float x2)
{
int i;
float x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if(y*y1>0)
{
y1=y;
x1=x;
}else
x2=x;
}whil