Page 1 of 1

Calculus Lesson, Tangents to curves

Posted: Thu Jan 22, 2015 3:00 pm
by applePi
this short code display the function f(x) = x^2 , and then will find the curve slopes at any given point. when the slope approaches the curve new turn it slow down (like a car on the road turns) then change its direction and speeding up the curve.
how to find the curve slope (the Tangent angle) at a specific point on the curve?. we need the derivative of the function. such as in f(x)=x^2 the derivative of x^2 = 2*x.
if we don't know we look this universal calculator machine :
http://www.wolframalpha.com/examples/Derivatives.html
just write: derivative of sin(x) and it will give you Cos(x)
write derivative of x^3 and it will give you 3*x^2
the essential code is:

Code: Select all

x.f + stepx
y.f = x*x
z.f=0
slope.f = 2*x ; this is the derivative of f(y)=x^2
slope = Degree(ATan(slope)) 
MoveEntity(200,x,y, z,   #PB_Absolute)
RotateEntity(200, 0,0,slope)
Image

Code: Select all

Declare DrawPoints()

#CameraSpeed = 1
;the range of the function under study is from xMin to xMax
Global xMin.f = -2*#PI
Global xMax.f =  2*#PI
Global stepx.f = (xMax - xMin) / 1000 ; the increment of x every loop

Enumeration
  #mesh
  #light
  #camera
  #plane
  
EndEnumeration

ExamineDesktops()

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"curve tangent, .. press space key to start/stop",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)

