From: Bruno Luong on 30 Mar 2010 13:02 James Allison <james.allison(a)mathworks.com> wrote in message <hot9lg$d67$1(a)fred.mathworks.com>... > roots will only provide the zeros of a polynomial, not x for a given > y=f(x). fzero can do this, but is a more general method that does not > take advantage of your function being a polynomial. As you mentioned, > there may be multiple solutions to y=f(x) for a polynomial; fzero will > find a solution that is near the starting point you specify. Here is an > example: > > % create a polynommial > y1 = [-1 8 17 19 10 -2 -7 -1 7 9]; > x1 = 1:10; > p = polyfit(x1,y1,5); > > % find values of x where polyval(p,x) = 5 > y = 5; x0 = 1; > f = @(x) polyval(p,x) - y; > x = fzero(f,x0) > > % try with other starting points: > x0 = 2; > x = fzero(f,x0) > > x0 = 5; > x = fzero(f,x0) > > x0 = 8; > x = fzero(f,x0) > > The results are: > > x = > 0.3507 > x = > 1.7616 > x = > 5.4230 > x = > 8.7112 > > which each correspond to a point where the polynomial is equal to y=5. >> p5=p p5 = -0.0387 1.0242 -9.4297 34.9149 -44.0166 16.5333 >> p5(end)=p5(end)-y p5 = -0.0387 1.0242 -9.4297 34.9149 -44.0166 11.5333 >> roots(p5) ans = 10.2075 8.7112 5.4230 1.7616 0.3507 No need to play around with x0. Bruno
From: James Allison on 30 Mar 2010 13:05
Thanks Walter and Bruno for setting me straight. :-) -James Bruno Luong wrote: > James Allison <james.allison(a)mathworks.com> wrote in message > <hot9lg$d67$1(a)fred.mathworks.com>... >> roots will only provide the zeros of a polynomial, not x for a given >> y=f(x). > > P(x) = f(x)-y is polynomial, and what is the root of P(x)? > > Bruno |