弦截法求解方程

来源:互联网 发布:dota2数据公开 编辑:程序博客网 时间:2024/04/29 03:45

/*弦截法求解方程  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");
 }

原创粉丝点击