Code Monkey home page Code Monkey logo

Comments (7)

markrogoyski avatar markrogoyski commented on May 14, 2024

How about: Math\NumericalAnalysis ?

from math-php.

Beakerboy avatar Beakerboy commented on May 14, 2024

Sounds good to me.

from math-php.

Beakerboy avatar Beakerboy commented on May 14, 2024

This file in also in the Pull Request

from math-php.

markrogoyski avatar markrogoyski commented on May 14, 2024

Can you provide a simple example of how to use the NewtonsMethod function? Some simple input with expected output. It will help me better understand the function to be able to write unit tests for it and possibly refactor with improvements.
Thanks.

from math-php.

Beakerboy avatar Beakerboy commented on May 14, 2024

Newton's Method is a numerical method to solve the inverse of a function. Say, for example, you want to find the roots of the polynomial, f(x) = x^4+8x³ -13x² -92x+96. You can use newton's method. You find the x that will make f(x) = 0;

In our method:
$function is a callback to a f(x). In this case it will be: function($x){return $x ** 4 + 8 * $x ** 3 - 13 * $x ** 2 - 92 * $x + 96;}

$args is an array of arguments that have to be passed to the function. In or case it is ['x'], because $function only needs one parameter, the one we are solving for. If the function were f(x,y) = x³y + y²x and we wanted f(x|y=2) = 0, we would provide $args = ['x', 2].

$target is the value of f(x) we a trying to solve for. in our case we want to find the x that gives us f(x) = 0.

$guess is the starting point. For our polynomial above this is very important because there are 4 solutions. the starting point will determine which solution we will receive.

$tol is the tolerance...How close to the actual solution we would like.

Putting it all together, here is a console php statement to run the method. This polynomial has 4 roots, 3,1,-8 and -4.

php -r "include 'src/NumericalAnalysis/NewtonsMethod.php';use Math\NumericalAnalysisewtonsMethod; print NewtonsMethod::solve(function(\$x){return \$x ** 4 + 8 * \$x ** 3 - 13 * \$x ** 2 - 92 * \$x + 96;}, ['x'], 0, -4.1, .00001);"

Running this gives -3.9999999999999

An alternative implementation might be to make $args a list of parameter initial values, and replace '$guess' with '$position', where $position indicates which element in the $args array needs to be changed, keeping the rest constant. I think this is a better idea.

from math-php.

Beakerboy avatar Beakerboy commented on May 14, 2024

If I move to the alternate implementation would you prefer zero index or one index on the position?

from math-php.

markrogoyski avatar markrogoyski commented on May 14, 2024

Thanks for the great explanation. I've added unit tests based on it.

If you decide to refactor, please update the unit tests as well. For index positions, I've been using zero indexing for everything.

from math-php.

Related Issues (20)

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.