Solved

# how to calculate Exponential function by using vba

Posted on 2008-11-17

In inputbox input x and n

exponential function : e^x=1+ x/1! + x^2/2! +.....x^n/n! ; n!=1*2*3...*n

inside the function we can use t(n)=x^n/n! and for recurrence formula t(n)=t(n-1)*x/n

The partial sum to the n-th clause is expressed with s(n) which is ; s(n)=s(n-1)+t(n)

then e=1+s(n)

the calculation will repeat until i=n and |t(n)|<0.001

the problem is when i run the program the answer is no same with the exponential function above.

Sub e2()

Dim n As Double

Dim x As Double

Dim i As Double

Dim tn As Double

Dim sn As Double

Dim t1 As Double

Dim e As Double

x = CInt(InputBox("x?"))

n = CInt(InputBox("n?"))

Do

For i = 1 To n

t1 = x / (n - 1)

tn = (x / (n - 1)) * (x / n)

sn = tn + t1

e = 1 + sn

If x = 0 Then Exit For

Next i

Loop Until tn < 0.001 Or -tn < 0.001

MsgBox "e^" & CStr(x) & "=" & CStr(e)

End Sub