Find zero of a polynomial using fzero

12 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2020-10-6
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)

请先登录,再进行评论。

采纳的回答

Ameer Hamza
Ameer Hamza 2020-9-30
编辑:Ameer Hamza 2020-9-30
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by