How to find the intersections of two functions in MATLAB?
Solution 1
If you have two analytical (by which I mean symbolic) functions, you can define their difference and use fzero
to find a zero, i.e. the root:
f = @(x) x; %defines a function f(x)
g = @(x) 2*sin(x); %defines a function g(x)
%solve f==g
xroot = fzero(@(x)f(x)-g(x),0.5); %starts search from x==0.5
For tricky functions you might have to set a good starting point, and it will only find one solution even if there are multiple ones.
The constructs seen above @(x) something-with-x
are called anonymous functions, and they can be extended to multivariate cases as well, like @(x,y) 3*x.*y+c
assuming that c
is a variable that has been assigned a value earlier.
Solution 2
When writing the comments, I thought that
syms x; solve(x==2*sin(x))
would return the expected result. At least in Matlab 2013b solve
fails to find a analytic solution for this problem, falling back to a numeric solver only returning one solution, 0
.
An alternative is
s = feval(symengine,'numeric::solve',2*sin(x)==x,x,'AllRealRoots')
which is taken from this answer to a similar question. Besides using AllRealRoots
you could use a numeric solver, manually setting starting points which roughly match the values you have read from the graph. This wa you get precise results:
[fzero(@(x)f(x)-g(x),-2),fzero(@(x)f(x)-g(x),0),fzero(@(x)f(x)-g(x),2)]
For a higher precision you could switch from fzero
to vpasolve
, but fzero
is probably sufficient and faster.
scadda
Updated on July 31, 2022Comments
-
scadda over 1 year
Lets say, I have a function 'x' and a function '2sin(x)'
How do I output the intersects, i.e. the roots in MATLAB? I can easily plot the two functions and find them that way but surely there must exist an absolute way of doing this.
-
scadda over 8 yearsWhy does it only find one solution and not all solutions? Is there a function that finds all roots in MATLAB?
-
Andras Deak -- Слава Україні over 8 yearsIf I'm not mistaken, MATLAB is short for MATrix LABoratory. It was designed for numerical problems and is best at solving those. Asking the question
where is my tricky symbolic function zero
is probably best left for programs designed for symbolic problems,like Mathematica or Maple. MATLAB does have a symbolic engine calledmupad
, but I don't trust it and never got to make use of it (of course this might just be my mistake). Long story short: if there's a way to get all zeros using matlab, I'm not aware of it. Specificallyfzero
finds one zero since it uses a kind of intersection method. -
Daniel over 8 yearsThe symbolic toolbox would be the right choice in this case. Is the toolbox available? Basically the first example for the solve function contains everything you need.
-
Andras Deak -- Слава Україні over 8 years@Daniel, as I said, I'm completely unfamiliar with that aspect of matlab. Can you provide an answer for that?
-
scadda over 8 yearsI do have access to the symbolic toolbox.
-
Andras Deak -- Слава Україні over 8 years@scadda, look at
doc solve
as @Daniel suggested. There are nice examples indeed. However, note that (at least according to the default behavior) it will not give you an infinite series of roots for trigonometric functions, e.g.syms x; solve(x==tan(x))
will only give you the root at the origin, andsolve(1==2*sin(x))
will only give you 2 roots.