fzero problem when doing integration using quadl.

1 次查看(过去 30 天)
1. I am trying to do an integration using quadl, so I need to define an inline function first. But this inline function is somewhat complex. I need to use the function fzero to define it and this causes a lot of errors. I dont know how to fix it.
2. If there is no fzero, it would be very easy, I have to use fzero to find the value of cc and then to do integration. If anyone can help, thanks a lot.
dd=quadl(@kk,1,5);
function kk=kk(y)
aa=y.^2;
bb=@(w) w^5-2*w-4-aa;
cc=fzero(bb,3);
kk=cc+1;
The error is the following:
??? Operands to the and && operators must be convertible to logical scalar values.
Error in ==> fzero at 333 elseif ~isfinite(fx) ~isreal(fx)
Error in ==> kk at 4 cc=fzero(bb,3);
Error in ==> quadl at 70 y = feval(f,x,varargin{:}); y = y(:).';

采纳的回答

Andrew Newell
Andrew Newell 2012-1-6
You get those error messages because quadl expects a function that can input a vector but fzero can only solve for a scalar input. Replace fzero by fsolve.
EDIT: You also need to change the initial guess for fsolve to a vector:
function kk=kk(y)
bb=@(w) w.^5-2*w-4-y.^2;
cc=fsolve(bb,3*ones(size(y)));
kk=cc+1;
But don't expect the integration to work then. You're finding one of the five roots of a quintic equation, and fsolve could jump discontinuously as you change y.^2. Getting the correct answer may require some careful mathematics.
EDIT 2: If you run the following code
y = 1:.05:5;
polyRoots = zeros(5,length(y));
for ii=1:length(y)
polyRoots(:,ii) = roots([1,0,0,0,-2,-4-y(ii).^2]);
end
polyRoots(abs(imag(polyRoots))>10*eps)=NaN;
plot(y,polyRoots,'.')
you will see that there is only one real root between y=1 and y=5. So you're in luck!
  8 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by