Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   ln-Funktion (https://www.delphipraxis.net/178880-ln-funktion.html)

Rebel 4. Feb 2014 18:48

AW: ln-Funktion
 
Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);   //Koordinatensystem
var i: integer;
begin
  with image1.Canvas do
  begin
  moveto(20,220); lineto(440,220);          //x-Achse
  moveto(220,5); lineto(220,420);          //y-Achse
  moveto(430,215); lineto (442,220);
  lineto (430,225);                         //x-Pfeil
  moveto(215,15); lineto (220,5);
  lineto (225,15);                          //y-Pfeil
  textout(445,225,'x');   //Beschriftung x
  textout(200,0,'y');   //Beschriftung y
    for i:=-10 to 10 do
    begin
    moveto(220+50*i,215);
    lineto (220+50*i,225); //x-Einteilung
    moveto(215,220+50*i);
    lineto (225,220+50*i); //y-Einteilung
    textout(222+50*i,225,inttostr(i)); //Zahlen x-Achse
    if i <> 0 then
    textout (205,213+50*i,inttostr(-i)); //Zahlen y-Achse
    end;
  end;

procedure TForm1.Button7Click(Sender: TObject);     //Logarithmusfunktion, die nicht klappt :(
var float:Double; x,y:real; i,j:integer;

begin
 Val(edit1.Text,a,f1);

begin
  float:= Ln(a);
  float := Exp(float);
  i:=20;
  while i<420 do
    begin
    i:=i+1;
    x:=(i-220)/50; //x skalieren
    y:=float; //y sklarieren                    //Befehl Quelle: [url]http://www.physik-multimedial.de/cvpmm/sle/trigonometrie/sinusfunktion.html[/url]
    j:=round((220-y*50)); //j berechnen
    image1.Canvas.Pixels[i,j]:=clteal; //zeichnen
    image1.Canvas.Font.Color:=clteal;
    image1.Canvas.TextOut(340,405, 'y=ln(a)');
end;
end;
end;
Ich glaube mein Befehl ist einfach falsch, aber ich weiß halt nicht, wie ich ihn richtig schreiben soll

Rebel 4. Feb 2014 19:18

AW: ln-Funktion
 
PS: ich habe mich dazu entschieden aus der logarithmusfunktion eine exponentialfunktion mit f(x)=a^x zu machen. wäre toll, wenn hr mir dabei helfen würdet ^^

Volker Z. 4. Feb 2014 20:50

AW: ln-Funktion
 
Hallo,

Zitat:

Ich glaube mein Befehl ist einfach falsch [...]
Naja, wenn Du den Graphen y = ln (a), für festes a > 0 im Interval [-4; 4] zeichnen möchtest, dann machst Du schon alles richtig - zwar sehr umständlich, aber richtig; es ist doch eine konstante Funktion.

Wenn Du hingegen y = ln (x) ∀ x ∈ R: 0 < x ≤ 4 plotten möchtest, dann sollte die Berechnung des jeweiligen Funktionswerts schon in der while-Schleife erfolgen. Also so:
Delphi-Quellcode:
procedure TForm7.Button1Click(Sender: TObject);
var
  c   : TCanvas;
  i, j : Integer;
  x, y : Double;
begin
  c := Image1.Canvas;
  i := 220;
  while i < 420 do
    begin
      Inc (i);
      x := (i - 220) / 50;
      y := Ln (x);
      j := Round (220 - y * 50);

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

  c.Font.Color := clTeal;
  c.TextOut (340, 405, 'y = ln (x)')
end;
Zitat:

PS: ich habe mich dazu entschieden aus der logarithmusfunktion eine exponentialfunktion mit f(x)=a^x zu machen [...]
f(x) = exp(x) geht dann ähnlich.

Gruß

Rebel 5. Feb 2014 12:07

AW: ln-Funktion
 
ich danke dir :)
Endlich siehts gut aus und vor allem wie eine ln-funktion ;)

wie formuliere ich das aber, dass ich f(x)=log(basis a)x (a element R, a>, a ungleich 1) bekomme?. Quasi, dass der user selbst den parameter a bestimmen kann?

Sir Rufo 5. Feb 2014 13:14

AW: ln-Funktion
 
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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 06:18 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz