Anda di halaman 1dari 36

Nama : Muhammad Minang Merdeka

Nim : 20029127
Tugas minggu 7 Solusi soal 1a sd 1f dengan metode bagi dua dan metode posisi palsu
A. Metode Bagi dua

1. x+cos x

#include <iostream>

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <cmath>

#include <iomanip>

using namespace std;

float f(float x);

int main()

float x1,x2,xr,error,tol;

int i=1;

cout<<"========METODE BAGI DUA======="<<endl<<endl;;

cout<<"========MUHAMMAD MINANG MERDEKA (20029127)======="<<endl<<endl;;

cout<<"f(x) =x+cos x"<<endl;

//User menginput nilai x1 dan x2

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

if (f(x1)*f(x2)>0){
do{

cout<<"Masukkan lagi x1 dan x2 sehingga f(x1)*f(x2)<0"<<endl;

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

while (f(x1)*f(x2)>0);

cout<<"Masukkan nilai batas error: ";cin>>tol;

cout<<setw(5)<<"i"<<setw(15)<<"x1"<<setw(20)<<"x2"<<setw(20)<<"xr"<<setw(20)<<"f(x1)";

cout<<setw(20)<<"f(x2)"<<setw(20)<<"f(xr)"<<setw(20)<<"error"<<endl;

do

xr=(x1+x2)/2;

error= abs(x2-x1);

cout<<setw(5)<<i<<setw(15)<<x1<<setw(20)<<x2<<setw(20)<<xr<<setw(20)<<f(x1)<<setw(20);

cout<<f(x2)<<setw(20)<<f(xr)<<setw(20)<<error<<endl;

if(f(x1)*f(xr)<0)

{x2=xr;}

else

{x1=xr;

i++;
}

while( (error > tol) && (i < 40));

cout<<"Approx. root = "<<xr<<endl;

cout<<"Banyaknya iterasi : "<<i-1;

getch();

return 0;

float f(float x)

return cos(x)+x;

}
2. x^2 + sin x -2

#include <iostream>

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <cmath>

#include <iomanip>

using namespace std;

float f(float x);


int main()

float x1,x2,xr,error,tol;

int i=1;

cout<<"========METODE BAGI DUA======="<<endl<<endl;;

cout<<"========MUHAMMAD MINANG MERDEKA (20029127)======="<<endl<<endl;;

cout<<"f(x) =x^2+ sin x-2"<<endl;

//User menginput nilai x1 dan x2

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

if (f(x1)*f(x2)>0){

do{

cout<<"Masukkan lagi x1 dan x2 sehingga f(x1)*f(x2)<0"<<endl;

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

while (f(x1)*f(x2)>0);

cout<<"Masukkan nilai batas error: ";cin>>tol;

cout<<setw(5)<<"i"<<setw(15)<<"x1"<<setw(20)<<"x2"<<setw(20)<<"xr"<<setw(20)<<"f(x1)";

cout<<setw(20)<<"f(x2)"<<setw(20)<<"f(xr)"<<setw(20)<<"error"<<endl;
do

xr=(x1+x2)/2;

error= abs(x2-x1);

cout<<setw(5)<<i<<setw(15)<<x1<<setw(20)<<x2<<setw(20)<<xr<<setw(20)<<f(x1)<<setw(20);

cout<<f(x2)<<setw(20)<<f(xr)<<setw(20)<<error<<endl;

if(f(x1)*f(xr)<0)

{x2=xr;}

else

{x1=xr;

i++;

while( (error > tol) && (i < 40));

cout<<"Approx. root = "<<xr<<endl;

cout<<"Banyaknya iterasi : "<<i-1;

getch();

return 0;

float f(float x)

return pow(x,2) + sin (x) - 2;

}
3. e^-x + sin x

#include <iostream>

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <cmath>

#include <iomanip>

using namespace std;

float f(float x);


int main()

float x1,x2,xr,error,tol;

int i=1;

cout<<"========METODE BAGI DUA======="<<endl<<endl;;

cout<<"========MUHAMMAD MINANG MERDEKA (20029127)======="<<endl<<endl;;

cout<<"f(x) =e^-x + sin x"<<endl;

//User menginput nilai x1 dan x2

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

if (f(x1)*f(x2)>0){

do{

cout<<"Masukkan lagi x1 dan x2 sehingga f(x1)*f(x2)<0"<<endl;

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

while (f(x1)*f(x2)>0);

cout<<"Masukkan nilai batas error: ";cin>>tol;

cout<<setw(5)<<"i"<<setw(15)<<"x1"<<setw(20)<<"x2"<<setw(20)<<"xr"<<setw(20)<<"f(x1)";

cout<<setw(20)<<"f(x2)"<<setw(20)<<"f(xr)"<<setw(20)<<"error"<<endl;
do

xr=(x1+x2)/2;

error= abs(x2-x1);

cout<<setw(5)<<i<<setw(15)<<x1<<setw(20)<<x2<<setw(20)<<xr<<setw(20)<<f(x1)<<setw(20);

cout<<f(x2)<<setw(20)<<f(xr)<<setw(20)<<error<<endl;

if(f(x1)*f(xr)<0)

{x2=xr;}

else

{x1=xr;

i++;

while( (error > tol) && (i < 40));

cout<<"Approx. root = "<<xr<<endl;

cout<<"Banyaknya iterasi : "<<i-1;

getch();

return 0;

float f(float x)

return exp(-x) + sin (x);

}
4. 1-x-e^-3x

#include <iostream>

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <cmath>

#include <iomanip>

using namespace std;

float f(float x);


int main()

float x1,x2,xr,error,tol;

int i=1;

cout<<"========METODE BAGI DUA======="<<endl<<endl;;

cout<<"========MUHAMMAD MINANG MERDEKA (20029127)======="<<endl<<endl;;

cout<<"f(x) =1-x-e^-3x"<<endl;

//User menginput nilai x1 dan x2

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

if (f(x1)*f(x2)>0){

do{

cout<<"Masukkan lagi x1 dan x2 sehingga f(x1)*f(x2)<0"<<endl;

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

while (f(x1)*f(x2)>0);

cout<<"Masukkan nilai batas error: ";cin>>tol;

cout<<setw(5)<<"i"<<setw(15)<<"x1"<<setw(20)<<"x2"<<setw(20)<<"xr"<<setw(20)<<"f(x1)";

cout<<setw(20)<<"f(x2)"<<setw(20)<<"f(xr)"<<setw(20)<<"error"<<endl;
do

xr=(x1+x2)/2;

error= abs(x2-x1);

cout<<setw(5)<<i<<setw(15)<<x1<<setw(20)<<x2<<setw(20)<<xr<<setw(20)<<f(x1)<<setw(20);

cout<<f(x2)<<setw(20)<<f(xr)<<setw(20)<<error<<endl;

if(f(x1)*f(xr)<0)

{x2=xr;}

else

{x1=xr;

i++;

while( (error > tol) && (i < 40));

cout<<"Approx. root = "<<xr<<endl;

cout<<"Banyaknya iterasi : "<<i-1;

getch();

return 0;

float f(float x)

return 1-x-exp(-3*x);

}
5. 2x+tan x

#include <iostream>

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <cmath>

#include <iomanip>

using namespace std;

float f(float x);


int main()

float x1,x2,xr,error,tol;

int i=1;

cout<<"========METODE BAGI DUA======="<<endl<<endl;;

cout<<"========MUHAMMAD MINANG MERDEKA (20029127)======="<<endl<<endl;;

cout<<"f(x) =2x+tan x"<<endl;

//User menginput nilai x1 dan x2

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

if (f(x1)*f(x2)>0){

do{

cout<<"Masukkan lagi x1 dan x2 sehingga f(x1)*f(x2)<0"<<endl;

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

while (f(x1)*f(x2)>0);

cout<<"Masukkan nilai batas error: ";cin>>tol;

cout<<setw(5)<<"i"<<setw(15)<<"x1"<<setw(20)<<"x2"<<setw(20)<<"xr"<<setw(20)<<"f(x1)";

cout<<setw(20)<<"f(x2)"<<setw(20)<<"f(xr)"<<setw(20)<<"error"<<endl;
do

xr=(x1+x2)/2;

error= abs(x2-x1);

cout<<setw(5)<<i<<setw(15)<<x1<<setw(20)<<x2<<setw(20)<<xr<<setw(20)<<f(x1)<<setw(20);

cout<<f(x2)<<setw(20)<<f(xr)<<setw(20)<<error<<endl;

if(f(x1)*f(xr)<0)

{x2=xr;}

else

{x1=xr;

i++;

while( (error > tol) && (i < 40));

cout<<"Approx. root = "<<xr<<endl;

cout<<"Banyaknya iterasi : "<<i-1;

getch();

return 0;

float f(float x)

return (2*x) + tan(x);

}
6. 2x^2-e^(-x)
#include <iostream>

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <cmath>

#include <iomanip>

using namespace std;

float f(float x);


int main()

float x1,x2,xr,error,tol;

int i=1;

cout<<"MUHAMMAD MINANG MERDEKA (20029127)"<<endl<<endl;;

cout<<"=============================="<<endl<<endl;;

cout<<"METODE BAGI DUA"<<endl<<endl;;

cout<<"=============================="<<endl<<endl;;

cout<<"f(x) =2x^2-e^(-x)"<<endl;

//User menginput nilai x1 dan x2

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

if (f(x1)*f(x2)>0){

do{

cout<<"Masukkan lagi x1 dan x2 sehingga f(x1)*f(x2)<0"<<endl;

cout<<"Masukkan batas bawah persamaan(x1): "; cin>>x1;

cout<<"Masukkan batas atas persamaan (x2): ";cin>>x2;

while (f(x1)*f(x2)>0);

cout<<"Masukkan nilai batas error: ";cin>>tol;

cout<<setw(5)<<"i"<<setw(15)<<"x1"<<setw(20)<<"x2"<<setw(20)<<"xr"<<setw(20)<<"f(x1)";
cout<<setw(20)<<"f(x2)"<<setw(20)<<"f(xr)"<<setw(20)<<"error"<<endl;

do

xr=(x1+x2)/2;

error= abs(x2-x1);

cout<<setw(5)<<i<<setw(15)<<x1<<setw(20)<<x2<<setw(20)<<xr<<setw(20)<<f(x1)<<setw(20);

cout<<f(x2)<<setw(20)<<f(xr)<<setw(20)<<error<<endl;

if(f(x1)*f(xr)<0)

{x2=xr;}

else

{x1=xr;

i++;

while( (error > tol) && (i < 40));

cout<<"Approx. root = "<<xr<<endl;

cout<<"Banyaknya iterasi : "<<i-1;

getch();

return 0;

float f(float x)

return 2*pow(x,2)-exp(-x);
}
B. Metode Posisi Palsu

Anda mungkin juga menyukai