# Thread: Cannot debug the calculation. been trying for 2 hours

1. ## Cannot debug the calculation. been trying for 2 hours

Trying to code a vb console application that calculates sin(x). But it compares the approximated value of sin x with an intrinsic function of sin(x) and gives the error as a percentage. There is something wrong with the calculation that i cannot crack. Any help would be appreciated.

Code:
```Module Module1
'The main
Sub Main()
'The variables
Dim x As Integer
Dim n As Integer
Dim approximated_Value As Double
Dim i As Integer
Dim correctValue As Double
Dim Err As Double

'tell user to input values of x and n
Console.WriteLine("Enter the value of x")

'convert the input value of x from degrees to radians
xradian = x * Math.PI / 180

Console.WriteLine("Enter the value of n")

Console.WriteLine("approximated value and error as a percentage: ")
Console.WriteLine(" ")
approximated_Value = 0

'for next loop to calculate the approximated value and the error
For i = 0 To n
approximated_Value = ((-1) ^ i) * (x ^ (2 * i + 1)) / Factorial(2 * i + 1) 'calculate the approximated value

Err = ((((correctValue - approximated_Value) / correctValue) ^ 2) ^ 1 / 2) * 100  'calculate the error

Next
Console.WriteLine("approximated value =" & approximated_Value) 'Output result for the approximated value
Console.WriteLine("error = " & Err) 'Output results for the error

End Sub

Function Factorial(n As Integer) As Long
Dim Fact As Long = 1
Dim i As Integer

If n = 0 Or n = 1 Then
Return Fact 'if
End If
'calculate n when greater than 2
For i = 2 To n
Fact = Fact * i
Next i
Return Fact
End Function

End Module```

2. ## Re: Cannot debug the calculation. been trying for 2 hours

Inside of your for loop seems to be where you are calculating each term of the approximation. For example, the first pass through you calculate x, then the second pass you get (x^3)/(3!). But you aren't keeping a running sum of the terms, you are just replacing the value in approximated_value each pass through the loop. So, in the end the result in approximated_value will essentially be 0 if your value of n is large enough, since each term of the series gets smaller and smaller.

Also, the calculation of Err should be done after the For loop.

Your logic should be more like:

Code:
```a = 0
for i = 0 to n
a += (nth term calculation logic)
Next
Err = (Err calculation logic)```
Edit: Also, you seem to be asking for input in degrees, and then converting to radians and storing in a separate variable. But in your calculation logic, you are still using the original input value variable which will still be in degrees.

3. ## Re: Cannot debug the calculation. been trying for 2 hours

Thanks heaps for the help, i've just switched the Err out of loop. But I still don't get what to change in the calculation for the approximated_value inside the for loop.

4. ## Re: Cannot debug the calculation. been trying for 2 hours

yoooooooo. thats what was wrong. i didnt have the x in radians in the calculation. youre a legend

5. ## Re: Cannot debug the calculation. been trying for 2 hours

that definitely fixed it for n =1 . But I am still unable to fix the code for when i use n =2

6. ## Re: Cannot debug the calculation. been trying for 2 hours

One of the biggest issues I see is people trying to write or debug code without a clear idea of what it is supposed to do. What you should have done here was actually write out the algorithm that you were trying to implement and then stepped through that algorithm manually as you were stepping through your code in the debugger. You would then be able to spot exactly where the behaviour of the code differed from your manual calculation. Even if you couldn't fix it, at least you'd be able to explain exactly where and how the two diverged. When someone posts a question that amounts to "here's my code, it doesn't work, what do I do" then you know that they haven't taken those steps.

7. ## Re: Cannot debug the calculation. been trying for 2 hours

approximated_Value = ((-1) ^ i) * (x ^ (2 * i + 1)) / Factorial(2 * i + 1) 'calculate the approximated value
This is very in-efficient. From one iteration to the next, new_numerator = old_numerator * x * x

Same for the denominator. new_denominator = old_denominator * (i * 2) * (i * 2 + 1)

8. ## Re: Cannot debug the calculation. been trying for 2 hours

Originally Posted by c3349627
that definitely fixed it for n =1 . But I am still unable to fix the code for when i use n =2
I gave you the reason and the answer in my first post, along with an example of how to do it in correctly code. You can't just copy and paste my answer and get it to work for you, and that was very intentional. This appears to be homework, so that is as much assistance as I will be providing. Good luck.

Edit: Final note, if you look at your code in your Factorial function, you will see that you have a For loop where inside the loop you are keeping a "cumulative product". You need to do the same in your approximation For loop, and keep a "cumulative sum".

#### Posting Permissions

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