Grafische Differentation

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
Olaf
Beiträge: 117
Registriert: 20.04.2006 14:51
Wohnort: 66606 St.Wendel (Niederlinxweiler, Dr.Albert-Schweitzer-Str.9)
Kontaktdaten:

Grafische Differentation

Beitrag von Olaf »

Das is'n sehr unmathematischer Code für'n sehr mathematisches Thema
Eigentlich nur so'n Test, aber ganz lustig und wenn man keine mathematischen, genauen Daten von der Funktion braucht, reichts.
(zum kucken, wie's aussieht und so.) :-)

Code: Alles auswählen

Enumeration
	#window
	#gadget
	#image
EndEnumeration
e.d=Pow(1+1/100000,100000)
Global delta_x.d=1/32,y1.d,y2.d,x1.d,x2.d,x.d
max=256*delta_x
Global Dim f1x.d(514),Dim f2x.d(514)
Global Dim a1x.d(513),Dim a2x.d(513)
Global Dim b1x.d(512),Dim b2x.d(512)

Macro Function(x)
	x*x*x-x
EndMacro

x1=-8
For n=0 To 514
	x2=x1+delta_x
	y1=Function(x1)
	y2=Function(x2)
	f1x(n)=y1
	f2x(n)=y2
	x1=x1+delta_x
Next n

For n=0 To 513
	a1x(n)=(f2x(n)-f1x(n))/delta_x
	a2x(n)=(f2x(n+1)-f1x(n+1))/delta_x
Next n

For n=0 To 512
	b1x(n)=(a2x(n)-a1x(n))/delta_x
	b2x(n)=(a2x(n+1)-a1x(n+1))/delta_x
Next n

OpenWindow(#window,0,0,512,512,"Grafische Ableitung",#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
CreateImage(#image,512,512)
	StartDrawing(ImageOutput(#image))
	Box(0,0,512,512)
	LineXY(0,256,512,256,RGB(255,255,255))
	LineXY(256,0,256,512,RGB(255,255,255))
		For n=0 To 512
			;Funktionsgraph
			If (f1x(n)<max And f1x(n)>-max) Or (f1x(n)<max And f2x(n)>max) Or (f1x(n)<-max And f2x(n)>-max) Or (f1x(n)>max And f2x(n)<max) Or (f1x(n)>-max And f2x(n)<-max)
				LineXY(n,256-f1x(n)*32,n+1,256-f2x(n)*32,RGB(0,0,255))
			EndIf
			;Maxima
			If (f1x(n)<f1x(n+1) And f1x(n+1)>f1x(n+2)) Or (f1x(n)>f1x(n+1) And f1x(n+1)<f1x(n+2))
				DrawingMode(4)
				Circle(n+1,256-f1x(n+1)*32,4,RGB(255,0,255))
				DrawingMode(0)
			EndIf
			;Nullstellen
			If (Abs(f1x(n))=0 Or (f1x(n)>0 And f2x(n)<0) Or (f1x(n)<0 And f2x(n)>0))
				DrawingMode(4)
				Circle(n,256,4,RGB(0,255,255))
				DrawingMode(0)
			EndIf
			;1.Ableitung
			If (a1x(n)<max And a1x(n)>-max) Or (a1x(n)<max And a2x(n)>max) Or (a1x(n)<-max And a2x(n)>-max) Or (a1x(n)>max And a2x(n)<max) Or (a1x(n)>-max And a2x(n)<-max)
				LineXY(n,256-a1x(n)*32,n+1,256-a2x(n)*32,RGB(255,0,0))
			EndIf
			;2.Ableitung
			If (b1x(n)<max And b1x(n)>-max) Or (b1x(n)<max And b2x(n)>max) Or (b1x(n)<-max And b2x(n)>-max) Or (b1x(n)>max And b2x(n)<max) Or (b1x(n)>-max And b2x(n)<-max)
				LineXY(n,256-b1x(n)*32,n+1,256-b2x(n)*32,RGB(0,255,0))
			EndIf
		Next n
	DrawText(5,5,"Funktionsgraph",RGB(0,0,255),0)
	DrawText(5,21,"1.Ableitung",RGB(255,0,0),0)
	DrawText(5,37,"2.Ableitung",RGB(0,255,0),0)
	DrawText(5,53,"Maxima",RGB(255,0,255),0)
	DrawText(5,69,"Nullstellen",RGB(0,255,255),0)
	StopDrawing()
	CreateGadgetList(WindowID(#window))
	ImageGadget(#gadget,0,0,512,512,ImageID(#image))
Repeat
event=WaitWindowEvent()
Until event=#PB_Event_CloseWindow
PB 4.xx (Windows & Linux & Mac OS X)
Treffen sich 2 Kurven in der Unendlichkeit. Sagt die eine zur anderen: "Hau ab aus meinem Definitionsbereich oder ich leite dich ab!"
Darauf die andere: "Mach nur! Ich bin die e-Funktion." :lol: