Goalseek is a simple C library used to seek for value in linear math function where goal value is set, similar to Goalseek tool you see in spreadsheet application. According to wikipedia, Goal Seeking is the ability to calculate backward to obtain an input that would result in a given output. This can also be called what-if analysis or back-solving. It can either be attempted through trial and improvement or more logical means.
goalseek function is declared as double goalseek(double* pf, int argn, int warg, double goal, ...)
pf
is pointer to function (type double)argn
is number of arguments accepted by functionpf
pointed to. egdouble add3num(double a, double b, double c)
accepts 2 arguments which is a, b and cwarg
is index of which arguments you want to seek. eg, if functionadd(500,300)
and you want to seek second value (currently is 300), the setwarg
value to2
goal
is the end value that you wants...
is variadic variable where you set to initialize the function
To use this library, just add #include "goalseek.h"
in your C code and call goalseek
function. See example below
#include <stdio.h>
#include "goalseek.h"
double avg(double a, double b, double c){
return (a + b + c) / 3;
}
double sum(double a, double b, double c){
return a + b + c;
}
double minu(double a, double b){
return a - b;
}
double nliner(double x,double a, double b){
return pow(a,2)+(a-6);
// as for now, goalseek dont work with nonlinear function
}
int main() {
double ans = 0;
printf("average of is 234, 456 and 876 is %lf\n", avg(234,456,876));
// i want goal for function average is 900. seek b value
ans = goalseek(&avg,3,2,900.0,234.0,456.0,876.0);
printf("If you want average to 900, C should be %lf\n", ans);
gets(stdin);
return (0);
}
Copyright (C) 2019 Mohd Kholid Yaacob (http://mrharmonies.blogspot.com)
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
A copy of the GNU General Public License is available on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You can also obtain it by writing to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.