2

I would like to solve the following polynomial numerically for r:

I am trying to use fzero() as follows:

r = (5/(r^2*9))- ((2)/(9*(6-r)^2))
x0 = 10; % some initial point
x = fzero(r,x0)

How can this be done with fzero()?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Mixter
  • 193
  • 9

2 Answers2

2
  • Input variable and function name should be different
  • Just change the function name to pol
  • To use fzero the function pol have to be a function handle defined via @
pol =@(r) (5/(r^2*9))- ((2)/(9*(6-r)^2))
x0 = 10; % some initial point
x = fzero(pol,x0)

solution

x =  3.6754
Adam
  • 2,726
  • 1
  • 9
  • 22
  • Though it is good practice to use a different name for the function handle and for the anonymous variable (and I highly recommend doing this), it is not necessary. But you were missing the `@` indicating that `r =` is a function handle; and you were missing a `(r)` just after the @ to say that you have an **anonymous** function handle and that `r` should be considered as an input *variable*. i.e. `r = @(r) (5/(r^2*9))- ((2)/(9*(6-r)^2))` would have worked but it is confusing to use `r` here twice, just as @Adam explained – max Dec 14 '19 at 16:35
2

It should be noted that, the first argument in fzero() should be "a function handle, inline function, or string containing the name of the function to evaluate", but yours is just an expression, which is not valid.

Besides the approach by @Adam (using function handle), another way is to use anonymous function, i.e.,

 x = fzero(@(r) (5/(r^2*9))- ((2)/(9*(6-r)^2)) ,x0)

where

@(r) (5/(r^2*9))- ((2)/(9*(6-r)^2))

is the anonymous function with respect to argument r.

You will get the same result that x = 3.6754.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81