用c语言求解热传导

typedef?double?(*func_call)(double?x);
double?x3(double?x)
{
??return?x*x*x?+?x*x*3?+?5*x?+?1;
}
double?resolve(func_call?f,?double?a,?double?b)
{
??double?fa;
??double?fb;
??if?(a?>?b)
??{
????double?t?=?a;
????a?=?b;
????b?=?t;
??}
??fa?=?f(a);
??fb?=?f(b);
??if?((fa?>=?0?&&?fb?<=?0)?||><=?0?&&?fb?>=?0))
??{
????double?m,?fm;
????while?(b?-?a?>?1e-5)
????{
??????m??=?(a?+?b)?/?2;
??????fm?=?f(m);
??????if?(fa?>?0)
??????{
????????if?(fm?>=?0)
????????{
??????????a?=?m;
??????????fa?=?fm;
????????}
????????else
????????{
??????????b?=?m;
??????????fb?=?fm;
????????}
??????}
??????else
??????{
????????if?(fm?>=?0)
????????{
??????????b?=?m;
??????????fb?=?fm;
????????}
????????else
????????{
??????????a?=?m;
??????????fa?=?fm;
????????}
??????}
????}
????return?m;
??}
??return?0;
}
int?main()
{
??double?x?=?resolve(x3,?-10,?10);
??printf("x:%f?f(x):%f\n",?x,?x3(x));
??return?0;
}

弦截法求方程的根是一种解方程得基本方法,在计算机编程中常用 。他的思路是这样的:任取两个数,判断这两个数的函数值,如果函数值是同号,换两个数再试,直到两个数x1,x2对应的函数值为异号时为止,这时方程的解肯定在这两个数x1,x2之间 。连接这两点所对应的函数值,连线与x轴的交点为新的x,若f(x)与f(x1)同号,则把x当作新的x1,将新的x1与x2连接,如此循环……如果f(x)与f(x1)异号,则把把x当作新的x2,将x1与新的x2连接,循环……
具体程序如下:
#include <math.h>
main()
{
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);
}
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;
  }while(fabs(y)>=0.0001);
return (x);
}
float f(float x)
{
float y;
y=((x-5.0)*x+16)*x-80;
return (y);
}
float xpoint(float x1,float x2)
{
float y;
y=(x1*f(x2)-x2*f(x1)) / (f(x2) - f(x1));
return (y) ;
}【用c语言求解热传导】

    推荐阅读