MATLAB: Piecewise for use in anonymous functions

piecewise

Hello,
clc,clear all,close all;
opengl('save', 'software')
syms beta theta r ;
B = 10;
H = 10;
C = 10;
L1 = 4;
Ri = B/2 + L1;
Rf = C + B/2 + L1;
Rbeta0 = Ri;
theta1 = atan((H/2)/(B/2+L1));
theta2 = pi - atan((H/2)/(B/2-L1));
Rt0 = piecewise((0<=theta) & (theta<theta1),(B/2+L1)/cos(theta),(theta1<=theta) & (theta<theta2),(H/2)/sin(theta),(theta2<=theta) & (theta<=pi),(B/2-L1)/cos(pi-theta));
Rmaxtb0 = (Rt0*Rbeta0)/Ri;
Rmaxtb00 = matlabFunction(Rmaxtb0)
Vmbeta0 = (2*(pi^2)*((B+(2*L1))^2)) / ((((2*L1*pi)+(pi*B))^2));
Vbeta0 = Vmbeta0 * (1 - ((r^2) / (Rmaxtb0^2)));
Vbeta00 = Vbeta0 * r;
Vbeta000 = matlabFunction(Vbeta00,'Vars',[theta r]);
Wsigmat = 2 * integral2(Vbeta000,0,pi,0,Rmaxtb00,'AbsTol',1e-5, 'RelTol',1e-5)
Error using symengine
Unable to generate code for piecewise for use in anonymous functions.
As you can see in my piecewise function there is 'theta' in the limits which is defined as 'syms' and is not defined prior
and I think it couldn't be written as if/else!! (I don't know if it can)
Is there any way to define a piecewise function as an anonymous function?
or any way to use an alternative for piecewise function??
thanks in advance for any help

Best Answer

Rmaxtb00 = matlabFunction(Rmaxtb0, 'File', 'Rmaxtb00.m', 'optimize', false);
The generated function in the .m file will use if/elseif to code the piecewise()
However, the generated if/elseif code will not be vectorized, so when you use it with integral() you need to use 'ArrayValued', true so that integral() does not pass in a vector of values to be evaluated.