none
How would you change both of the codes to cos? RRS feed

  • Question

  • Module Module1

     

        Sub Main()

                  ' declare variables

            Dim x, n As Integer

            Dim fact, ans, a, b, c As Double

     

            'declare pi

            Dim pi As Double = 3.1415926535897931

     

            'ask for and store user input values

            Console.WriteLine("Enter 'x' value (degrees): ")

            x = Console.ReadLine()

            Console.WriteLine("Enter the 'n' degree of the sum: ")

            n = Console.ReadLine()

     

            'change x input to radians

            c = x * (pi / 180)

             'calculation by formula

             For a = 0 To n   'establish a value for (2n + 1)!

                fact = 1

                For b = 1 To ((2 * a) + 1)

                    fact = fact * b

                Next b

     

                'add each term in the series until specified by the user by their n input

                ans += ((((-1) ^ a) * (c) ^ ((2 * a) + 1)) / fact)

             Next a

             'output result to user

            Console.WriteLine("Sin(" & x & ") = " & ans & " (to " & (n + 1) & " terms). ")

     

             Console.ReadKey()

     

        End Sub

     End Module

    THE NEXT CODE IS FOR GUI:

    Public Class Form1

     

           ' This program calculates the value of sin(x) using a truncated series to the nth degree when given x and n

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

     

        End Sub

     

        Private Sub button_calculate(sender As Object, e As EventArgs) Handles Calculate.Click

     

            'declare variables

            Dim x, n As Integer

            Dim fact, ans, a, b, c As Double

     

            'declare pi

            Dim pi As Double = 3.1415926535897931

     

             'ask for and store user input values

            x = CInt(Val(Inputbox.Text))

            n = CInt(Val(Inputboxn.Text))

     

     

            'change x input to radians and store as variable c

            c = x * (pi / 180)

     

            'calculation by formula

     

            For a = 0 To n   'establish a value for (2n + 1)!

                fact = 1

                For b = 1 To ((2 * a) + 1)

                    fact = fact * b

                Next b

     

     

                'add each term in the series until specified by the user by their n input

                ans += ((((-1) ^ a) * (c) ^ ((2 * a) + 1)) / fact)

     

            Next a

            ' output calculated sin(x) function to the user

            Outputbox.Text = ans.ToString()

        End Sub

     

        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles Inputbox.TextChanged

     

        End Sub

     

        Private Sub button_exit(sender As Object, e As EventArgs) Handles Exit_form.Click

            Close()     'allows the user to close the program

        End Sub

    End Class

    Tuesday, October 4, 2016 2:02 AM

Answers

