触发功能 [英] Trig Functions
问题描述
嘿,
我正在编写一个VB程序,用截断的系列来计算各种度数值'x'的cos值。控制台应用程序允许用户输入"x"和"n"。然后,程序使用序列中的'n'首字母
,通过截断公式计算cos(theta)。使用程序我需要为n = 2,3,4和5找到cos(30)和cos(60)的值。然后我需要程序使用内在函数计算Cos(theta)的'真值'函数Math.cos(x)。然后我需要实现使用
近似cos值和真实cos值的代码,并使用下面的公式为我提供相对百分比误差。
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.
我能成功计算cos(theta)的近似值但是不太确定如何使用内在函数计算cos(theta)的真值。
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
任何帮助将不胜感激。
干杯
Mr.Steez
推荐答案
我成功地能够计算cos(theta)的近似值,但不太确定如何使用内在函数计算cos(theta)的真值。
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.
Math.Cos方法接受以弧度表示的值。因此,如果θ为度,则首先将其转换为弧度。
the Math.Cos Method accepts values in radians. So if theta is in degrees, convert it to radians first.
(deg / 180.0 * Math.PI)
(deg / 180.0 * Math.PI)
问候,
Thorsten
Thorsten
这篇关于触发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!