fzero problem when doing integration using quadl.

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 日

0 投票

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 件のコメント

Lin LI
Lin LI 2012 年 1 月 7 日
Thank you very much for your answer, Andrew. The w in the equation w.^5-2*w-4-y.^2=0 is implicit. If it is explicit, it will be very easy.Then how can I fix this problem? Is there other way to solve this problem? What can I do? I have to do the integration. Thank you very much.
Andrew Newell
Andrew Newell 2012 年 1 月 7 日
Before you can be sure you have the right answer, you need to know which of the five roots you need, and why. Perhaps it would help to back up a step and look at how you derived the expression for the integral.
Walter Roberson
Walter Roberson 2012 年 1 月 7 日
The answer is 11.02028032, and 5.785255654+6.576584145*i, and -1.295395817+3.550130217*i, and -1.295395817-3.550130217*i, and 5.785255654-6.576584145*i . Take your pick.
The primary root is "close to" linear over y=1 to 5. At least visually, but closer examination shows that it is not _really_ linear.
Lin LI
Lin LI 2012 年 1 月 8 日
Thank you for your answer, Andrew and Walter. Following Andrew's suggestion, I only got a value of 11.02, not five values. why? Am i missing something?
Walter Roberson
Walter Roberson 2012 年 1 月 8 日
fsolve() only ever finds one root.
Also, "fsolve only handles real variables. When x has complex variables, the variables must be split into real and imaginary parts."
To get multiple roots in a single optimization call, you would need to use the Global Optimization Toolbox MultiStart() routine.
To get the roots for a _known_ yy, you could use roots([1,0,0,0,-2,-4-y.^2])
but fsolve() is not going to be able to handle returning multiple solutions.
Lin LI
Lin LI 2012 年 1 月 8 日
Thank you very much, Walter. if fsolve only handles real variables and I only need real variable results and I am sure the real variable result exists, does that mean I am going to get the right result using fsolve?
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