您的位置:首页 > 其它

弦截法求解方程

2007-09-08 15:59 239 查看
/*弦截法求解方程  Micro_lee*/

#include <stdio.h>
#include <string.h>
#include <math.h>

void asterisk_triangle(int n);

float f(float x)
{
 return x*( x*(x-5)+16 )-80;
}

float xpoint(float x1, float x2)
{
 return (x1*f(x2) - x2*f(x1))/( f(x2)-f(x1) );
}

float root(float x1, float x2)
{
 float x,y,y1,y2;

 y1=f(x1);
 y2=f(x2);
 do{
  x=xpoint(x1,x2);
  y=f(x);

  if(y*y1 > 0)
  {
   y1=y;
   x1=x;
  }
  else
  {
   y2=y;
   x2=x;
  }
 
 }while( fabs(y)>=0.0001);

 return (x);

}

int 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 is %8.4f/n/n", x);

 return 0;

}

 

 

 

 

 

 

void asterisk_triangle(int n)
{
 int i,j,k, num=0;

 for(i=1; i<=n;  i++)
 {
  
        num= (2*i-1) > n ? 2*(n+1-i)-1: 2*i-1 ;

  for(k=1; k<=(n-num)/2; k++)
   printf(" ");
  for(j=1; j<=num; j++)
   printf("*");
  
     printf("/n");
 }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  float math.h input