二分法方案。我只有一个问题 [英] Bisection Method Program. Just one issue I have

查看:90
本文介绍了二分法方案。我只有一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将程序设置为打印出最后一次迭代。我只是不确定如何打印每次迭代,以便我可以看到它接近根。



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

double f( double x)
{
return 3 *(x + 1)*(x - 。< span class =code-digit> 5 )*(x- 1 ); // 更改每个问题的等式
}

int main()
{
double a,b,p,Tol,FA, FP;
int i = 1 ;
int 否;

printf( 输入第一个端点:);
scanf( %lf,& a);

printf( 输入第二个/最后一个终点:);
scanf( %lf,& b);

printf( Desired Tolerance:);
scanf( %lf,& Tol);

printf( Maximum Iterations:);
scanf( %d,& No);
FA = f(a);

while (i< = No)
{
p = a +(b-a)/ 2;
FP = f(p);

if ((FP == 0 )||((ba) / 2)< Tol)
{
printf( %lf \ n< /跨度>,p);
break ;
}
i ++; // i = i + 1

if ((FA * FP)> 0)
{
a = p;
FA = FP;
}
else
{
b = p;
}
}
if (i> No)
{
printf( %d后方法失败,否);
printf( iterations);
}
}





[edit]已添加代码块 - OriginalGriff [/ edit]

解决方案

只需添加另一个printf,或移动现有的printf

如果你想用它的数字和当前值打印每个迭代,那么在底部:

 printf( 迭代%d:当前value =%lf,i,p); 

否则,只需将现有的printf移到之外,如果条件:

 printf( %f\ n,p); 
if ((FP == 0 )||((ba)/ 2)< ; Tol)
{
break ;
}





[edit]将输出格式从整数更改为浮点数 - OriginalGriff [/ edit]


I have the program set to print out the final iteration. I am just unsure on how to print out each iteration so that I can see it approaching the root.

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

double f(double x)
{
return 3*(x+1)*(x-.5)*(x-1);  //change equation for each problem
}

int main()
{
    double a, b, p, Tol, FA, FP;
    int i=1;
    int No;

    printf("Enter first endpoint: ");
    scanf ("%lf", &a);

    printf("Enter second/last endpoint: ");
    scanf ("%lf", &b);

    printf("Desired Tolerance: ");
    scanf ("%lf", &Tol);

    printf("Maximum Iterations: ");
    scanf ("%d", &No);
    FA = f(a);

    while (i<=No)
    {
        p=a+(b-a)/2;
        FP = f(p);

        if ((FP==0)||((b-a)/2)< Tol)
        {
            printf("%lf\n",p);
            break;
        }
        i++;  //i=i+1

        if ((FA*FP)>0)
        {
            a=p;
            FA=FP;
        }
           else
           {
                b=p;
           }
        }
   if (i>No)
   {
   printf("Method Failed after %d", No);
   printf(" iterations");
   }
}



[edit]Code block added - OriginalGriff[/edit]

解决方案

Just add another printf, or move the existing one
If you want to print each iteration with it's number and current value, then add a line at the bottom:

printf("Iteration %d: Current value = %lf", i, p);

Otherwise, just move the existing printf outside the if condition:

printf("%f\n",p);
if ((FP==0)||((b-a)/2)< Tol)
{
    break;
}



[edit]Changed output format from integer to float - OriginalGriff[/edit]


这篇关于二分法方案。我只有一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