-2

I'm study Computer science in 4 degree and i'm trouble with bisection method implementation in c++. The error is about the code run one time and end, i tried some changes but any good result :( If you can help me, please do it. I saw some alternative done codes, but not helped me because my is so different.

my code following:

#include <iostream>
#include <math.h>

using namespace std;


double funcao(double x)
{
    double resultado;
    resultado = x*log10(x)-1;
    return resultado;
}

double E(double xk,double xkAnt)
{
    double resultado =0;
    resultado= fabs((xk-(xkAnt))/xk);
    cout<<"O resultado de E é: "<<resultado<<"\n\n";
    return resultado;
}

//   1)metodo da Bissecção:
// Este programa implementa o método da bissecção para obter a raíz
int main()
{
    setlocale(LC_ALL,"Portuguese");
    double a,b,xk,xkAnt,erro;

    xkAnt=0;
    a=2.0;
    b=3.0;

    //cout<<"Digite o valor(double) para o erro \n"<<"Erro: ";
    erro=0.005;


    while(E(xk,xkAnt)>erro)
    {
        xk= (a+b)/2;
        if((funcao(a)*funcao(xk))<0)
        {
            b=xk;
            xkAnt=xk;
            cout<<"Multiplicação das Func. <0 B = xk \n\n";
        }
        else
        {
            a=xk;
            xkAnt=xk;
            cout<<"Multiplicação das Func. >0 A = xk \n\n";
        }


        if(xkAnt==0)
        {
            cout<<"A: "<<a<<endl<<"B: "<<b<<endl<<"XK: "<<xk<<endl<<"Fun(XK): "<<funcao(xk)<<endl<<"Erro: *"<<endl<<endl;
        }
        else
        {
            cout<<"A: "<<a<<endl<<"B: "<<b<<endl<<"XK: "<<xk<<endl<<"Fun(XK): "<<funcao(xk)<<endl<<"Erro: "<<E(xk,xkAnt)<<endl<<endl;
        }

    }

    return 0;
}
Gui
  • 1
  • If your code works and you want it reviewed, you may want to post to [codereview.se]. – Thomas Matthews Aug 20 '17 at 16:20
  • Please edit your post with the results of your debugging session. Which statement is causing the issue? Is the issue about accuracy? What are the inputs? What are the expected outputs versus the actual outputs? – Thomas Matthews Aug 20 '17 at 16:22
  • If you assign `funcao(a)` to a temporary variable and `funcao(xk)` to another variable before the `if` statement, you can see their values easier during debugging. Same with the product. – Thomas Matthews Aug 20 '17 at 16:23
  • You can calculate `xkAnt=xk` before the `if` statement since it is the same in both cases. – Thomas Matthews Aug 20 '17 at 16:24
  • I don't understand why you need `funcao` and `E` since they are only a single statement of computation. You can assign the results to temporary variables and use the temporary variables in `while` or `if` statements instead of calling the functions in `while` or `if` statements. – Thomas Matthews Aug 20 '17 at 16:27
  • do you mean ... turn the function bellow into main statement? – Gui Aug 21 '17 at 12:26

1 Answers1

0

I've reading more and my solution for these consist in add int variable call k(iteration numbers) and if statement, wich ignore when k equals 0, after that the xkAnterior=xk and program runs like shine. Thanks for Thomas Matthews for help me thinking.

    #include <iostream>
#include <math.h>

using namespace std;


    double funcao(double x){
        double resultado;
        resultado = x*log10(x)-1;
        return resultado;
    }

    double calculaErro(double xk,double xkAnterior){
        double resultado;
        resultado= fabs((xk-xkAnterior)/xk);
        return resultado;
        cout<<"O resultado de e é: "<<resultado;
    }

//   1)metodo da Bissecção:
// Este programa implementa o método da bissecção para obter a raíz
int main()
{
    setlocale(LC_ALL,"Portuguese");
    double a,b,xk,xkAnterior,k=0,e;

    a=2.0;
    b=3.0;

        cout<<"Digite o valor(double) para o erro \n"<<"E: ";
        cin>>e;

        do
            {
            if(k==0)
            {
                xkAnterior=1;
            }
            else
            {
                xkAnterior=xk;
            }

            xk= (a+b)/2;
        cout<<"A: "<<a<<endl<<"B: "<<b<<endl<<"XK: "<<xk<<endl<<"Fun(XK): "<<funcao(xk)<<endl<<"Erro: "<<calculaErro(xk,xkAnterior);
        cout<<endl<<"XK: "<<xk<<endl<<"XK_Anterior: "<<xkAnterior<<endl;

        if((funcao(a)*funcao(xk))<0)
            {
            b=xk;
            cout<<"Multiplicação das Func. <0 B = xk \n\n";
            }
        else
            {
            a=xk;
            cout<<"Multiplicação das Func. >0 A = xk \n\n";
            }
            k++;
        }while(calculaErro(xk,xkAnterior)>e);

    return 0;
}
Gui
  • 1