fzero problem when doing integration using quadl.

조회 수: 3 (최근 30일)
Lin LI
Lin LI 2012년 1월 6일
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
Andrew Newell
Andrew Newell 2012년 1월 8일
Yes - see above.
Lin LI
Lin LI 2012년 1월 9일
Thanks a lot,Andrew and Walter.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by