+ Reply to Thread
Results 1 to 11 of 11

VBA Program taylors formula

  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
    MS365 Family 64-bit
    Posts
    24,531

    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
    MS365 Family 64-bit
    Posts
    24,531

    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.

    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.

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

    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

    Please Login or Register  to view this content.
    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
    MS365 Family 64-bit
    Posts
    24,531

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

    Please try this:
    Please Login or Register  to view this content.

  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
    MS365 Family 64-bit
    Posts
    24,531

    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
    MS365 Family 64-bit
    Posts
    24,531

    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?

    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,802

    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):
    Please Login or Register  to view this content.
    Does that help?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  11. #11
    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.

+ 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. [SOLVED] goal seek wont calculate an accurate value past 3 decimal places
    By Joe Browning in forum Excel General
    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