Professional Documents
Culture Documents
NIM : 200170017
TUGAS !
1. Buatlah 1 soal tentang metode newton, secant, dan direct (selain soal dari modul) dan
selesaikan, lalu buatlah program untuk menyelesaikan soal – soal tersebut !
A. Metode Newton
• Sourcecode Program :
#include <stdio.h>
#include <math.h>
#include <conio.h>
float Fungsi(float x);
float FungsiTurunan(float x);
main()
{
int n=0;
float x[100];
float e = 0.0005;
printf(” Program Newton Rapshon \n”);
printf(” ====================== \n\n”);
printf(“Persamaan Fungsi f(x) : \n”);
printf(” x*x + 3x – 10 \n\n”);
printf(“x(n+1) : \n”);
printf(” x(n)-(f(xn)/f'(xn)) \n\n”);
printf(“Nilai e = 0.0005\n\n”);
printf(“==========================================================
========\n”);
printf(“! n ! xn ! f(xn) ! f'(xn) ! f(xn)/f'(xn) ! x(n+1) !\n”);
printf(“==========================================================
========\n”);
do
{
x[n+1] = x[n] – (Fungsi(x[n])/FungsiTurunan(x[n]));
printf(“! %d ! %2.4f ! %2.4f ! %2.4f ! %2.4f ! %2.4f !\n”, n, x[n],Fungsi(x[n]),
FungsiTurunan(x[n]),Fungsi(x[n])/FungsiTurunan(x[n]),x[n+1]);
n++;
}while(abs(x[n-1]-x[n-2])>e);
printf(“! %d ! %2.4f ! %2.4f ! %2.4f ! %2.4f ! %2.4f !\n”, n, x[n],Fungsi(x[n]),
FungsiTurunan(x[n]),Fungsi(x[n])/FungsiTurunan(x[n]),x[n+1]);
printf(“==========================================================
========\n\n”);
printf(“Jadi, Hasil yang memenuhi dari persamaan tersebut x = %2.4f”, x[n]);
getch();
}
float Fungsi(float x)
{
return pow(x,2)+(3*x)-10;
}
float FungsiTurunan(float x)
{
return 2*x+3;
}
a. Output – 1
B. Metode Secant
• Sourcecode Program
#include <iostream>
#include <conio.h>
#include <cmath>
#include <iomanip>
using namespace std;
cout<<"========================================================"<
<endl;
cout<<"================= ============"<
<endl;
cout<<"==================== Secant
Method ======="<<endl;
cout<<"================= ==========="<
<endl;
cout<<"========================================================"<
<endl;
cout<<"Function: f(x) = e^x - (x^2)/3 - x^3"<<endl;
cout<<"Enter the 2 initial guess "<<endl;
cout<<"Guess 1: ";cin>>d;
cout<<"Guess 2: ";cin>>e;
cout<<"Enter the relative error [E]: "; cin>>epsilon;
n=1;
if(e>d)
{p=d;
q=e;}
else
{q=d;
p=e;}
cout<<setw(10)<<"i"<<setw(15)<<"x0"<<setw(15)<<"x1";
cout<<setw(15)<<"xk"<<setw(15)<<"|E|"<<endl;
do{
r=q-((f(q)*(q-p))/(f(q)-f(p)));
s=fabs((r-q)/r);
cout<<setw(10)<<n<<setw(15)<<p<<setw(15)<<q<<setw(15)<<r<<setw(15)<<s<<en
dl;
p=q;
q=r;
n++;
} while (n<30 && s>epsilon && s!=0);
cout<<"\n\n Approx. root = "<<r;
cout<<endl<<endl<<endl;
return 0;
}
//FUNCTION
float f( float x) {
return (exp(x) - x*x/3-pow(x,3));
}
• Screenshoot Hasil Program
C. Metode Direct
• Sourcecode Program
// Program 1.3a
// Metode Iterasi
#include <stdio.h>
#include <math.h>
/* Daftar Variable
x0 = harga awal
tol = toleransi
float x0,tol;
int max_iter;
float g(float x)
int main()
int it;
float epsilon,xb;
it = 0;
do
{
it = it + 1;
xb = g(x0);
epsilon = fabs(xb-x0);
x0 = xb;
if(it<=max_iter)
printf(“Toleransi terpenuhi\n”);
#include
float i,x0,tol_error;
int iterasi_max;
float f(float x)
return x+exp(x);
float g(float x)
return -exp(x);
}
void main()
int i;
float epsilon,xb;
printf("\n");
scanf("%f",&x0);
scanf("%f",&tol_error);
scanf("%d",&iterasi_max);
i=0;
printf("\n");
printf(" ~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~");
printf(" ~~~~~~~~~~~~~~
~~~~~~~~~~~~~\n");
do
{
i=i+1;
xb=g(x0);
epsilon=fabs(f(xb));
x0=xb;
printf(" ~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~\n");
if(i<=iterasi_max)
}
• Screenshoot Hasil Program