-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterasi_sederhana.cpp
282 lines (247 loc) · 5.72 KB
/
iterasi_sederhana.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <iostream>
#include <stdio.h>
#include <conio.h> //yogi
#include <math.h>
#include <cmath>
#include <iomanip>
#include <windows.h>
using namespace std;
float xiyogi(float yogi) //g(x-i) and xi
{
float inYogi;
inYogi = exp(-yogi);
return inYogi;
}
float fxiyogi(float arif) //f(xi)
{
float theYogi;
theYogi = exp(-arif)-arif;
return theYogi;
}
void yogi()
{
cout<<" Created On Thursday 09.30am - 09/20/2018 | MTK DISKRIT \n";
cout<<"___________________________________________| 17615006 [NIM]";
puts("\n\n\tMETHOD - ITERESI SEDERHANA [ Numeric ]");
for (int yogis=0 ; yogis <66 ; yogis++)
{
cout <<"_";
}
cout <<endl;
cout<<"\n\n\tDiketahui : Fungsi\t= F(x)=exp(-x)-x | g(x) = e^-3";
cout <<endl ;
}
int program() //program
{
YOGIPROGRAM:
system("Color 0A");
cout << fixed;
cout.precision(6);
yogi();
float jikaFabs;
float xNol;
float epilson;
float iterasi;
float dante; //for xi
cout <<endl;
for(int i = 0 ; i < 66; i++)
{
cout <<"_";
}
cout <<endl <<"NO.1"<<endl;
/*
2. Sediakan fasilitas input nilai awal x0, galat e, jumlah Iterasi N
*/
cout <<"\tMasukan Nilai Awal X NOL \t: " ;
cin >>xNol;
float tetapOk;
tetapOk = xNol; //penetapan x awal val perm
cout <<"\tMasukan Nilai epilson(Galat) \t: " ;
cin >>epilson;
// cout <<"\tMasukan Nilai Iterasi \t\t: " ;
// cin >>iterasi;
//============================ DI Soal menggunakan input tetapi kita tidak tahu berapa iterasi hingga ia stop
//============================ algoritma jika abs xi maka loop
//============================ program ini bisa menggunakan input iterasi dan tanpa iterasi
cout <<endl;
for(int i = 0 ; i < 105; i++)
{
cout <<"_";
}
cout <<endl <<endl;
cout <<"Iterasi\t\tx0\t\t\tg(x-1)\t\t\txi\t\t\tf(xi)";
cout<<endl;
for(int ix = 0 ; ix <105;ix++)
{
cout<<"_";
}
cout <<endl;
float duno = 2;
for ( int it = 1; it <duno ; it++)
{
YOGI:
// //for xi = //g(x-1)
dante = xiyogi(xNol);
// //iterasi //x0 //g(x-1) //xi //f(xi)
cout <<"\n| "<<it <<"|\t\t|" <<tetapOk <<"|\t\t|" <<xiyogi(xNol) <<"|\t\t|" <<dante <<"|\t\t|" <<fxiyogi(dante)<<"\t| ";
//for loop
if(jikaFabs= fabs(fxiyogi(dante)) > epilson) //karena iterasi inputan sebetulnya
{ //kondisi if Fabs tidak di butuhkan
xNol = dante; //langsung panggil xNol = dante program akan loop
duno++; //buat nambah looping
it++; //penyesuaian nilai berikutnya
goto YOGI; //ke dalam if
}
cout <<endl;
}
for(int ix = 0 ; ix <105;ix++)
{
cout<<"_";
}
cout <<endl;
cout <<"hasil akar persamaan = " <<dante;
cout <<endl;
for(int ix = 0 ; ix <35;ix++)
{
cout<<"_";
}
cout <<endl;
char ulangi;
cout <<endl <<"Ulangi Program ? 'Y' / '!Y' Exit :";
cin >>ulangi;
if(ulangi == 'Y' || ulangi == 'y')
{
system("cls");
program();
}
if(!ulangi == 'Y' || !ulangi == 'y')
{
system("exit");
}
}
int programSatu() //program
{
system("Color 0A");
cout << fixed;
cout.precision(6);
yogi();
float jikaFabs;
float xNol;
float epilson;
float iterasi;
float dante; //for xi
cout <<endl;
for(int i = 0 ; i < 66; i++)
{
cout <<"_";
}
cout <<endl <<"NO.2" <<endl;
/*
2. Sediakan fasilitas input nilai awal x0, galat e, jumlah Iterasi N
*/
cout <<"\tMasukan Nilai Awal X NOL \t: " ;
cin >>xNol;
float tetapOk;
tetapOk = xNol; //penetapan x awal val perm
cout <<"\tMasukan Nilai epilson(Galat) \t: " ;
cin >>epilson;
cout <<"\tMasukan Nilai Iterasi \t\t: " ;
cin >>iterasi;
//============================ DI Soal menggunakan input tetapi kita tidak tahu berapa iterasi hingga ia stop
//============================ algoritma jika abs xi maka loop
//============================ program ini bisa menggunakan input iterasi dan tanpa iterasi
cout <<endl;
for(int i = 0 ; i < 105; i++)
{
cout <<"_";
}
cout <<endl <<endl;
cout <<"Iterasi\t\tx0\t\t\tg(x-1)\t\t\txi\t\t\tf(xi)";
cout<<endl;
for(int ix = 0 ; ix <105;ix++)
{
cout<<"_";
}
cout <<endl;
//
// float duno = 2;
for ( int it = 1; it <=iterasi ; it++)
{
// YOGI:
// //for xi = //g(x-1)
dante = xiyogi(xNol);
// //iterasi //x0 //g(x-1) //xi //f(xi)
cout <<"\n| "<<it <<"|\t\t|" <<tetapOk <<"|\t\t|" <<xiyogi(xNol) <<"|\t\t|" <<dante <<"|\t\t|" <<fxiyogi(dante)<<"\t| ";
//for loop
if(jikaFabs= fabs(fxiyogi(dante)) > epilson) //karena iterasi inputan sebetulnya
{ //kondisi if Fabs tidak di butuhkan
xNol = dante; //langsung panggil xNol = dante program akan loop
// duno++; //buat nambah looping
//// it++; //penyesuaian nilai berikutnya
// goto YOGI; //ke dalam if
}
cout <<endl;
}
for(int ix = 0 ; ix <105;ix++)
{
cout<<"_";
}
cout <<endl;
cout <<"hasil akar persamaan = " <<dante;
cout <<endl;
for(int ix = 0 ; ix <35;ix++)
{
cout<<"_";
}
cout <<endl;
char ulangi;
cout <<endl <<"Ulangi Program ? 'Y' / '!Y' Exit :";
cin >>ulangi;
if(ulangi == 'Y' || ulangi == 'y')
{
system("cls");
programSatu();
}
if(!ulangi == 'Y' || !ulangi == 'y')
{
system("exit");
}
}
void pilihanMenu()
{
int pil;
cout <<endl <<endl;
cout <<"\t\tPilih program yang ingin digunakan";
cout <<endl;
for(int i = 0 ; i < 66; i++)
{
cout <<"_";
}
cout <<"\n\t1. Dengan Input-an X0 , Galat , Iterasi"
"\n\t2. Dengan Input-an X0 dan Galat";
cout <<endl ;
TIDAK:
cout <<"\nMasukan nomor Program : ";
cin >>pil;
if(pil ==2)
{
system("cls");
program();
}
else if(pil == 1)
{
system("cls");
programSatu();
}
else
{
cerr<<"Pilihan Tidak ada";
goto TIDAK;
}
}
int main()
{
system("Color 0A");
yogi();
pilihanMenu();
}