|
Hi all. I've been working on a model in Excel that I'm considering porting into C/C++. I've been using the "Goal Seek" function in Excel to find a value in one cell that causes the results in another cell to equal some target value.
Is there an existing numerical recipe/algorithm to perform a similar function in C without reinventing the wheel?
Thanks for any help!
|
|
|
|
|
Not in C/C++, but this is a custom implementation in VB:
http://www.bmsltd.ie/DLCount/DLCount.asp?file=GoalSeek.zip[^]
Goal seeking is based on linear interpolation, perhaps you can adapt the VB code to C++?
I'm not too familiar with COM, etc. so I don't know the details but I do know that you can make calls to Excel from C++, maybe you can call the Excel Goal Seek function using COM (by loading the Excel .dll)?
|
|
|
|
|
"...but I do know that you can make calls to Excel from C++..."
Well, THAT would solve my problem! I've been doing a lot of processing in Excel (nicer, because I can see the numbers all laid out), then saving data to a text file, reading it into my C program and continuing my model that way. If I could just make calls right into a spreadsheet I could save myself a lot of dumb grunt labor. I'll check into that. Thanks!
|
|
|
|
|
Here[^] is a Microsoft knowledge-base article that provides some explanation. I think you have to use MFC to do it.
If you want to use Managed C++, here's a nice CP article:
Link[^].
I don't know a whole lot about COM, etc...so you might want to try asking for more details in the C++ forums here. Somebody can probably give you more assistance there.
|
|
|
|
|
KimN wrote: Is there an existing numerical recipe
There is a famous book called "Numerical Recipes", the original editions are available online for free. See here[^]. I think you need some adobe plugin to view the books - it is well worthwhile.
What you are interested in is optimisation of functions. The way this is typically done is you define a function that you want to minimize, e.g.
double func(double* x)
which takes N variables in the array 'x' and returns a single value. An optimization routine will attempt to find the values of 'x' that minimize the function. A very simple workhorse is called the downhill simplex method in multidimensions, but to you it is simply a function Amoeba to which you pass mainly a pointer to your function, the number of variables and starting values.
If you want to solve f(x) = c, make func(x) = (f(x) - c)^2
You will find a listing for amoeba in section 10.4 of Numerical Recipes in C.
If you know more about the function you are trying to minimize then there are probably better routines available, but I'd suspect that amoeba will work as well as excel (and be much faster in native code).
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Good answer, nicely explained.
|
|
|
|
|
Hi,
I am looking for the best way to sort multiple lists of unique items so none of the list items match at the same index.
Here is an example of what I want to do.
Original lists:
List 1: A, B, E, F
List 2: A, C, D, E
List 3: A, C, E, F
List 4: A, D, F, G
Should be sorted to:
List 1: A, B, E, F
List 2: C, A, D, E
List 3: E, F, A, C
List 4: D, G, F, A
Can anyone see a good way to solve this? Let me know if the above doesn't make sense.
Thanks
J
|
|
|
|
|
If the elements of the list can be completely random, then it seems to me you'd have to do this brute-force style. I don't see an obvious algorithm for speeding this up.
I do know that genetic algorithms are used to generate Sudoku tables (which is a similar problem to the above), but GA's are rather non-trivial to implement. It's probably easier to brute-force it for small lists.
|
|
|
|
|
Thanks, yeah I was thinking that I might need to do something pretty ugly to get this working. I've potentially got 5 sets of 42 items to sort so it could turn into alot of cycles to get 5 unique sets.
J
|
|
|
|
|
jrg2000 wrote: I've potentially got 5 sets of 42 items to sort
As a matter of interest, how many different items can there be in each set of 42 and do you have repeats? e.g. the sets are collections of different elements from a set of 80 elements, with no repeats OR the sets are collections from 10 elements with arbitrary numbers of repeats.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
I think this is really a form of a timetable problem, for which there are a number of references and algorithms on the web. Genetic algorithms are one common approach.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Thanks I didn't think of timetables but that is exactly what I am trying to do. I'll definately look into that.
J
|
|
|
|
|
I would assume the best way, is the following.
1. Assume the first list will not be sorted.
2. Move down the first list, verifying that each subsequent list doesn't have the matching item.
3. If an item matches in that list with the current item in the first list, push it to the end.
4. Iterate till the end.
5. Once at the end, you look over the last items, if any match the final element of the first list, then you must swap that item with one of the items in the list that doesn't conflict with the first list's item.
Basically, once you're at the end, you're down to a single lookup for each list. That should be as simple as you can get it.
Does that make sense?
|
|
|
|
|
Andrew Rissing wrote: I would assume the best way, is the following.
This is a way, it may not be the best way. The reason is that it there is no guarantee that it will work. When you find a clash you make a decision as to what swap to make to resolve it. There are probably many ways to resolve each clash - you choose one. If you make the wrong choice your algorithm may get stuck (i.e you end up with a clash that you cannot resolve - all the elements you have left to swap with clash as well) - there is no way of backtracking in your algorithm to change those earlier choices. If you include backtracking to try all the choices you can make - then you have a simple exhaustive search.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Ah, this should be easy.
Assume you have list1 fully filled.
list2 = new list<char>();
for (int i = 1; i < list1.size(); i++)
{
list2.push_back(list1.at(i));
}
list2.push_back(list1.at(0);
</char>
Basically, you're shifting indices by one, and keep doing it for every index. The theoretical maximum number of lists you can make is the size of the initial list, since you can only shift indices that much before they repeat again (informal proof). I can demonstrate this more formally, but I won't write it here.
|
|
|
|
|
hi!
i have a question. that is we have ha a problem "which one is smaller n pow 2, 1000 pow n, n pow n, n pow 1000 , when n value is nearer to infinite" plz also give reason along with answer
Best Regards,
Huma Satti
|
|
|
|
|
Exponential functions are larger then powers of a number when n is nearer infinity
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
He's not comparing exponential functions.
|
|
|
|
|
|
Try looking at the logarithms of these values and comparing them. You should easily see the answer.
|
|
|
|
|
THNX ALOT 4 UR HELP SO NICE OV U
|
|
|
|
|
I assume you mean the derivatives? Once upon a time I learned about something that I recall as l'Hôpital's rule[^]. On the other hand, I have no clue what the hell the derivation of n log n is. Do you think Ilidiot knows?
--
Kein Mitleid Für Die Mehrheit
|
|
|
|
|
My guess is
n pow 2
your task is to proof it (by induction?)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
SO NICE OV U THNX ALOT OV UR HELP
|
|
|
|
|
huma satti wrote: when n value is nearer to infinite
Is n interger? real number? complex number? What interval are we talking about? Positive or negative infinity? I'd give it back to teacher and say that (s)he formulated the problem too vaguely :P
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|