Nugroho's blog.: [Delphi] Differential of Function (just for self documentation)

Wednesday, June 4, 2014

[Delphi] Differential of Function (just for self documentation)

Here the code, it have the graph of function and plot of it diferential (gradien) line



unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;

type
TForm1 = class(TForm)
Image1: TImage;
Timer1: TTimer;
procedure proses;
procedure awal;
function f(x:real):real;
function df(x:real):real;
function fs(x,xs,ys,m:real):real;
procedure gambar;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
dx:real=0.01;
x0,y0:integer;
implementation

{$R *.dfm}

procedure tform1.awal;
begin
{gambar sumbu}
x0:=round(image1.Width/2);
y0:=round(image1.Height/2);
with image1.Canvas do begin
moveto(0,y0);
lineto(image1.Width,y0);
moveto(x0,0);
lineto(x0,image1.Height);
end;
end;
function tform1.f(x:real):real;
begin
f:=10*cos(x)+x;
end;
function tform1.df(x:real):real;
begin
df:=(f(x+dx)-f(x))/dx;
end;
function tform1.fs(x,xs,ys,m:real):real;
var c:real;
begin
c:=ys-m*xs;
fs:=m*x+c;
end;

procedure tform1.gambar;
var i:integer;
x,y,x1,y1,m:real;
begin
// image1.Canvas.TextOut(0,0,'f(x)=x^2-2x-8 memiliki gradien '+floattostrf(df(0),ffnumber,8,2)+' di x=0');
image1.Canvas.TextOut(0,0,'f(x)=x^2-2x-8 memiliki gradien '+format('%8.2f',[df(0)])+' di x=0');
with image1.Canvas do begin
{gambar fungsi}
for i:=-200 to 200 do begin
x:=i/8;
y:=f(x);
x1:=(i+1)/8;
y1:=f(x1);
pen.Color:=clblue;
moveto(x0+i,y0-round(y));
lineto(x0+i+1,y0-round(y1));
end;
{gambar garis singgung}
m:=df(0);
for i:=-200 to 200 do begin
x:=i/8;
y:=fs(x,0,f(0),m);
x1:=(i+1)/8;
y1:=fs(x1,0,f(0),m);
pen.Color:=cllime;
moveto(x0+i,y0-round(y));
lineto(x0+i+1,y0-round(y1));
end;
end;
end;
procedure tform1.proses;
begin
awal;
gambar;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
timer1.Enabled:=false;
proses;
end;

end.

No comments:

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)