# VBA Program taylors formula

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

2. ## 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. ## 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. ## 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. ## Re: VBA Program that calculate the value of sin(x) with taylors formula on k decimalal pla

``Please Login or Register  to view this content.``

6. ## 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. ## 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. ## 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. ## 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. ## 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?

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

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