3 Replies - 2150 Views - Last Post: 02 April 2011 - 05:34 AM Rate Topic: -----

#1 Googler20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-April 11

VB 10 Derivative Calculating

Posted 01 April 2011 - 07:13 PM

Hello, im currently working on a program to have a program produce a derivative of a function.
 Sub derivative()
        Dim firstx, h, df, f, x, fprime As Double
        Dim a, b As Double

        x = 1000
        h = 1 / 45555555
        firstx = x
        x = x + h
        df = -0.0002 * x * x * x + 0.0003 * (a + B)/> * x * x - 0.0006 * a * b * x + 10
        x = firstx
        f = -0.0002 * x * x * x + 0.0003 * (a + B)/> * x * x - 0.0006 * a * b * x + 10
        fprime = (df - f) / h
        MsgBox(fprime)
    End Sub 


This is the code that I have. The function I am trying to get the derivative for is g(x) = -2x^3 + 3(a+b)x^2 -6abx + 10. In my program a and b will be given by the user and than the derivative will be computed. I am not quite sure if I have done this correctly.

Also, I would like the program to output the Maximum and Minimum of the function g(x) on the interval (0,1). I know I will need to use a function for each max and min.
The way I have done this is:

 Function Min(x As Variant, y As Variant) As Variant
If x > y Then
      Min = y
 Else
      Min = x
End If
End Function

Function Max(x As Variant, y As Variant) As Variant
If x < y Then
    Max = y
 Else
    Max = x
End If
End Function 


where x and y are the critical points of g(x).
Have I done this correctly?

I am also a little stuck on how to combine these codes into one so that with the push of a button the derivative is done and a max and min produced.
Any suggestions on how to go about doing this?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: VB 10 Derivative Calculating

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,090
  • Joined: 02-July 08

Re: VB 10 Derivative Calculating

Posted 01 April 2011 - 08:20 PM

Your variablesa and b have no value, your adding them so they probably should. Functions return the type specified. Have you debugged your code to check the values?
Was This Post Helpful? 0
  • +
  • -

#3 Googler20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-April 11

Re: VB 10 Derivative Calculating

Posted 01 April 2011 - 08:43 PM

View Posthawkvalley1, on 01 April 2011 - 08:20 PM, said:

Your variablesa and b have no value, your adding them so they probably should. Functions return the type specified. Have you debugged your code to check the values?


I probably should have added the beginning of my code where the values a and b are added.

  Dim g As Graphics
        Dim PBlack As New Pen(Color.Black)
        Dim PCoral As New Pen(Color.Coral)
        Dim PBlue As New Pen(Color.SkyBlue)
        Dim sdBrush As SolidBrush = New SolidBrush(Color.Honeydew)
        Dim a, b As Double
        Dim step1 As Double

        g = PictureBox1.CreateGraphics

        xmin = 0
        xmax = 1
        ymax = 1
        ymin = 0
        a = TextBox1.Text
        b = TextBox2.Text
        step1 = 0.01
        g.Clear(Color.Gainsboro)

        For i = 0 To 1 Step step1
            g.DrawLine(PCoral, xc(i), yc(f(i, a, B)/>), xc(i + step1), yc(f(i + step1, a, B)/>))

            g.DrawLine(PBlack, xc(0), yc(0), xc(1), yc(1))
            g.DrawLine(PBlack, xc(0), yc(0), xc(1), yc(0))
            g.DrawLine(PBlack, xc(0), yc(0), xc(0), yc(1))
        Next
        Dim output As String
        
        output = "" 


This code is for creating the graph of my g(x). I have the code for the derivative as a sub procedure with in this.
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: VB 10 Derivative Calculating

Posted 02 April 2011 - 05:34 AM

Sometimes writing it out helps.

        'g(x) = -2x^3 + 3(a+b)x^2 -6abx + 10
        'with a = 1 and b =2 find g(3)

        Dim x As Double = 3
        Dim a As Double = 1
        Dim b As Double = 2

        Dim xSq As Double = x ^ 2
        Dim xCube As Double = x ^ 3
        Dim t1 As Double = -2 * xCube
        Dim t2 As Double = 3 * (a + B)/> * xSq
        Dim t3 As Double = 6 * a * b * x

        Dim gx As Double = t1 + t2 - t3 + 10


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1