|
-
Feb 1st, 2006, 08:01 PM
#1
Drawing Derivative Help
I'm learning calculus on my own pretty much, and even made a sample app on finding the easiest way to find a derivative of a polynomial. My program graphs the polynomial (which is a parabola), and the derivative (which is a straight line representing slope). When the line intersects the parabola, I have it draw green end points. But I want to be able to only draw the line within the 2 end points. Any ideas on how to do this? Here's my code:
Code:
Option Explicit
Private Type Point_2D
X As Single
Y As Single
End Type
Private Center As Point_2D
Private X As Single, Y As Single
Private X2 As Single, Y2 As Single
Private Current_X As Single
Private Initial As Point_2D
Private Pos As Point_2D, Pos2 As Point_2D
Private Scalar As Point_2D
Private Sub Form_Activate()
'Easiest way to find the derivative of a polynomial:
'---------------------------------------------------
'A derivative is the slope of a tangent line
'
'Formula for finding a derivative of X^n: n(x^(n - 1))
'
'Example:
'
'd/dx(x^2)
'= 2(X^(2-1))
'= 2X^1
'= 2X
'So the derivative of X^2 is 2X.
ScaleMode = 3
AutoRedraw = True
BackColor = RGB(0, 0, 0)
Center.X = ScaleWidth / 2
Center.Y = ScaleHeight / 2
Initial.X = Center.X
Initial.Y = Center.Y
Scalar.X = 25
Scalar.Y = 5
For Current_X = -100 To 100 Step 0.01
X = Current_X
Y = X ^ 2
Pos.X = Initial.X + X * Scalar.X
Pos.Y = Initial.Y - Y * Scalar.Y
PSet (Pos.X, Pos.Y), RGB(255, 0, 0)
'------------------------------------
X2 = Current_X
Y2 = 2 * X2
Pos2.X = Initial.X + X2 * Scalar.X
Pos2.Y = Initial.Y - Y2 * Scalar.Y
If Int(Pos.X) = Int(Pos2.X) And Int(Pos.Y) = Int(Pos2.Y) Then
'Draw end points for secant line.
PSet (Pos2.X, Pos2.Y), RGB(0, 255, 0)
Else
PSet (Pos2.X, Pos2.Y), RGB(255, 0, 0)
End If
Next Current_X
End Sub
-
Feb 1st, 2006, 09:32 PM
#2
Re: Drawing Derivative Help
I don't know if I understand it correctly.
the line is there, you mean you want to draw it JUST between that interception points and not outside?
Like this?
Code:
If Int(Pos.X) = Int(Pos2.X) And Int(Pos.Y) = Int(Pos2.Y) Then
'Draw end points for secant line.
PSet (Pos2.X, Pos2.Y), RGB(0, 255, 0)
Else
If Int(Pos.Y) > Int(Pos2.Y) Then 'Just inside the parabole
PSet (Pos2.X, Pos2.Y), RGB(255, 0, 0)
End If
End If
Last edited by jcis; Feb 1st, 2006 at 09:59 PM.
-
Feb 1st, 2006, 10:09 PM
#3
Re: Drawing Derivative Help
Yep that's what I want. Just between the parabola. Don't have VB on this comp to test it yet, but will give it a whirl soon.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|