how can one obtain coordinates of intersections of two line diagrams with given expression or equation?
for example: L1= sin(2x) , L2= Ln(x); or anything else.
how can one obtain coordinates of intersections of two line diagrams with given expression or equation?
for example: L1= sin(2x) , L2= Ln(x); or anything else.
In general, you have to solve the equation L1(x) = L2(x). If you don't know from the beginning what L1 and L2 are (linear, polynominal...) then the only solution is numeric solving for example with Netwon algorithm. The problem is then reduced to finding roots (zeros) of function f(x) = L1(X) - L2(X).
This is not a trivial question: what you're asking for is a general method for solving any mathematical equation.
For instance, you could consider using the bisection method, or Newton's method.
There is no general answer.
Since you tagged with matlab, you can do it with fsolve(@(x)sin(2*x)-log(x),1)
which gives 1.3994 (1 is the initial starting point or guess). The y-coordinate is log(1.3994) = 0.3361
.
That is, you use fsolve
, pass it the function you want to solve for the zero of, in this case sin(2*x) == log(x)
so you want sin(2*x) - log(x) == 0
(log
is the natural log in matlab).
If you already have functions set up like, e.g. L1 = @(x)sin(2*x)
and L2 = @(x)log(x)
(or in functions L1.m
and L2.m
) you can use fsolve(@(x)L1(x)-L2(x),1)
.
Amazingly, nobody has yet suggested using the function designed to do this in matlab. Use fzero here. Fzero is a better choice than fsolve anyway, which requires the optimization toolbox. And, yes, you could do this with Newton's method, or even bisection or the secant method. But reinventing the wheel is the wrong thing to do in general. Use functionality that already exists when it is there.
The problem at hand is to find a point where
sin(2*x) == log(x)
Here log(x) refers to the natural log. Do this by subtracting one from the other, then looking for a zero of the result.
fun = @(x) sin(2*x) - log(x);
Before you do so, ALWAYS plot it. ezplot can do that for you.
ezplot(fun)
The plot will show a single root that lies between 1 and 2.
fzero(fun,2)
ans =
1.3994
As a general non-analytic solution, when you have any 2 curves described by 2 sets of points, there is great submission at File Exchange - Fast and Robust Curve Intersections.