+ Reply to Thread
Results 1 to 11 of 11

VBA Program taylors formula

Hybrid View

tzgajner VBA Program taylors formula 12-21-2020, 08:06 AM
6StringJazzer Re: VBA Program that... 12-21-2020, 10:52 AM
6StringJazzer Re: VBA Program that... 12-21-2020, 11:15 AM
6StringJazzer Re: VBA Program that... 12-21-2020, 11:19 AM
6StringJazzer Re: VBA Program that... 12-21-2020, 11:22 AM
tzgajner Re: VBA Program that... 12-21-2020, 12:15 PM
6StringJazzer Re: VBA Program that... 12-21-2020, 12:24 PM
tzgajner Re: VBA Program that... 12-21-2020, 12:31 PM
6StringJazzer Re: VBA Program that... 12-21-2020, 03:26 PM
tzgajner Re: VBA Program that... 12-21-2020, 03:59 PM
MrShorty Re: VBA Program that... 12-21-2020, 03:31 PM
  1. #1
    Registered User
    Join Date
    10-29-2020
    Location
    Slovenia
    MS-Off Ver
    365
    Posts
    9

    Question VBA Program taylors formula

    Help!
    I have writen a program that calculates the sin(x) with taylors formula but it doesnt work the way i want. The program should read the x value from A2 (radians) and k value from B2 (number of decimal places) and write the result value in C2.
    Program should calculate the value of sin(x) with taylors formula to k decimal places until the decimals doesnt change anymore. I think the best is that program calculate to k+1 decimal places and then round off the result to k decimal places.
    Thank you guys for help.

    The excel document is attached.
    Attached Files Attached Files
    Last edited by tzgajner; 12-22-2020 at 03:33 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,991

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    Can you explain the overall objective? You are using the sin function to determine whether the Taylor function has converged to the desired precision. If you are using sin, why not just use sin to calculate the sine?

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,991

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    These declarations are probably not what you intend.

       Dim k, a, b, c, d, s, fakulteta, fakultet As Integer
       Dim i, f As Long
       Dim x, result, taylor As Double
    In VBA each variable must explicitly get a type, or it is Variant by default. I think you are assuming that the first line will all be Integer, the second line will all be Long, the third line will all be Double.

       Dim k As Integer, a As Integer, b As Integer, c As Integer, d As Integer, s As Integer, fakulteta As Integer, fakultet As Integer
       Dim i As Long, f As Long
       Dim x As Double, result As Double, taylor As Double

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,991

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    calculation of factorial can be done with a worksheet function

          fakulteta = Application.WorksheetFunction.Fact(i)
    and the result should be assigned to a Long. You are getting overflow, which you did not mention in your question. Actually, your post is not a question--not sure what your question is.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,991

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    Please try this:
    Sub sinus_taylor()
    
       Dim k As Integer, a As Integer, b As Integer, c As Integer, d As Integer, s As Integer
       Dim i As Long, f As Long, fakulteta As Long, fakultet As Long
       Dim x As Double, result As Double, taylor As Double
       'program should read the x value from A2 (radians) and number of decimals from B2 and write the result of sin(x) by taylors formula in C2.
       'i have problem because i dont know how to change do until ... so program would calculate until decimals (k+1) stop changing and after that end loop and round the result on k decimals
       'problem is also overflow
       
       x = Cells(2, 1) 'the x
       k = Cells(2, 2) 'number od decimals
       i = 1
       f = 3
       taylor = 0
       
       Do Until Mid(taylor, (k - 3), 4) = Mid(Sin(x), (k - 3), 4)
       
          If x = 0 Then
             Exit Do
          End If
          'factorial
          fakulteta = Application.WorksheetFunction.Fact(i)
          'factorial
          fakultet = Application.WorksheetFunction.Fact(f)
          
          'taylors formula for sin(x) = ((x ^ 1) / 1!) - ((x ^ 3) / 3!) + ((x ^ 5) / 5!) - ((x ^ 7) / 7!)
          taylor = taylor + (((x ^ i) / (fakulteta)) - ((x ^ f) / (fakultet)))
          
          i = i + 4
          f = f + 4
          
       Loop
       
       result = Round(taylor, k)
       Cells(2, 3) = result 'cell where the result must be written
    
    End Sub

  6. #6
    Registered User
    Join Date
    10-29-2020
    Location
    Slovenia
    MS-Off Ver
    365
    Posts
    9

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    Thank you, but the problem is overflow as you said. Do you know how to fix this. I have to calculate the sin(x) with taylors formula to k decimal places (i must not use sin(x) like i did in macro).

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,991

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    The code I posted does not overflow.

  8. #8
    Registered User
    Join Date
    10-29-2020
    Location
    Slovenia
    MS-Off Ver
    365
    Posts
    9

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    yea thank you a lot. But do you know how would i fix Do until so the program would calculate until k decimal places doesnt change anymore (without comparing to sin(x) like i did).

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,991

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    You monitor the change each time until it falls below the threshold. Also I had to redeclare the factorials as Double to prevent overflow.

    So why are you even doing this when you could just use SIN?

    Sub sinus_taylor2()
    
       Dim k As Integer, a As Integer, b As Integer, c As Integer, d As Integer, s As Integer
       Dim i As Long, f As Long, fakulteta As Double, fakultet As Double
       Dim x As Double, result As Double, taylor As Double
       Dim Change As Double
       Dim LastValue As Double
       'program should read the x value from A2 (radians) and number of decimals from B2 and write the result of sin(x) by taylors formula in C2.
       'i have problem because i dont know how to change do until ... so program would calculate until decimals (k+1) stop changing and after that end loop and round the result on k decimals
       'problem is also overflow
       
       x = Cells(2, 1) 'the x
       k = Cells(2, 2) 'number od decimals
       i = 1
       f = 3
       taylor = 0
       Change = 1 ' need a large number for first pass through loop
       
       Do Until Change <= 10 ^ -k  ' Mid(taylor, (k - 3), 4) = Mid(Sin(x), (k - 3), 4)
          If x = 0 Then
             Exit Do
          End If
          'factorial
          fakulteta = Application.WorksheetFunction.Fact(i)
          'factorial
          fakultet = Application.WorksheetFunction.Fact(f)
          
          'taylors formula for sin(x) = ((x ^ 1) / 1!) - ((x ^ 3) / 3!) + ((x ^ 5) / 5!) - ((x ^ 7) / 7!)
          Change = Abs(taylor - (taylor + (((x ^ i) / (fakulteta)) - ((x ^ f) / (fakultet)))))
          
          taylor = taylor + (((x ^ i) / (fakulteta)) - ((x ^ f) / (fakultet)))
          
          i = i + 4
          f = f + 4
          
       Loop
       
       result = Round(taylor, k)
       Cells(2, 3) = result 'cell where the result must be written
    
    End Sub

  10. #10
    Registered User
    Join Date
    10-29-2020
    Location
    Slovenia
    MS-Off Ver
    365
    Posts
    9

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    thank you a lot, you are the best. I know i could use just sin(x) but i got instructions not to because that program is 1/10 of my final exam at programming, im studying civil engineering and i agree with you :D.
    i am really grateful for your time, have a nice day.
    Last edited by tzgajner; 12-21-2020 at 04:01 PM.

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,438

    Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

    But do you know how would i fix Do until so the program would calculate until k decimal places doesnt change anymore
    The way I usually do this is to store the result of the previous loop and the result of the current loop, and then compare them. You can see how I tend to structure this in the VBA UDFs that I included in my Newton-Raphson tutorial (post #9 https://www.excelforum.com/tips-and-...ml#post4688682 ) (one procedure included here):
    Function NRcuberoot(xi As Double, a As Double, b As Double, c As Double, d As Double) As Double
    'Because this receives parameters as doubles, it should be faster than the other function.
    'However, it is limited to cubic (or smaller) polynomials. It is not flexible.
    Dim xnew As Double, xold As Double, f As Double, df As Double
    Dim i As Integer
    'initialize loop
    xnew = xi 'your Taylor series will start at 0
    For i = 1 To 100 'max iteration is 100
        xold = xnew 'store result from previous iteration (or initialized value if 1st time through)
        f = a + b * xnew + c * xnew ^ 2 + d * xnew ^ 3 'this is where your
        df = b + 2 * c * xnew + 3 * d * xnew ^ 2 'Taylor series calculations would go
        xnew = xnew - f / df 'result from current loop
        'If converged exit loop and finish
        If Abs(xnew - xold) < 0.000001 Then Exit For 'your convergence test would somehow need to include k
    Next i
    'normally I would include a block to return an error if the loop did not converge (if i>=100)
    NRcuberoot = xnew
    End Function
    Does that help?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 07-29-2020, 08:33 AM
  2. I need excel program which calculate perfomance.
    By max2598 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-21-2019, 05:17 AM
  3. Calculate Net Funding for Bonus Program
    By scruz9 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-14-2017, 12:47 AM
  4. Calculate distance ran using time vs program
    By cls1983 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-21-2013, 11:52 AM
  5. Automatically Calculate Race Places
    By emmsee_two in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-23-2007, 12:42 AM
  6. [SOLVED] Program to calculate how much I can afford to spend?
    By Looking to start over in forum Excel General
    Replies: 3
    Last Post: 11-30-2005, 06:15 PM
  7. Replies: 1
    Last Post: 04-13-2005, 03:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1