CreateMaterial(1, LoadTexture(1, "snow_1024.jpg"))
MaterialCullingMode(1, #PB_Material_NoCulling)
CreatePlane(#plane, 200, 200, 50, 50, 10, 10)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0,-16,0)

CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,-5,30,43,#PB_Absolute)
CameraLookAt(0, 0, 10, 0)
CameraBackColor(0, RGB(227,239,242))

CreateText3D(0, "I'm Waiting")
Text3DColor(0, RGBA(150, 0, 250, 255))
Text3DAlignment(0, #PB_Text3D_HorizontallyCentered)
ScaleText3D(0, 2,2,2,#PB_Absolute)


AmbientColor(RGB(255,255,255))

;temp=1

CreateLine3D(#PB_Any, -20, 0, 0, #Red, 20, 0, 0, RGB(255, 0, 0)) ; Red 
CreateLine3D(#PB_Any, 0,-20,0, #Green, 0, 20, 0, RGB(0, 255, 0)); Green 
CreateLine3D(#PB_Any, 0,0,-20, #Blue, 0, 0, 20, RGB(0, 0, 255)) ;Blue 
;-Mesh
DrawPoints()


CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(255,0,0)) ; red color
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)
  
CreateCube(200, 4) ; make the slope , then scale it like a line
CreateEntity(200, MeshID(200), MaterialID(2) )
ScaleEntity(200, 1,0.1,0.1)
MoveEntity(200, -2*#PI, 0, 0, #PB_Absolute)


x.f = -2*#PI
start = 0
Repeat
  Repeat
    event = WindowEvent()
  Until event = 0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
             
      If KeyboardReleased(#PB_Key_Space)   
        start ! 1
      EndIf
      
      If start
        
          x.f + stepx
          y.f = x*x
          z.f=0
          slope.f = 2*x ; this is the derivative of f(y)=x^2
          slope = Degree(ATan(slope)) 
          
          MoveEntity(200,x,y, z,   #PB_Absolute)
          RotateEntity(200, 0,0,slope)
          
          If x > 2*#PI
            x = -2*#PI
          EndIf 
          
          MoveText3D(0, 0, -50, 0, #PB_Absolute) ;hide the text underground when slope is busy
        Else
          MoveText3D(0, x, y+4, 0, #PB_Absolute) ; move the text near the slope when waiting
    EndIf
    
   
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;main drawing routine
Procedure DrawPoints()
  Protected.f x, y
    
  CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
  
  x.f = xMin: z.f = 0
      
    While x <= 2*#PI
      
      y.f = Pow(x,2) ; the function we want to study its tangent at several points
                      
      MeshVertexPosition(x, y, z) 
      MeshVertexColor(RGB(0,0,255))
      
      x.f + stepx
    Wend
    
    FinishMesh(#True)
    CreateEntity(0, MeshID(0), MaterialID(0) ,0,0,0)
    
EndProcedure  

the next code is the same above but for f(x) = Sin(x) . i have amplified x,y by 3
Image

Code: Select all

Declare DrawPoints()

#CameraSpeed = 1
;the range of the function under study is from xMin to xMax
Global xMin.f = -2*#PI
Global xMax.f =  2*#PI
Global stepx.f = (xMax - xMin) / 1000 ; the increment of x every loop

Enumeration
  #mesh
  #entity
  #tex
  #light
  #camera
  #plane
  
EndEnumeration

ExamineDesktops()

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"curve tangent, .. press space key to start/stop",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)

CreateMaterial(1, LoadTexture(1, "snow_1024.jpg"))
MaterialCullingMode(1, #PB_Material_NoCulling)
CreatePlane(#plane, 200, 200, 50, 50, 10, 10)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0,-16,0)

CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,-50,30,33,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, RGB(227,239,242))

AmbientColor(RGB(255,255,255))

CreateLine3D(#PB_Any, -20, 0, 0, #Red, 20, 0, 0, RGB(255, 0, 0)) ; Red 
CreateLine3D(#PB_Any, 0,-20,0, #Green, 0, 20, 0, RGB(0, 255, 0)); Green 
CreateLine3D(#PB_Any, 0,0,-20, #Blue, 0, 0, 20, RGB(0, 0, 255)) ;Blue 
;-Mesh
DrawPoints() ; call the function drawing procedure

CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(255,0,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)
  
CreateCube(200, 4) ; make the slope , then scale it like a line
CreateEntity(200, MeshID(200), MaterialID(2) )
ScaleEntity(200, 1,0.1,0.1)
MoveEntity(200, -2*#PI*3, Sin(-2*#PI)*3, 0, #PB_Absolute)


x.f = xMin
start = 0
Repeat
  Repeat
    event = WindowEvent()
  Until event = 0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
             
      If KeyboardReleased(#PB_Key_Space)   
        start ! 1
      EndIf
      
      If start
        
          x.f + stepx
          y.f = Sin(x)
          z.f=0
          slope.f= Cos(x) ; the derivative of sin(x) is cos(x)
          slope = Degree(ATan(slope))
          
          MoveEntity(200,x*3,y*3, z,   #PB_Absolute) ; we just amplify x,y by 3
          RotateEntity(200, 0,0,slope)
          
          If x > xMax
            x =  xMin
          EndIf  
              
      EndIf
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;function drawing
Procedure DrawPoints()
  Protected.f x, y
    
  CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
    
  x.f = xMin: z.f = 0
  
    While x <= 2*#PI
      
      y.f = Sin(x)
                      
      MeshVertexPosition(x*3, y*3, z) 
      MeshVertexColor(RGB(0,0,255))
      
      x.f + stepx
    Wend
        
    FinishMesh(#True)
    CreateEntity(#entity, MeshID(0), MaterialID(0) ,0,0,0)
    
EndProcedure  

another interesting function x*Sin(x*x)+1 , from wolfram alpha its derivative is Sin(x*x)+2*x*x*Cos(x*x)

Code: Select all

Declare DrawPoints()

#CameraSpeed = 1
;the range of the function under study is from xMin to xMax
Global xMin.f = -2
Global xMax.f =  4
Global stepx.f = (xMax - xMin) / 1000 ; the increment of x every loop

Enumeration
  #mesh
  #entity
  #tex
  #light
  #camera
  #plane
  
EndEnumeration

ExamineDesktops()

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"curve tangent, .. press space key to start/stop",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)

CreateMaterial(1, LoadTexture(1, "snow_1024.jpg"))
MaterialCullingMode(1, #PB_Material_NoCulling)
CreatePlane(#plane, 200, 200, 50, 50, 10, 10)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0,-16,0)

CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,10,33,#PB_Absolute)
CameraLookAt(0, 0, 5, 0)
CameraBackColor(0, RGB(227,239,242))

AmbientColor(RGB(255,255,255))

CreateLine3D(#PB_Any, -20, 0, 0, #Red, 20, 0, 0, RGB(255, 0, 0)) ; Red 
CreateLine3D(#PB_Any, 0,-20,0, #Green, 0, 20, 0, RGB(0, 255, 0)); Green 
;CreateLine3D(#PB_Any, 0,0,-20, #Blue, 0, 0, 20, RGB(0, 0, 255)) ;Blue 
;-Mesh
DrawPoints() ; call the function drawing procedure

CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(255,0,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)
  
CreateCube(200, 4) ; make the slope , then scale it like a line
CreateEntity(200, MeshID(200), MaterialID(2) )
ScaleEntity(200, 1,0.01,0.1)
MoveEntity(200, -2*3, 0, 0, #PB_Absolute)


x.f = xMin
start = 0
Repeat
  Repeat
    event = WindowEvent()
  Until event = 0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
             
      If KeyboardReleased(#PB_Key_Space)   
        start ! 1
      EndIf
      
      If start
        
          x.f + stepx
          y.f = x*Sin(x*x)+1
          z.f=0
          slope.f= Sin(x*x)+2*x*x*Cos(x*x) ; the derivative of sin(x) is cos(x)
          slope = Degree(ATan(slope))
          
          MoveEntity(200,x*3,y*3, z,   #PB_Absolute) ; we just amplify x,y by 3
          RotateEntity(200, 0,0,slope)
          
          If x > xMax
            x =  xMin
          EndIf  
              
      EndIf
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;function drawing
Procedure DrawPoints()
  Protected.f x, y
    
  CreateMesh(0, #PB_Mesh_LineStrip  , #PB_Mesh_Static)
    
  x.f = xMin: z.f = 0
  
    While x <= 4
      
      ;y.f = Sin(x)
      y.f = x*Sin(x*x)+1
                      
      MeshVertexPosition(x*3, y*3, z) 
      MeshVertexColor(RGB(0,0,255))
      
      x.f + stepx
    Wend
        
    FinishMesh(#True)
    CreateEntity(#entity, MeshID(0), MaterialID(0) ,0,0,0)
    
EndProcedure  
Image

i forgot Calculus COMPLETELY and now trying to learn again the introduction, here is a list of some of the introductory books for who may find it useful
Bob Miller's High School Calc for the Clueless
Calculus For The Utterly Confused
Calculus Know-It-ALL
Everyday Calculus_ Discovering the Hidden Math All around Us
Mathematics - Teach Yourself Visually Calculus - (Dale Johnson) Wiley 2008
The Britannica Guide to Analysis and Calculus
The Everything Guide to Calculus I
The Manga Guide to Calculus : Cartoon book
An Approach to CALCULUS (online version) http://www.themathpage.com/acalc/calculus.htm
Calculus Demystified
Calculus Made Easy :SILVANUS P. THOMPSON + Martin Gardner
Calculus Essentials For Dummies
Calculus For Dummies
Calculus II For Dummies
Paradoxes and Sophisms in Calculus
The Calculus Direct: An intuitively Obvious Approach to a Basic Understanding of the Calculus for the Casual Observer
The Complete Idiot's Guide to Calculus, 2nd Ed
Calculus for Computer Graphics :John Vince :Springer-Verlag London 2013
Calculus simpleSneesyBook
Workshop Calculus with Graphing Calculators
Zombies and Calculus
E-Z Calculus : Douglas Downing
Calculus the Easy Way : Douglas Downing
What Is Calculus About: w.w. Sawyer
Discovering the Art of Mathematics: Calculus (free ebook): https://www.artofmathematics.org/books/calculus
A Gentle Introduction To Learning Calculus: http://betterexplained.com/articles/a-g ... -calculus/
Calculus for the Ambitious
From calculus to chaos
Higher Maths for Beginners – Zeldovich, Yaglom , this book is from the soviet era. many books from that era available for free from http://mirtitles.org
Calculus with Applications: Margaret L. Lial
THE MATHEMATICAL MECHANIC using physical reasoning to solve problems , Mark Levi

Re: Calculus Lesson, Tangents to curves

Posted: Thu Jan 22, 2015 4:04 pm
by T4r4ntul4
Very nice examples applePi!

made something similar lately in the canvas gadget, it gives a curve (with Acos()) between 2 choosen points in the gadget. it then plots the curve between those points.

Re: Calculus Lesson, Tangents to curves

Posted: Thu Jan 22, 2015 4:32 pm
by Comtois
you can try with #PB_Mesh_LineStrip

Code: Select all

CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static)

Re: Calculus Lesson, Tangents to curves

Posted: Thu Jan 22, 2015 8:07 pm
by applePi
Thanks T4r4ntul4 and Comtois ,
yes with #PB_Mesh_LineStrip the curves appears more continuous