user17150885
user17150885

Reputation:

Javascript find intersections given two functions

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?

Upvotes: 2

Views: 474

Answers (1)

user6302504
user6302504

Reputation:

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;

Upvotes: 5

Related Questions