Solving symbolic equations that contain numeric integration

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);

 采纳的回答

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.

4 个评论

Thanks, Walter.
1. I am going to program "for..end" loops to perform integration and get an eventual expression that is a mix of sine/consine and unknown parameters. (Since my expression doesn't have multinomial as what you mentioned, symbolic integration won't work).
2. thanks for correcting me on the subs().
3. thank you for pointing out fmincon() will not work for symbolic expressions. I did use matlabFunction to convert, but the error says that the function handle created has too many input argumnets for fmincon(). What does this mean?
Thanks!
What kind of expressions do you have inside of your trig calls? I just did some quick experimentation that was *suggestive* that multiplying together any number of sin(f(x)) and cos(f(x)) formula can be symbolically integrated provided that each f(x) is a polynomial of degree at most 2. And naturally sin(g(t)) becomes just a constant factor for any complexity of g(t) if the integration is not relative to t.
The function you generate using matlabFunction will have one input for each symbolic variable, and is therefor only useful for fmincon for integration of the "last" formula (i.e., expected to produce a numeric result.) If that is suitable for your case then you can use a wrapping anonymous function to give a single-input function handle that delivers appropriate values to the multi-input function matlabFunction created. For example,
wrapper = @(x) Generated(x, p, q, r);
Note that when you do this, the _current_ values of p, q, and r would be remembered inside of the anonymous function handle, and changing p, q, or r afterward would not change the values delivered to the generated function. (If you need to change the delivered values, you would have to re-created the wrapper anonymous function.)
equations were like:
beta_quote=atan(tan(beta)/((tan(alpha))^2+1));
theta=pi/2-beta_quote;
P2=(3*(cos(beta))^2-1)/2;
P4=(35*(cos(beta))^4-30*(cos(beta))^2+3)/8;
f_beta=exp(-(lamda2*P2+lamda4*P4));
A_p_1130=(alpha_c*(c*(sin(theta))^2*(sin(x))^2+a*(sin(phi)*cos(x)+cos(theta)*cos(phi)*sin(x))^2 ...
+b*(cos(phi)*cos(x)-cos(theta)*sin(phi)*sin(x))^2)^2+(1-alpha_c)*(-2*d*(sin(phi)*cos(x)...
+cos(theta)*cos(phi)*sin(x))*(cos(phi)*cos(x)-cos(theta)*sin(phi)*sin(x)))^2)*f_beta*sin(beta);
In your wrapper example: there are two x on the right side, are they different?
do I have to use both matlabFunction or anonymous function in that case? or I only need to choose one?
wrapper = @(x) Generated(x,p,q,r)
makes wrapper a function handle designating a function that takes a single argument named x, whose result is calculated by calling Generated(x,p,q,r) where p, q, and r have the values that they had when the function handle was generated, and x is the value that was passed to the function call.
Your generated matlabFunction will almost certainly be a function in multiple variables, one for each undefined symbol. In order to call that generated function within fmincon you need to provide concrete values for each of those variables, but fmincon only provides a single argument to the user function it calls upon. The solution is to create a wrapper function that provides the values for all of the variables except the one being minimized.
Will you be trying to minimize the function over all four variables simultaneously? If so, then the wrapper function might look like,
wrapper = @(x) Generated(x(1),x(2),x(3),x(4));

请先登录,再进行评论。

更多回答(1 个)

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?
If the result of integration contains free parameters, you cannot use numeric integration. Quadrature (i.e., numeric integration) can only return a number, and you may be able, depending on your problem, to get an approximation to the formula you are looking for by interpolating from numerical answers to parameter values, but numerical quadrature by itself will not be able to tell you that the result is something like 0.1234*sin(a)+5.6. Also note that quadrature is, necessarily, limited to definite integration.
What Walter said, is correct, though: The number of complicated formulas for which a symbolic integral exists (let alone having an algorithm/a computer program that can find them) is not all that big really.

6 个评论

