Thread: formula code
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Dana DeLouis Dana DeLouis is offline
external usenet poster
 
Posts: 947
Default formula code

Do While x t (THIS MAKES A COMPILE ERROR

Hi. I think something didn't get copied correctly. The program should keep
looping as long as the new 'x does not equal the older (t)emporary value.

Do While x < t

I have tried pasting it into both the worksheet and a separate module


Place the function code in a Worksheet Module.

It sounds like you were using range names. Are your names correct?
Try not to use variable 'a' and angle "A", as these are the same to Excel.

I entered this in B7, and it worked ok.:
=find_x(a,b,f,p)

(returns 0.13111617608834 in Radians)

You can also use this, and it worked ok also:
=find_x(B2,B3,B4,B5)

Also, your main equation may be incorrect also in terms of Excel's format.
(ie cos^2(A) ). Try:
=a*COS(B7)+(b-f)*SIN(B7)-p*COS(B7)^2

This returned 0.00000000000000

I made a slight change to the equation that you should use. Excel's
sometimes won't see a 0 as really being a zero, so we need to round the
numbers. This helps prevent endless loops.

Function Find_x(a, b, f, p)
Dim x As Double
Dim t As Double
Dim j As Long
x = 0
Do
t = x
x = x + (a + (b - f) * Tan(x) - p * Cos(x)) / _
(f - b + a * Tan(x) - 2 * p * Sin(x))
Loop Until Round(x - t, 16) = 0
Find_x = x
End Function


As a side note, there are multiple solutions to your angle x, all differing
by 2 Pi.
Suppose you do not want a negative solution.
You could modify that code so that you start your guess at 0, and then make
your best guess before the loop.
ie x = x + (a + (b - f) * Tan(x)...etc
Now, if this is negative, add 2 Pi.
If x < 0 Then x = x + 2 * WorksheetFunction.Pi

Now, continue the code within the loop, and you should get a positive
number.

--
HTH. :)
Dana DeLouis
Windows XP, Office 2003


"nobbyknownowt" wrote in message
...

Hi there, hopefully this is simple!! I've asked the same question as the
last entry on my last question but it could get lost t the bottom
there!



I have been given the following code (the solution is
7.51240351575601)

Sub TestiT()
Dim a, b, f, p
a = 1430
b = 1651
f = 7984
p = 600
Debug.Print Find_x(a, b, f, p)
End Sub



Function Find_x(a, b, f, p)
Dim x As Double
Dim t As Double
Dim j As Long
x = 0
t = 1
Do While x t (THIS MAKES A COMPILE ERROR SO I CHANGED TO
COMBINATIONS OF xt,xt, x<t and x=t )
t = x
x = x + (a + (b - f) * Tan(x) - p * Cos(x)) / _
(f - b + a * Tan(x) - 2 * p * Sin(x))
Loop
Find_x = x
End Function


How do I put this in my workbook?
I have tried pasting it into both the worksheet and a seperate module
and having the formula =find_x(a,b,f,p) in the cell i need the result
but I just get a #value error, when I evaluate the formula I get #NAME
errors for a,b,f,p

By the way values for a,b,f,p are on the sheet with cell references
b2,b3,b4 & b5. If I change a=1430 to a="b2" or a=b2 I still get the
same error.

Have had a look at various resources to find out what I'm doing wrong
here but its difficult with such a wide range of search terms to
enter.

Anybody able to point me in the right direction?

Cheers
Nobby




--
nobbyknownowt