2

I am trying today to solve for the coordinates where functions intersect. I am using the nerdarmer library right now, but it only returns only one solution out of all the possible solutions. For example, I want the code below to print -1, 0, 1 but it only outputs 0. Another example is if I want to find intersections between y = 0 and sin(x), I want the output to be ..., (-2pi, 0), (-pi, 0), (pi, 0), (2pi, 0), (3pi, 0), ...

intersect("x^3", "x")
function intersect(f1, f2){
    var x = nerdamer.solve('f1', 'f2');
    console.log(x.toString());  
}

Is there any way to get all the possible solutions?

7
  • 2
    y = 0 and sin(x) gives infinite number of solutions. How is it possible to get "all possible solutions" for this case? Commented Oct 27, 2021 at 22:35
  • 1
    For those cases, given a coordinate I would like the nearest solutions (for example the nearest solution to the right and to the left of a given coordinate) Commented Oct 27, 2021 at 22:37
  • You might be better off using pre-existing APIs like Wolfram Alpha API, which can solve relatively complex equations, instead of attempting to reinvent the wheel or find a third party library (the latter of which is off topic on SO). Commented Oct 27, 2021 at 22:40
  • Have you tried Nerdamer "If you have a feature request or a suggestion, please leave a message below or let us know on Github. We can only help if we know what it is that you need and look forward to your suggestions." at the comments section of nerdamer.com "You can also send me an email at [email protected] ." Commented Oct 27, 2021 at 22:40
  • 1
    I will try messaging them, thanks. Regarding wolfram alpha, are there javascript examples on how I can use their api to solve functions? Commented Oct 27, 2021 at 22:44

1 Answer 1

5
+150

You've misunderstood the syntax of nerdamer.solve

The first argument is the formula or equation. The second argument is the variable to solve for. If the first argument is not an equation, it is assumed to be equal 0. In your case x^3=0. which only has the solution 0.

If you want to intersect the equations you will need to set them equal to each other in the first argument. And in the second argument just specify x. (or change it to suit your needs if required).

intersect("x^3", "x")
function intersect(f1, f2){
    var x = nerdamer.solve(f1+"="+f2, "x");
    console.log(x.toString());  //outputs [0,1,-1]
}

Edit:

In your example you also directly put the strings "f1" and "f2" into the solve function which seems to just solve f=0;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.