bisection in C++


#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
float fungsi(float x)
{
float y = pow(x, 0.666666) + log(x) ;
return y ;
}
int main()
{
m:
float xl,xh,xr,yr;
cout << ” fungsi : y = x^(2/3) + ln (x) ” << “\n”;
cout << “:: Kita akan mencari nilai x dimana y = 0, dengan metode bisection ::”<< “\n”;
cout << “Masukkan batas bawah : “; cin >> xl;
cout << “Masukkan batas atas  : “; cin >> xh;
for (int n=0;n<1000;++n)
{
xr = (xl+xh)/2;
yr = fungsi(xr);
cout << xr << “:::::” << yr << “\n”;
if(yr<0.00001 && yr>-0.00001)
{
cout << “Hasil akarnya adalah : ” << xr <<endl;
getch();
return 0;
}
else if (yr < 0 )
xl = xr;
else
xh = xr;
}
if (yr>0.00001)
{ cout << “Batas anda salah, ubahlah…!”<<”\n”;
goto m;
}
if (yr=’error’)
{ cout << “fungsi ln gak ada yg negatif… ubah batasnya kuncup!”<<”\n\n\n”;
goto m;
}
getch ();
return 0;
}

#include <iostream>

#include <math.h>

#include <conio.h>

using namespace std;

float fungsi(float x)

{

float y = pow(x, 0.666666) + log(x) ;

return y ;

}

int main()

{

m:

float xl,xh,xr,yr;

cout << ” fungsi : y = x^(2/3) + ln (x) ” << “\n”;

cout << “:: Kita akan mencari nilai x dimana y = 0, dengan metode bisection ::”<< “\n”;

cout << “Masukkan batas bawah : “; cin >> xl;

cout << “Masukkan batas atas  : “; cin >> xh;

for (int n=0;n<1000;++n)

{

xr = (xl+xh)/2;

yr = fungsi(xr);

cout << xr << “:::::” << yr << “\n”;

if(yr<0.00001 && yr>-0.00001)

{

cout << “Hasil akarnya adalah : ” << xr <<endl;

getch();

return 0;

}

else if (yr < 0 )

xl = xr;

else

xh = xr;

}

if (yr>0.00001)

{ cout << “Batas anda salah, ubahlah…!”<<”\n”;

goto m;

}

if (yr=’error’)

{ cout << “fungsi ln gak ada yg negatif… ubah batasnya kuncup!”<<”\n\n\n”;

goto m;

}

getch ();

return 0;

}

By ridlocepheid Posted in nethelp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s