Write VBA functions to calculate sin (x) using the Maclaurin arcsine series, and
ID: 3592700 • Letter: W
Question
Write VBA functions to calculate sin (x) using the Maclaurin arcsine series, and compare the values for sin-1(x) from your program to those given by the Excel spreadsheet function ASIN(x). The Maclaurin arcsine expansion is given by x 3x 6 40 (2n)! sin1(x)-2((2n+1) Note: This function by definition is only defined for-1 SxS1. When you write the code for calculating it, you will need to include code that assigns a value to it that reflects it is undefined for values of x outside this range. One way to do this would be to assign to the function the value of a variable that is declared as a variant, so you can assign the variable a real number value for-1s x S 1, but assign it as a text string for other values of x 1. Write a VBA function, Th (n, x), to calculate the value of an individual terms in the arcsine series as a function of n and x. Watch out for n! when n> 84. Write a VBA function, MacASIN (x), that converges the Maclaurin arcsine series to 15 significant figures, where the error should be less than the tolerance, as defined by the expression: 2. tol-10-16 > error = T,(1.x) or where the terms become too large or too small to calculate. You can do this using a while.WEnd loop, just use a variable, such as Sum, which is set to Sum Tn (x,0) outside the While..WEnd loop, then set to Sum-Sum Tn (x, 1) inside the loop. Be sure to count the iterations of the loop, and to set the loop to exit if the iterations don't converge. Since you may want to set Sum to a text string, declare it as a Variant. Finally, since sin-1(0): O sin-1(-1) = /2, and sin-1(1) = /2, there is no need to converge the series expansio for the arcsine function for those values of x; just set MacASIN to the appropriate value. Make a table comparing the values of the Excel spreadsheet ASINCx) function to your user defined function MacASin(x). You do not need to write a Sub to do this. Be sure to set your table to display 15 significant figures so you can see the differences between the functions (Excel only displays 15 significant figures, but carries 16 in its calculations). Make your comparisons for x-±001.0.1.09 and ± 1. Include error trapping code in case the user enters text instead of a number. 3. 4.Explanation / Answer
Option Explicit
Dim sign As String
Dim val1 As Double
Dim val2 As Double
Private Sub cmd0_Click()
txtbox.Value = txtbox.Value & cmd0.Caption
End Sub
Private Sub cmd1_Click()
txtbox.Value = txtbox.Value & cmd1.Caption
End Sub
Private Sub cmd2_Click()
txtbox.Value = txtbox.Value & cmd2.Caption
End Sub
Private Sub cmd3_Click()
txtbox.Value = txtbox.Value & cmd3.Caption
End Sub
Private Sub cmd4_Click()
txtbox.Value = txtbox.Value & cmd4.Caption
End Sub
Private Sub cmd5_Click()
txtbox.Value = txtbox.Value & cmd5.Caption
End Sub
Private Sub cmd6_Click()
txtbox.Value = txtbox.Value & cmd6.Caption
End Sub
Private Sub cmd7_Click()
txtbox.Value = txtbox.Value & cmd7.Caption
End Sub
Private Sub cmd8_Click()
txtbox.Value = txtbox.Value & cmd8.Caption
End Sub
Private Sub cmd9_Click()
txtbox.Value = txtbox.Value & cmd9.Caption
End Sub
Private Sub cmdclear_Click()
txtbox.Value = ""
val1 = 0
val2 = 0
sign = ""
End Sub
Private Sub cmdcos_Click()
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Value)
txtbox.Value = Math.Cos(v)
aa: Exit Sub
End Sub
Private Sub cmddivide_Click()
sign = "/"
On Error GoTo aa
val1 = CDbl(txtbox.Value)
txtbox.Value = ""
aa: Exit Sub
End Sub
Private Sub cmdequal_Click()
On Error GoTo aa
val2 = CDbl(txtbox.Value)
If (sign = "+") Then
txtbox.Value = val1 + val2
ElseIf (sign = "-") Then
txtbox.Value = val1 - val2
ElseIf (sign = "*") Then
txtbox.Value = val1 * val2
Else: txtbox.Value = val1 / val2
End If
aa: Exit Sub
End Sub
Private Sub cmdmultiply_Click()
sign = "*"
On Error GoTo aa
val1 = CDbl(txtbox.Value)
txtbox.Value = ""
aa: Exit Sub
End Sub
Private Sub cmdplus_Click()
sign = "+"
On Error GoTo aa
val1 = CDbl(txtbox.Value)
txtbox.Value = ""
aa: Exit Sub
End Sub
Private Sub cmdsin_Click()
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Value)
txtbox.Value = Math.Sin(v)
aa: Exit Sub
End Sub
Private Sub cmdsqrt_Click()
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Value)
txtbox.Value = Math.Sqr(v)
aa: Exit Sub
End Sub
Private Sub cmdsquare_Click()
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Value)
txtbox.Value = v ^ 2
aa: Exit Sub
End Sub
Private Sub cmdsubtract_Click()
sign = "-"
On Error GoTo aa
val1 = CDbl(txtbox.Value)
txtbox.Value = ""
aa: Exit Sub
End Sub
Private Sub cmdtan_Click()
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Value)
txtbox.Value = Math.Tan(v)
aa: Exit Sub
End Sub
Private Sub Form_Load()
txtbox.Value = ""
End Sub
Private Sub txtbox_KeyPress(KeyAscii As Integer)
'validate user inputs
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or (Chr(KeyAscii) = ".") Or _ KeyAscii=vbKeyBack Then
Exit Sub
Else: KeyAscii =0
End If
End Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.