A couple of months ago, I read a simple proof that there are as many formulas that can be integrated as there are that cannot. Generate a random expression from components each of which are individually differentiable; through the chain rule, the overall formula will be differentiable. The coefficients possible for those formula include the continuous reals, so there are there will be Aleph One such differentiable formulas, and each of the differentials is a formula that can be integrated. As there is no known infinity larger than Aleph One, there cannot be a larger infinity of formulae that cannot be integrated.
Although this proof of relative numbers is clear, still there are a large number of formulae that are "interesting" to us that there is no way to integrate. That is partly, though, because the ones that are easy to integrate tend to stop being "interesting".
Thanks, christopher.
I am going to program "for..end" for integration since my expression is long mixed with sine/cosine. In this way, I will get an expression with symbles after integration. Now I get stuck with fmincon(). I tried different approaches but all showed different errors.
#1 codes are:
out=(f1-1)^2+(f2-1)^2+(f3-1)^2+(f4-1)^2;
x1=sym('x1',[1 4]);
subs(out,{x,phi,lamda2,lamda4},{x1(1),x1(2),x1(3),x1(4)});
out1=matlabFunction(out);
x0=[pi/6 pi/6 -1 -2];
lb=[0 0 -20 -20];
ub=[2*pi 2*pi 0 0];
[x1,fval,exitflag,output]=fmincon(out1,x0,[],[],[],[],lb,ub);
Errors are:
??? Error using ==> sym.matlabFunction>@()NaN
Too many input arguments.
Error in ==> fmincon at 574
initVals.f = feval(funfcn{3},X,varargin{:});
Error in ==> solveequs_new at 77
[x1,fval,exitflag,output]=fmincon(out1,x0,[],[],[],[],lb,ub);
Caused by:
Failure in initial user-supplied objective
function evaluation. FMINCON cannot continue.
#2: Codes are
out=(f1-f_p_1130)^2+(f2-f_p_1296)^2+(f3-f_c_1130)^2+(f4-f_c_1296)^2;
x1=sym('x1',[1 4]);
subs(out,{x,phi,lamda2,lamda4},{x1(1),x1(2),x1(3),x1(4)});
x0=[pi/6 pi/6 -1 -2];
lb=[0 0 -20 -20];
ub=[2*pi 2*pi 0 0];
[x1,fval,exitflag,output]=fmincon(@(x1)out,x0,[],[],[],[],lb,ub);
eerors are
??? Error using ==> sym.sym>notimplemented at 2621
Function 'ge' is not implemented for MuPAD symbolic
objects.
Error in ==> sym.sym>sym.ge at 823
notimplemented('ge');
Error in ==> nlconst at 739
elseif f >=0
Error in ==> fmincon at 758
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in ==> solveequs_new at 76
[x1,fval,exitflag,output]=fmincon(@(x1)out,x0,[],[],[],[],lb,ub);
I understand I need to convert symbolic expression to function. But either of above is now working. Can you tell me why?
Thanks!
Lu
As Walter already pointed out in his answer, your subs call is not doing anything, since you simply discard its result. subs(a, whatever) does *not* change a as a side-effect. Not seeing f1 through f4, it's difficult to say more. (If you wish to give them, please try editing your question, that would be much easier to read.)
@Walter: The number of formulas which can be written in finite length over a finite alphabet that is integrable is, of course, countably infinite and therefore as large as the set of all such formulas. But even without a formal proof, I'm pretty confident that, given a “randomly chosen” enumeration of these formulas, the asymptotic density of formulas integrable in finite terms would be small, probably zero, almost surely. Differentiating a randomly selected formula very often results in a much larger one.
What is a "formula integrable in finite terms" ? It is not uncommon for the integration of formulas involving rational constants to result in irrational numbers that we do not happen to have a finite name for.
At least for anything that comes anywhere close to Liouvillian field towers, the only constants required are algebraics, and we have names for them, as in sum(b*ln(...), b in RootOf(...)).
A “formula integrable in finite terms” is a formula f where there is, in the notation we use (the term obviously depends on that notation), a formula g such that g'=f. If such a g would require constants that cannot be represented, f is not integrable in finite terms.
But I'm not sure I see how to construct such an f, especially given that I'd happily accept finite-length descriptions involving infinite sums etc. for the constants, since they are part of the CAS vocabulary.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by