MATLAB: How to use correctly bvp4c / bvp5c

bvp4cbvp5c

Hello,
How can I solve this boundaries value problem : y''(r) = (k*(k+1)/r^2)*y(r)-((2*k+1)/r)*f_1(r)*f_2(r)
where :
  1. r is defined as r=0:dr:r_max with dr and r_max given.
  2. y(r) is the function that I need to solve.
  3. k is a given integer parameter.
  4. f_1(r) and f_2(r) are two known functions.
The boundaries conditions are the following:
  1. y(0) = 0
  2. y'(r_max) = -(k/r_max)*y(r_max)
I have tried solving it using bvp5c but I failed :
if
function sol=myproblem(dr,r_max,k,f1,f2)
r=0:dr:r_max;
solinit=bvpinit([0 r_max],[0 0]);
sol=bvp5c(@odefun,@bcfun,solinit);
end
if
function dydr=odefun(r,y,k,f1,f2,dr)
c1=k*(k+1)./r.^2;
c1(1)=k*(k+1)./(dr/10)^2; % to avoid dividing by 0

c2=(2*k+1)*f1(:).*f2(:)./r;
c2(1)=0; % to avoid dividing by 0
dydr=[y(2);c1.*y(1)-c2];
end
if
function res=bcfun(ya,yb,k,r_max)
res=[ya(1);yb(2)+(k/r_max)*yb(1)];
end
In the code above, ignore the if, I can not remove them without loosing the code format.
thanks in advance,
David

Best Answer

Hi Torsten,
Thank you for your answer.
Somme addings regarding what I wrote : f1 and f2 have already the same length as r when I pass them into the function myproblem. I do not need to make an interpolation.
Matlab writes : Error in bvp5c : Not enough input argument.
David