Exponential Problem

Exponential Problem - MsgBox ("The calculated...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Jordan Cooper 36 Problem Statement : Design a way to determine e x and compare it to e(x) in a VBA function. Create two user defined functions, one for calculating e x , and the other for calculating factorial. Assumptions : The user enters an integer. The program runs correctly. Theory : e x = 1 + (x/1!) + (x 2 /2!) + … e x = Σ(x n /n!) Solution : e (2) = 1 + ((2)/1) + ((2) 2 /(2*1)) e (2) = 1 + 2 + (4/2) e (2) = 1 + 2 + 2 e (2) = 5 Option Explicit Sub Exponential_Problem() Dim x As Double, maxvalue As Double, exponent As Double, E As Single x = InputBox("Enter a value for x in calculating e^x") maxvalue = InputBox("enter a maximum value of the last term in the estimate suggested 10^-7") exponent = whole(maxvalue, x) E = Exp(x)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: MsgBox ("The calculated e^x value = " & exponent & " and e(x) = " & E) End Sub Function whole(ByVal maxvalue As Double, ByRef x As Double) As Single Dim i As Single, n As Single, a As Single i = 0 n = 0 whole = 1 Do n = n + 1 a = (x ^ n) / (fact(n)) If (Abs(a) <= maxvalue) Then Exit Do whole = whole + a i = i + 1 Loop End Function Function fact(ByVal n As Single) As Single Dim s As Integer fact = 1 For s = 1 To n fact = fact * s Next s End Function Discussion : For an x-value of 1, the e x function returns a value of 2.718 and the e(x) function returns the same value. Also, the e x function should return the same value as e(x) for any value x....
View Full Document

Ask a homework question - tutors are online