Thema: ln-Funktion

Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#15

AW: ln-Funktion

  Alt 5. Feb 2014, 12:14
Eine Funktion sieht ja immer so aus
Code:
y = f(x)
Also brauchst du etwas, dass dir dieses f(x) abbildet.
Delphi-Quellcode:
type
  TPlotFunction = class
  public
    function Name : string; virtual;
    function f( x : double ) : double; virtual;
  end;
und du könntest dir dann solch eine Prozedur bauen
Delphi-Quellcode:
procedure Plot( ACanvas : TCanvas; AFunction : TPlotFunction );
var
  i, j : Integer;
  x, y : Double;
begin
  i := 220;
  while i < 420 do
    begin
      Inc (i);
      x := (i - 220) / 50;
      y := AFunction.f(x);
      j := Round (220 - y * 50);

      ACanvas.Pixels [i, j] := clTeal
    end;

  ACanvas.Font.Color := clTeal;
  ACanvas.TextOut (340, 405, AFunction.Name );
end;
Eine konkrete Funktion ist ja diese Ln(x)
Delphi-Quellcode:
type
  TLN_PlotFunction = class( TPlotFunction )
  public
    function Name : string; override;
    function f( x : double ) : double;
  end;

function TLN_PlotFunction.Name : string; override;
begin
  Result := 'y = ln(x)';
end;

function TLN_PlotFunction.f( x : double ) : double;
begin
  Result := ln(x);
end;
Deine ButtonClick-Methode würde dann so aussehen
Delphi-Quellcode:
procedure TForm7.Button1Click(Sender: TObject);
var
  LFunction : TLN_PlotFunction;
begin
  LFunction := TLN_PlotFunction.Create;
  try
    Plot( Image1.Canvas, LFunction );
  finally
    LFunction.Free;
  end;
end;
Und das mit der y = Log(a)x Funktion geht dann so
Delphi-Quellcode:
type
  TLOGA_PlotFunction = class( TPlotFunction )
  private
    Fa : Integer;
  public
    property a : Integer read Fa write Fa;
    function Name : string; override;
    function f( x : double ) : double;
  end;

function TLOGA_PlotFunction.Name : string; override;
begin
  Result := Format( 'y = log(%d)x',[a] );
end;

function TLN_PlotFunction.f( x : double ) : double;
begin
  Result := log(a)*x;
end;
und angewendet dann so
Delphi-Quellcode:
procedure TForm7.Button1Click(Sender: TObject);
var
  LFunction : TLOGA_PlotFunction;
begin
  LFunction := TLOGA_PlotFunction.Create;
  try
    LFunction.a := 3; // für f(x) = log(3)*x
    Plot( Image1.Canvas, LFunction );
  finally
    LFunction.Free;
  end;
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat