Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"Goal Seek" from Excel in Javascript

2,570 views
Skip to first unread message

Michal

unread,
May 16, 2008, 5:31:12 AM5/16/08
to
Does any one know how to create a function that works similar as "Goal
Seek" in Excel?

For example:
var a=8;
var b=6;
function c(a,b) {
return a*b;
}

And now I want that "c" will return me 56 and I want to do that only
by changing "b" and this "goal seek" function will return me solution
that "b" must be equal 7

I hope I didn't explain too chaotic.

I tried to find that on google but without any results.
Thanks in advance
Michael

Gregor Kofler

unread,
May 16, 2008, 1:26:21 PM5/16/08
to
Michal meinte:

Perhaps this for a start:

http://en.wikipedia.org/wiki/Newton%27s_method

Gregor

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum

Dr J R Stockton

unread,
May 16, 2008, 6:27:26 PM5/16/08
to
In comp.lang.javascript message <733f879c-e602-46dd-98aa-226d82210f4c@y3
8g2000hsy.googlegroups.com>, Fri, 16 May 2008 02:31:12, Michal
<ret...@gmail.com> posted:

>For example:
>var a=8;
>var b=6;
>function c(a,b) {
> return a*b;
>}
>
>And now I want that "c" will return me 56 and I want to do that only
>by changing "b" and this "goal seek" function will return me solution
>that "b" must be equal 7

Go to <URL:http://www.merlyn.demon.co.uk/js-demos.htm#FZ>, insert
in the textarea

function c(a,b) { return a*b; }
c(8, X) - 56

put Min X =, say, 0 and Max X = 50, press Find, and read the answer.

Then read the whole page, and View Source to find out how it is done :
it uses


function FindZero(F) { with (F) {
var j, X, XV, HV, LV, Fn = EX1.value
var Lo = userIn(Min1), Hi = userIn(Max1)
X = Lo ; LV = eval(Fn)
X = Hi ; HV = eval(Fn)
if ((LV*HV)>=0) { Ans1.value="Bad bounds" ; return }
for (j=0; j<40; j++) {
X = (Hi+Lo)/2 ; XV = eval(Fn) ; if (XV==0) break
if ((XV*LV)<0) { Hi = X ; HV = XV } else { Lo = X ; LV = XV }
}
Ans1.value="X = "+SigFigExp((Hi+Lo)/2, 8) } }


It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Michal

unread,
May 19, 2008, 10:37:26 AM5/19/08
to
On 16 May, 23:27, Dr J R Stockton <j...@merlyn.demon.co.uk> wrote:
>...
> ...
Thank you Dr J R Stockton.
Thats something what I was looking for.


> It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

It's not that I didn't read. I just couldn't find.. Maybe because my
"question" was bad :)

Thanks again

Michael

ad...@faze-1.com

unread,
Oct 19, 2014, 5:09:15 PM10/19/14
to
I know this is an old question but it is the first result in a google search and I wanted a better solution than the findZero function sugested below. So I made my own script which uses the Stephensen's Method:

https://github.com/adam-hanna/goalSeek.js

Scott Sauyet

unread,
Oct 30, 2014, 9:31:10 AM10/30/14
to
ad...@faze-1.com wrote:
> Michal wrote:

>> Does any one know how to create a function that works similar
>> as "Goal Seek" in Excel? [ ... ]

> I know this is an old question but it is the first result in a
> google search and I wanted a better solution than the findZero
> function sugested below. So I made my own script which uses the
> Stephensen's Method:
>
> https://github.com/adam-hanna/goalSeek.js

I like this idea quite a bit. But your API is a little... well,
crowded might be a good word. I think rather than a positional
parameter list a configuration object would provide both more
flexibility and more readability:

goalSeek({
testFunction: function(a, b, c) {return a * b * c;},
goal: 140,
params: [4, 5, 3],
position: 2,
tolerance: 0.01, // optional
maxIterations: 1000 // optional
});
//=> 7

goalSeek({
testFunction: function(i, o) {return i * o.a o.b.b1;},
goal: 140,
params: [4, {a: 5, b: {b1: 3}}],
position: 2,
prop: "b.b1", // optional
tolerance: 0.01, // optional
maxIterations: 1000 // optional
});
//=> 7

The other thing I would suggest is that allow an undefined value
as the starting choice for the target parameter. I know that this
would make the algorithm harder to get off the ground; you'll have
to go seeking for an appropriate initial value that works. But it
could lead to a nicer user experience, using a token such as `__`
(which is not defined) as a placeholder:

var __; // (undefined)
goalSeek({
testFunction: function(a, b, c) {return a * b * c;},
goal: 140,
params: [4, 5, __],
position: 2,
tolerance: 0.01, // optional
maxIterations: 1000 // optional
});
//=> 7

Note the change to `params`. This makes it much clearer what you're
looking for.

Cheers,

-- Scott
0 new messages