Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Excel Troubles
I have a equation that you can solve only by trial an error, but I wan
excel to do the work for me. The equation is this 1/(X^.5)=2Log(C*X^.5)-.8 Where c is some constant that isant worth mentioning ;) and the Log is base 10. Ok I need a macro that will keep trying values untill one side is equa to the other up to .001 decimal places. I tried making some sort o random function generator, but it seems like the random function kep repeating certain values. Any advice/help is appericated. -Mar -- Message posted from http://www.ExcelForum.com |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Excel Troubles
I came up with 3.73250246, ignoring C
"medialint" wrote: Something like this, though if C isn't worth mentioning then why put it in? Sub LoopSolve() Randomize Dim myRandomNum As Single Dim YRow As Long Dim XAdd As Long Dim CalcDiff As Single YRow = 2 Worksheets("Sheet1").Cells(1, 1) = "Number" Worksheets("Sheet1").Cells(1, 2) = "Difference" Do myRandomNum = Rnd * 1000 CalcDiff = Abs((1 / (myRandomNum ^ 0.5)) - (2 * Log(myRandomNum ^ 0.5) - 0.8)) Worksheets("Sheet1").Cells(YRow, 1 + XAdd) = myRandomNum Worksheets("Sheet1").Cells(YRow, 2 + XAdd) = CalcDiff YRow = YRow + 1 If YRow 65536 Then YRow = 2 XAdd = XAdd + 2 End If Loop Until CalcDiff < 0.001 End Sub "mrmark " wrote: I have a equation that you can solve only by trial an error, but I want excel to do the work for me. The equation is this 1/(X^.5)=2Log(C*X^.5)-.8 Where c is some constant that isant worth mentioning ;) and the Log is base 10. Ok I need a macro that will keep trying values untill one side is equal to the other up to .001 decimal places. I tried making some sort of random function generator, but it seems like the random function kept repeating certain values. Any advice/help is appericated. -Mark --- Message posted from http://www.ExcelForum.com/ |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Excel Troubles
Sorry, but the formula needs a slight tweak. The log
functtion in VBA is a natural log function not Log10. Also the formula will give negative answers so will meet the <.0001 criteria. Change the routine as follows: CalcDiff = Abs((1 / (myRandomNum ^ 0.5)) - (2 * (Log (myRandomNum ^ 0.5)/log(10#)) - 0.8)) and Loop Until CalcDiff < 0.001 and Calcdiff -.001 All the best DavidC -----Original Message----- I came up with 3.73250246, ignoring C "medialint" wrote: Something like this, though if C isn't worth mentioning then why put it in? Sub LoopSolve() Randomize Dim myRandomNum As Single Dim YRow As Long Dim XAdd As Long Dim CalcDiff As Single YRow = 2 Worksheets("Sheet1").Cells(1, 1) = "Number" Worksheets("Sheet1").Cells(1, 2) = "Difference" Do myRandomNum = Rnd * 1000 CalcDiff = Abs((1 / (myRandomNum ^ 0.5)) - (2 * Log(myRandomNum ^ 0.5) - 0.8)) Worksheets("Sheet1").Cells(YRow, 1 + XAdd) = myRandomNum Worksheets("Sheet1").Cells(YRow, 2 + XAdd) = CalcDiff YRow = YRow + 1 If YRow 65536 Then YRow = 2 XAdd = XAdd + 2 End If Loop Until CalcDiff < 0.001 End Sub "mrmark " wrote: I have a equation that you can solve only by trial an error, but I want excel to do the work for me. The equation is this 1/(X^.5)=2Log(C*X^.5)-.8 Where c is some constant that isant worth mentioning ;) and the Log is base 10. Ok I need a macro that will keep trying values untill one side is equal to the other up to .001 decimal places. I tried making some sort of random function generator, but it seems like the random function kept repeating certain values. Any advice/help is appericated. -Mark --- Message posted from http://www.ExcelForum.com/ . |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Excel Troubles
Forgot to remind you to add in the constant. So try the
routine from medialint as revised below to fix the natural log problem and the negative value issue and add in the constant. I have referenced cell C1 for the constant to make it easier for the constant to be changed without going to the routine. Also if you want, add Msgbox (myrandomnum) after the Loop statement to give you the answer quickly without having to search for it. Sub LoopSolve() Randomize Dim myRandomNum As Single Dim YRow As Long Dim XAdd As Long Dim CalcDiff As Single YRow = 2 Worksheets("Sheet1").Cells(1, 1) = "Number" Worksheets("Sheet1").Cells(1, 2) = "Difference" con = ActiveSheet.Range("C1").Value Do myRandomNum = Rnd * 100 CalcDiff = Abs(1 / (myRandomNum ^ 0.5)) - (2 * con * (Log(myRandomNum ^ 0.5) / Log(10#)) - 0.8) Worksheets("Sheet1").Cells(YRow, 1 + XAdd) = myRandomNum Worksheets("Sheet1").Cells(YRow, 2 + XAdd) = CalcDiff YRow = YRow + 1 If YRow 65536 Then YRow = 2 XAdd = XAdd + 2 End If Loop Until CalcDiff < 0.001 And CalcDiff -0.001 End Sub Best of luck DavidC -----Original Message----- I have a equation that you can solve only by trial an error, but I want excel to do the work for me. The equation is this 1/(X^.5)=2Log(C*X^.5)-.8 Where c is some constant that isant worth mentioning ;) and the Log is base 10. Ok I need a macro that will keep trying values untill one side is equal to the other up to .001 decimal places. I tried making some sort of random function generator, but it seems like the random function kept repeating certain values. Any advice/help is appericated. -Mark --- Message posted from http://www.ExcelForum.com/ . |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Excel Header - Auto Format troubles | Excel Discussion (Misc queries) | |||
Excel 2007 Formula troubles | Excel Worksheet Functions | |||
Excel troubles | Excel Worksheet Functions | |||
troubles with the Workbooks.Open method in Excel 97 | Excel Programming | |||
saving *.xls files but troubles with opening with MS Excel | Excel Programming |