All replies

  • As stated in the other post. Also, best to post one question at a time rather than several.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 4, 2016 2:06 AM
    Moderator
  • Hi Improve MSDN,

    Thank you for your post.

    Firstly, please edit your post and put your code in code block as @Kareninstructor suggested.

    Secondly, is it your homework? If yes, please tell us what have you done for this and how do you want us to help you?

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 4, 2016 6:49 AM
  • This shows one method in example 1.

    http://www.matrixlab-examples.com/sine-series.html

    This looks like your equations?

    https://en.wikipedia.org/wiki/Taylor_series

    Tuesday, October 4, 2016 10:14 AM
  • Hey,

    Just posting here as this looks extremely similar to what I am having trouble with.

    I am writing a VB program to calculate the cos value of various degree values 'x' using a truncated series. The console application allows the user to enter 'x' and 'n'. The program then calculates cos(theta) by a truncated formula using 'n' first terms of the series.

    Using the program I am required to find values for cos(30) and cos(60) for n = 2, 3, 4 and 5. I then need the program to calculate the 'True value' of Cos(theta) using the intrinsic function Math.cos(x). I then need to implement code that uses both the approximated cos value and the true cos value and provide me with a relative percentage error using the formula below.

    I am successfully able to calculate the Approximate value of cos(theta) but not too sure how to calculate the true value of cos(theta) using the intrinsic function.

    This is what I have so far. 

    Module Module1
    
        Sub Main()
      'Define variables
            Dim n As Decimal                        'n Value
            Dim i As Decimal                        'Counter For Loop
            Dim theta As Decimal                    'Angle / x Value To Be Evaluated
            Dim Radians As Decimal                  'Calculated Radian Value
            Dim Pi As Double = 3.14159265358979     'Value Of Pi To 14 d.p.
            Dim k As Single
            Dim Sum As Single                       'Sum
            Dim Fact As Long                        'Factorial
            Dim f As Long                           'Function
            Dim rel_e As Double                     'Relative Error in %
            Dim v As Double                         'True Value
            Dim v_approx As Double                  'Approximated Value
    
    
            'Input Variables.
            Console.WriteLine(" Please Enter Value Of 'x' For Cos(x):") 'Asks For input
            theta = Console.ReadLine()                                  'Reads input
            Console.WriteLine("")
            Console.WriteLine(" Please Enter A Value For n:")           'Asks For Input
            n = Console.ReadLine()                                      'Reads Input
    
    
            'Converts Radians to Degrees
            Radians = (theta * Pi) / 180
    
    
            'Relative Error Formula
            rel_e = (Math.Abs(v) - Math.Abs(v_approx) / Math.Abs(v)) * 100
    
    
            'VB Intrinsic Function
            v = Math.Cos(theta)
    
            'Set Sum To 1 And Use A Do Loop To n
            Sum = 1
            For i = 1 To n
                Fact = 1
                f = (2 * i)
                Do While f > 0
                    Fact = Fact * f
                    f = f - 1
                Loop
                k = ((-1) ^ i) * ((Radians ^ (2 * i) / Fact)) 'Truncated Series
                Sum = Sum + k
            Next i
    
    
            'Results
            Console.WriteLine("")
            Console.WriteLine("Cos(" & theta & ") = " & Sum)
            Console.WriteLine("")
            Console.WriteLine("The Cosine value of 'x' is = " & Math.Cos(theta))
            Console.WriteLine("")
            Console.WriteLine("The Relative Error 'x' is = " & rel_e = (Math.Abs(v) - Math.Abs(v_approx) / Math.Abs(v)) * 100)
            Console.ReadLine()
        End Sub
    
    End Module

    Any help would be greatly appreciated. 

    Cheers

    Mr.Steez

    Tuesday, August 22, 2017 2:03 PM
  • Hey,

    Just posting here as this looks extremely similar to what I am having trouble with.

    I am writing a VB program to calculate the cos value of various degree values 'x' using a truncated series. The console application allows the user to enter 'x' and 'n'. The program then calculates cos(theta) by a truncated formula using 'n' first terms of the series.

    Using the program I am required to find values for cos(30) and cos(60) for n = 2, 3, 4 and 5. I then need the program to calculate the 'True value' of Cos(theta) using the intrinsic function Math.cos(x). I then need to implement code that uses both the approximated cos value and the true cos value and provide me with a relative percentage error using the formula below.

    I am successfully able to calculate the Approximate value of cos(theta) but not too sure how to calculate the true value of cos(theta) using the intrinsic function.

    This is what I have so far. 

    Module Module1
    
        Sub Main()
      'Define variables
            Dim n As Decimal                        'n Value
            Dim i As Decimal                        'Counter For Loop
            Dim theta As Decimal                    'Angle / x Value To Be Evaluated
            Dim Radians As Decimal                  'Calculated Radian Value
            Dim Pi As Double = 3.14159265358979     'Value Of Pi To 14 d.p.
            Dim k As Single
            Dim Sum As Single                       'Sum
            Dim Fact As Long                        'Factorial
            Dim f As Long                           'Function
            Dim rel_e As Double                     'Relative Error in %
            Dim v As Double                         'True Value
            Dim v_approx As Double                  'Approximated Value
    
    
            'Input Variables.
            Console.WriteLine(" Please Enter Value Of 'x' For Cos(x):") 'Asks For input
            theta = Console.ReadLine()                                  'Reads input
            Console.WriteLine("")
            Console.WriteLine(" Please Enter A Value For n:")           'Asks For Input
            n = Console.ReadLine()                                      'Reads Input
    
    
            'Converts Radians to Degrees
            Radians = (theta * Pi) / 180
    
    
            'Relative Error Formula
            rel_e = (Math.Abs(v) - Math.Abs(v_approx) / Math.Abs(v)) * 100
    
    
            'VB Intrinsic Function
            v = Math.Cos(theta)
    
            'Set Sum To 1 And Use A Do Loop To n
            Sum = 1
            For i = 1 To n
                Fact = 1
                f = (2 * i)
                Do While f > 0
                    Fact = Fact * f
                    f = f - 1
                Loop
                k = ((-1) ^ i) * ((Radians ^ (2 * i) / Fact)) 'Truncated Series
                Sum = Sum + k
            Next i
    
    
            'Results
            Console.WriteLine("")
            Console.WriteLine("Cos(" & theta & ") = " & Sum)
            Console.WriteLine("")
            Console.WriteLine("The Cosine value of 'x' is = " & Math.Cos(theta))
            Console.WriteLine("")
            Console.WriteLine("The Relative Error 'x' is = " & rel_e = (Math.Abs(v) - Math.Abs(v_approx) / Math.Abs(v)) * 100)
            Console.ReadLine()
        End Sub
    
    End Module

    Any help would be greatly appreciated. 

    Cheers

    Mr.Steez

    This is a double post of the same thing:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/6d37069f-2dd6-4488-9e27-70f91725a317/trig-functions?forum=vbgeneral

    Why don't you respond to that thread instead of digging up an unrelated question from a year ago? And address those issues that others brought up?

    As noted in the other thread you posted on this you appear to have radians mixed with degrees in some places.

    You never assign a value to v_approx, instead you use Sum.

    Tuesday, August 22, 2017 2:31 PM