Here is a great Goal Seek function for Numbers (macOS)

Many people have been looking for a goal seek function for Numbers for more than a decade. And since Apple still hasn't included this in Numbers, I decided to make one. It's based on an AppleScript and therefore works only in macOS.


All you need to do is copy the script text below into a new file in Script Editor, save it as "Goal Seeker" in the proper folder for Numbers scripts, ie ~Library/Scripts/Applications/Numbers/   In order to run the script, I suggest switching on the "Show script menu in menu bar" setting in the Script Editor. The script comments contain usage instructions. Personally, I use this almost every day. (Thx to Y. K. for the original idea.)



--===================================
-- Created: 2024/01/29
-- Based on original solution: https://discussions.apple.com/thread/3678032
--===================================
-- How to use: Select 2 cells. The first cell will contain the goal value (x) after the script calculation. The second cell should contain a formula (y) that the script will try to make into the value zero.
-- Ex: Put the start value 1 into cell A1. Put the formula "Pi - A1^2" into cell B1. Select both cells and run the script. The result will be 1.772… (in cell A1), because that x-value will result in a y-value close to zero.
-- Comment: The property cTol is a constant that specifies when y is considered to be close enough to zero (the tolerance). You can change this value in the first line of the script below. Alternatively, you could modify the script to take this tolerance as input from a third selected cell.
--===================================


property cTol : 1.0E-3 --This tolerance value defines the finish criteria

on run
	local dID, theSheet, tNum, rowNum1, colNum1, rowNum2, colNum2, x, prevX, y, prevY, deltaX, deltaY
	
	set {dID, theSheet, tNum, rowNum1, colNum1, rowNum2, colNum2} to my get_SelParams()
	
	tell application "Numbers" to tell document dID to tell theSheet to tell table tNum
		set x to value of cell rowNum1 of column colNum1
		set deltaX to 1 --Any start value
		set y to (value of cell rowNum2 of column colNum2) + 1 --Any start value
		
		repeat 100 times
			set prevY to y
			set y to value of cell rowNum2 of column colNum2
			if my abs(y) < cTol then exit repeat
			
			set deltaY to y - prevY
			set prevX to x
			set x to x - y / (deltaY / deltaX) --Follow the tangent (derivative)
			set deltaX to x - prevX
			set value of cell rowNum1 of column colNum1 to x as text
		end repeat
		
	end tell
	
end run


--=====


on get_SelParams()
	local d_ID, theSheet, t_Num, row_Num1, col_Num1, row_Num2, col_Num2
	
	tell application "Numbers" to tell document 1
		set d_ID to its id
		set theSheet to active sheet
		
		tell theSheet
			repeat with i from 1 to number of tables
				if selection range of table i is not missing value then exit repeat
			end repeat
			if selection range of table i is missing value then error "You must select cells"
			set t_Num to i
			
			
			tell selection range of table t_Num
				set {top_left, bottom_right} to {name of first cell, name of last cell}
				
				tell cell top_left to set {row_Num1, col_Num1} to {address of its row, address of its column}
				
				if top_left is bottom_right then
					set {row_Num2, col_Num2} to {row_Num1, col_Num1}
				else
					tell cell bottom_right to set {row_Num2, col_Num2} to {address of its row, address of its column}
				end if
			end tell --selection range
			
		end tell --theSheet
		
		return {d_ID, theSheet, t_Num, row_Num1, col_Num1, row_Num2, col_Num2}
	end tell -- Numbers
	
end get_SelParams

on abs(x)
	if x < 0 then
		set x to -x
	end if
	return x
end abs

Posted on Feb 15, 2024 2:02 AM

Reply

There are no replies.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Here is a great Goal Seek function for Numbers (macOS)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.