MATLAB: Solving symbolic equations that contain numeric integration

integrationoptimizationsymbolicSymbolic Math Toolbox

I am trying to solve several equations simultaneously. These equations have multiple variables, complex sine/cosine and triple integration. I have problems in both integration and equation solving. Here are my questions.
1. About integration: after integration, I need to get a function that still has several unknown parameters. should I use symbolic integration "int()" or numeric integration "dblquad()" if the function expression is really complex?
2. about equation solving: which way is faster, symbolic "solve()" or numeric like "fmincon()"? Here is my code for using "fmincon()". Is the logic right?
%%f1,f2,f3,f4 are four functions with four unknown variables x phi lamda4 lamda
out=(f1-1)^2+(f2-2)^2+(f3-3)^2+(f4-f4)^2;
syms x1
subs(out,x,x1(1));
subs(out,phi,x1(2));
subs(out,lamda2,x1(3));
subs(out,lamda4,x1(4));
x0=[pi/6 pi/6 -1 -2];
lb=[0 0 -20 -20];
ub=[2*pi 2*pi 0 0];
syms x1 clear;
[x1,fval,exitflag,output]=fmincon(out,x0,[],[],[],[],lb,ub);

Best Answer

If the function expression is really complex, then it becomes improbable that symbolic integration will be able to find a closed form solution, in which case numeric integration would be necessary anyhow. If, though, your f1, f2, f3, and f4 are multinomials (that is, can be written as the sum of terms that involve multiplying together non-negative integral powers of the variables), then expand(out) will produce an expression that can be mechanically integrated, which would be faster than numeric integration.
I would suggest, though, that in your expression for "out" that you want (f4-4)^2 instead of (f4-f4)^2
With regards to solving: it depends on the functions involved and their powers. If the expression can be rewritten as a polynomial in degree at most 4, or if it contains some other functions such as exp() or GAMMA() used in fairly narrow ways, then there are standard solutions which solve() can find faster than numeric solving would work. Symbolic solutions can also be important if all of the solutions must be found. If, though, it is simply important to find some solution over a known range (or it is known that only one solution exists in that range) then numeric solutions can end up being much much faster.
There is another consideration that comes in to play at times, and that is whether to use a solver at the Matlab level or to use one of the numeric solvers built in to the symbolic toolbox. For some problems, it can be critical to use the symbolic toolbox for numeric solutions, as the symbolic toolbox is able to use ranges of numbers beyond those available in IEEE binary floating point, and the symbolic toolbox is able to operate at extended accuracies (if you set the Digits higher.) When using higher-order polynomials or distributions near the tail ends, binary floating point calculations often slip in to numeric nonsense through loss of precision.
Your subs() code is not correct, as you are not assigning the results of the substitution to anything; the substituted expression is produced but then discarded before the next substitution starts. You can also combine several subs() in to one expression:
out = subs(out, {x,phi,lambda2,lambda4},{x1(1),x1(2),x1(3),x4(4)});
I am not certain, but I do not believe that fmincon can be applied to a symbolic expression. You may need to use matlabFunction to produce a function that fmincon can use from the expression.
Related Question