3

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.

das-g
  • 9,718
  • 4
  • 38
  • 80
scadda
  • 31
  • 1
  • 1
  • 5

2 Answers2

3

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.

  • 1
    Why does it only find one solution and not all solutions? Is there a function that finds all roots in MATLAB? – scadda Aug 16 '15 at 14:21
  • 1
    If 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 called `mupad`, 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. Specifically `fzero` finds one zero since it uses a kind of intersection method. – Andras Deak -- Слава Україні Aug 16 '15 at 14:27
  • The 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. – Daniel Aug 16 '15 at 14:34
  • @Daniel, as I said, I'm completely unfamiliar with that aspect of matlab. Can you provide an answer for that? – Andras Deak -- Слава Україні Aug 16 '15 at 14:36
  • I do have access to the symbolic toolbox. – scadda Aug 16 '15 at 14:39
  • @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, and `solve(1==2*sin(x))` will only give you 2 roots. – Andras Deak -- Слава Україні Aug 16 '15 at 14:57
3

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.

Community
  • 1
  • 1
Daniel
  • 36,610
  • 3
  • 36
  • 69