Program hitung_bifeksi;
uses wincrt;
function f(x:real):real;
begin
f:=(((sqr(x))*x)+(sqr(x))-(3*(x))-3);
end;
function error(x1,x2:real):real;
var
err:real;
begin
err:=(((x2-x1)/x2)*100);
if err<0 then
error:=err*(-1)
else
error:=err;
end;
function hitxc(xa,xb:real):real;
begin
hitxc:=(xa+xb)/2;
end;
procedure pilakar(var xa,xb:real;xc:real);
begin
if (f(xa)*f(xc)<0 )then
begin
xb:=xc;
end
else
begin
xa:=xc;
end;
end;
function cek(xa,xb:real):boolean;
var
c:boolean;
cek1:real;
begin
c:=false;
cek1:=(f(xa)*f(xb));
if (cek1<0) then
begin
c:=true;
end;
cek:=c;
end;
procedure lanjut(var it:integer; var xc1,xc2:real;xc:real);
begin
it:=it+1;
xc1:=xc2;
xc2:=xc;
end;
var
x1,x2,emin,er,xc,xc1,xc2,a,b,c:real;
iterasi:integer;
cek2:boolean;
begin
repeat
begin
clrscr;
write('masukan x1 : ');readln(x1);
write('masukan x2 : ');readln(x2);
write('masukan error min : ');readln(emin);
cek2:=(cek(x1,x2));
if (cek2=false) then
begin
writeln('iterasi tidak dapat dilakukan');
write('tekan enter');readln;
end;
end;
until(cek2=true);
iterasi:=0;
er:=100;
xc1:=0;
xc2:=0;
clrscr;
writeln('|ite | x1 | x2 | f(x1) | f(x2) | xc | f(xc) | eror |');
while (er > emin) do
begin
xc:=hitxc(x1,x2);
a:=f(x1);
b:=f(x2);
c:=f(xc);
lanjut(iterasi,xc1,xc2,xc);
if (iterasi<2) THEN
writeln('| ',iterasi,' | ',x1:0:5,'| ',x2:0:5,'| ',a:0:5,'| ',b:0:5,'| ',x1:0:5,'| ',c:0:5,'| |')
else
begin
er:=error(xc1,xc2);
writeln('| ',iterasi,' | ',x1:0:5,'| ',x2:0:5,'| ',a:0:5,'| ',b:0:5,'| ',x1:0:5,'| ',c:0:5,'| ',er:0:5,' |');
end;
pilakar(x1,x2,xc);
end;
writeln;
writeln('jawaban = ',xc:0:5);
end.
uses wincrt;
function f(x:real):real;
begin
f:=(((sqr(x))*x)+(sqr(x))-(3*(x))-3);
end;
function error(x1,x2:real):real;
var
err:real;
begin
err:=(((x2-x1)/x2)*100);
if err<0 then
error:=err*(-1)
else
error:=err;
end;
function hitxc(xa,xb:real):real;
begin
hitxc:=(xa+xb)/2;
end;
procedure pilakar(var xa,xb:real;xc:real);
begin
if (f(xa)*f(xc)<0 )then
begin
xb:=xc;
end
else
begin
xa:=xc;
end;
end;
function cek(xa,xb:real):boolean;
var
c:boolean;
cek1:real;
begin
c:=false;
cek1:=(f(xa)*f(xb));
if (cek1<0) then
begin
c:=true;
end;
cek:=c;
end;
procedure lanjut(var it:integer; var xc1,xc2:real;xc:real);
begin
it:=it+1;
xc1:=xc2;
xc2:=xc;
end;
var
x1,x2,emin,er,xc,xc1,xc2,a,b,c:real;
iterasi:integer;
cek2:boolean;
begin
repeat
begin
clrscr;
write('masukan x1 : ');readln(x1);
write('masukan x2 : ');readln(x2);
write('masukan error min : ');readln(emin);
cek2:=(cek(x1,x2));
if (cek2=false) then
begin
writeln('iterasi tidak dapat dilakukan');
write('tekan enter');readln;
end;
end;
until(cek2=true);
iterasi:=0;
er:=100;
xc1:=0;
xc2:=0;
clrscr;
writeln('|ite | x1 | x2 | f(x1) | f(x2) | xc | f(xc) | eror |');
while (er > emin) do
begin
xc:=hitxc(x1,x2);
a:=f(x1);
b:=f(x2);
c:=f(xc);
lanjut(iterasi,xc1,xc2,xc);
if (iterasi<2) THEN
writeln('| ',iterasi,' | ',x1:0:5,'| ',x2:0:5,'| ',a:0:5,'| ',b:0:5,'| ',x1:0:5,'| ',c:0:5,'| |')
else
begin
er:=error(xc1,xc2);
writeln('| ',iterasi,' | ',x1:0:5,'| ',x2:0:5,'| ',a:0:5,'| ',b:0:5,'| ',x1:0:5,'| ',c:0:5,'| ',er:0:5,' |');
end;
pilakar(x1,x2,xc);
end;
writeln;
writeln('jawaban = ',xc:0:5);
end.
No comments:
Post a Comment