Integral Function inside while loop only works for certain values. ArrayValued results in empty volumn vectors.

I am having issues with the integral function only working for certain values of K.
I am looping through an interative summation where I calculate the nth solution of tan(L*z) = -K/h*z. The nth solution is a term in the functions defined by fun and fun2. If the value of K is set to 0.04, the program runs just fine. If I set the value of K to 20, the program throws the error
"Output of the function must be the same size as the input. If FUN is an array-valued integrand, set the 'ArrayValued' option to
true."
If I set the integral function to 'ArrayValued', true, then the code will run for the first iteration of the while loop, but in the second iteration the integral function will return an empty 0x1 vector.
while deltaW> 0.1 && n<1000
S= double(vpasolve(tan(L*z)== -K/h*z,z, [sol sol+pi/2/L]));
if isempty(S) ==1 %If empty reads false happens when there is a solution
sol = sol+pi/2/L;
S= vpasolve(tan(L*z)== -K/h*z,z, [sol sol+pi/2/L]);
sol=single(S);
end
lambdan=double(S);
sol = lambdan+0.01;
fun = @(x) ((Tinit-(T0 - h*(T0 - Tinf)/(K + h*L).*x))) .* sin(x.*lambdan);
fun2 = @(x) sin(x.*lambdan).^2;
anumerator = integral(fun,0,L);
adenominator = integral(fun2,0,L);
An = anumerator/adenominator;
Wn= An .* sin (lambdan.*x) .* exp(-lambdan.^2.*K.*time);
if n==1
W=Wn;
end
if n>1
deltaW = max(abs(W-Wn));
W=W+Wn;
end
n=n+1;
end

Answers (1)

You probably want to recompute S while it is empty not if it is empty. Otherwise S could still be empty after the second vpasolve call.

Products

Release

R2020b

Asked:

on 3 Mar 2021

Commented:

on 3 Mar 2021

Community Treasure Hunt

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

Start Hunting!