| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 427 人关注过本帖
标题:关于二分法的小问题,高手帮帮忙吧
只看楼主 加入收藏
qicaiwuya
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-11-8
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:1 
关于二分法的小问题,高手帮帮忙吧
我想让用户输入一个区间比如-10~10,然后我根据这个区间中根的个数将区间划分成count个,并分别把各个区间的左右边界保存到bot[]和top[]数组中,然后用二分法对各个区间的值求解并输出。

其实主要应该是find函数出了问题,哪位高手帮帮忙啊,我是新手,谢谢啦
#include "math.h"
#include "stdio.h"
float polynomial(float x)
{//函数功能,根据参数x求得函数值并返回
    float y;
    y=((3*x+6)*x-1)*x+2;
    return y;
}//polynomial函数

int find(int *bot,int *top,int bot1,int top1)
{//函数功能,根据多项式函数fun自动搜索求根区间,并把各个区间的上限和下限分别保存到top[]和bot[]数组中,返回包含根的区间个数
    float y1,y2;
    int x1,x2;
    int count=0;
    x1=bot1;
    x2=bot1+1;
    while(x2<=top1)
      {
      y1=polynomial((float)x1);
      y2=polynomial((float)x2);
      while(y1*y2>0&&x2<=top1)
        {
        x2++;
        y2=polynomial((float)x2);   
        }
      bot[count]=x1;
      top[count]=x2;
      count++;
      x1=x2;
      x2=x1+1;  
      }
    return count;  
   
}//find函数

float bin_root(int bot,int top)
{//函数功能,二分法求根
    float y,y1,y2;
    float x1=bot,x2=top,x;
    x=(x1+x2)/2;
    y=polynomial(x);
    y1=polynomial(x1);
    y2=polynomial(x2);
    while(fabs(y)>=1e-6)
      {
      if(y*y1>0)
        {
        x2=x;   
        }   
      else
        {
        x1=x;   
        }
      x=(x1+x2)/2;
      y=polynomial(x);
      y1=polynomial(x1);
      y2=polynomial(x2);  
      }
    return x;  
}//二分法求根bin_root函数

void main()
{
    int count,bot[10],top[10];
    int bot1,top1;//由用户输入求根区间,程序自动找到该区间中存在的根的个数
    int i;
    float root;
    printf("请输入求根区间:\n");
    scanf("%d%d",&bot1,&top1);
    count=find(bot,top,bot1,top1);
    if(count==0)printf("函数在您输入的区间内无根:\n");
    for(i=0;i<count;i++)
      {
      root=bin_root(bot[i],top[i]);
      printf("函数在子区间[%d,%d]内的根为:%.4f\n",bot[i],top[i],root);   
      }
}

[ 本帖最后由 qicaiwuya 于 2011-11-8 16:22 编辑 ]
搜索更多相关主题的帖子: 多项式 二分法 return include 搜索 
2011-11-08 16:20
编程的乐趣
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:229
专家分:1027
注 册:2011-4-4
收藏
得分:7 
.
2011-11-08 16:24
快速回复:关于二分法的小问题,高手帮帮忙吧
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018167 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved