Code Monkey home page Code Monkey logo

goalseek's Introduction

goalseek

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.

function declaration and arguments definition

goalseek function is declared as double goalseek(double* pf, int argn, int warg, double goal, ...)

  1. pf is pointer to function (type double)
  2. argn is number of arguments accepted by function pf pointed to. eg double add3num(double a, double b, double c) accepts 2 arguments which is a, b and c
  3. warg is index of which arguments you want to seek. eg, if function add(500,300) and you want to seek second value (currently is 300), the set warg value to 2
  4. goal is the end value that you wants
  5. ... is variadic variable where you set to initialize the function

usage example

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);
}	

screenshot

license

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.

goalseek's People

Contributors

mrharmonies avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.