Find zero of a polynomial using fzero
13 views (last 30 days)
Show older comments
Rachel A. Eaglin
on 30 Sep 2020
Commented: Walter Roberson
on 6 Oct 2020
I am trying to find the zeros of deflecPrime using fzero (not roots).
deflecPrime=(w/(48*E*I)).*[8 -15*L 6*L^2 0];
%zero of deflection derivative
fun=@deflecPrime;
x0=L/2;
maxDeflec=fzero(fun,x0)
I keep getting this error:
FZERO cannot continue because user-supplied function_handle ==> deflecPrime failed with the
error below.
Undefined function 'deflecPrime' for input arguments of type 'double'.
1 Comment
Walter Roberson
on 6 Oct 2020
By the way, with respect to your recent question that you deleted:
[X,Y]=meshgrid(x,y);
X and Y will both be 2D arrays.
f=2.25*X.*Y+1.75*Y-1.5*X.^2-2*Y.^2;
f is built from 2D arrays, so f will be a 2D array.
fun = @(z)-2.25*X.*Y-1.75*Y+1.5*X.^2+2*Y.^2;
You accept an input but ignore it and always return the same thing that was calculated and stored into f.
You could change the @(z) to @(f) but that would not make any difference.
z = fminsearch(fun,x0)
the function you pass to fminsearch must return a scalar, not an array.
I suspect what you want is
fun = @(XY)-2.25*XY(1).*XY(2)-1.75*XY(2)+1.5*XY(1).^2+2*XY(2).^2;
Note, by the way, that you can calculate the optimum using calculus instead of fminsearch. When f is of the form
A*X.*Y + B*Y - C*X.^2 - D*Y.^2
then
x = -(A*B)/(A^2 - 4*C*D)
y = -(2*B*C)/(A^2 - 4*C*D)
is the critical location. (Caution: you really should check whether it is a minima or maxima)
Accepted Answer
Ameer Hamza
on 30 Sep 2020
Edited: Ameer Hamza
on 30 Sep 2020
You need to define deflecPrime as a function handle
deflecPrime = @(L) sum((w/(48*E*I)).*[8 -15*L 6*L^2 0]); % sum all the terms too
and then call fzero like this
fun = deflecPrime;
x0=L/2;
maxDeflec=fzero(fun,x0)
However, fzero will not give al the roots. For that, use roots(): https://www.mathworks.com/help/matlab/ref/roots.html.
deflecPrime = (w/(48*E*I)).*[6 -15 8] % [cofficients of x^2, x, 1]
roots(deflecPrime)
3 Comments
More Answers (0)
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!