M
Mr.Steez
Guest
Hey,
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
Continue reading...
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
Continue reading...