Code covered by the BSD License  

Highlights from
GoalFind

from GoalFind by Keith Sudheimer
Do the opposite of any single variable function or transformation

GoalFind(Formula,Goal, MaxNum, MinNum, MaxSeconds, PrintSolution)
%For the given function 'Formula' (with  single variable denoted as capital N) GoalFind will
%iteratively change the input to that function until the 'Formula' returns the
%value Goal.  If GoalFind reports the output of the 'Formula' as howClose.
%and the input to the formula as Solution.  MaxNum and MinNum provide
%bookends for possible values of Solution.  MaxSeconds allows one to
%specify how long you want matlab to work on finding the solution (some
%searches would go on indefinitely).  The final argument toggles the
%printing of the solution to the screen.  I reccommend you use it to make
%sure you are in the pallpark of values you are looking for.
%ex. [Solution howClose] = GoalFind('sqrt(N)',12,150,2, 10, 1)
%Solution = 144
%howClose = 12
% sqrt(144) = 12
%warning!!!!   Precision of the solution is dependant on how much time you
%give matlab to work
% warning!!! Goal find will only yeild Solutions between MinNum and MaxNum

%Keith Sudheimer
%Stanford University
%10/01/2009
%


function [Solution, howClose] = GoalFind(Formula,Goal, MaxNum, MinNum, MaxSeconds, PrintSolution)
tic;
%alter the formula the user typed so that we can experiment with it

Formula = strrep(Formula,'N', 'Solution');

%try min first
Solution = MinNum;
eval(sprintf('temp = %s;',Formula));
howClose = abs(temp-Goal);
howCloseL = howClose;
BhowClose = howClose;
BSolution = Solution;
kdata=[];
count = 0;
NumRange = MaxNum - MinNum;
goalDiff = 0;

while max(goalDiff)~=1 &&  toc<MaxSeconds
    %generate 1000 points of data for your formula
    for i = MinNum:((MaxNum-MinNum)/999):MaxNum
        count = count+1;
        kinput(count) = i;
        %make a test value to try
        Solution = i;
        %enter it into formula
        eval(sprintf('temp = %s;',Formula));
        kdata(count) = temp;
        
    end
    
    %how far off the goal
    goalDiffabs = abs(Goal-kdata);
    %subtract 1 so that perfect is 1 and less than perfect is less than 1
    goalDiff = 1-goalDiffabs;
    
    %if we found the perfect solution then tell the user
    if max(goalDiff)==1
        Solution = kinput(find(goalDiff==1));
        howClose = kdata(find(goalDiff==1));

    else
        %best solution
        %                  fprintf('\nSolution not exact');
        Solution = kinput(find(goalDiff==(max(goalDiff))));
        Solution = Solution(1,1);
        howClose = kdata(find(goalDiff==(max(goalDiff))));
        howClose = howClose(1,1);
        %reset max and minimum numberss based on best solution
        MinNum = mean([Solution MinNum]);
        MaxNum = mean([Solution MaxNum]);
        %         fprintf('\n best solution=%f \nmin=%f max =%f',Solution,MinNum,MaxNum)       
    end
      
end
if PrintSolution == 1
SolutionSTR = num2str(Solution);
fprintf('\n%s = %f\n', strrep(Formula,'Solution',num2str(Solution)), howClose)   
else
end

end





Contact us at files@mathworks.com