Numerical integral with syms variables

Hi everybody,
I am trying to make numerical integral from a function which consists of several symbolic. It's clear that I want to make the integral with respect to a single symbolic variable (x). Can anybody help me doing this task?
many thanks in advance.
syms A B C x
f=@(x) A*x^2+B*x+C
integral(@(x) f, -1, 2)

Antworten (1)

Steven Lord
Steven Lord am 23 Sep. 2019

1 Stimme

If you had numeric values for A, B, and C then integral would be the right tool for the job. Note that nowhere in this code do I create a symbolic variable.
A = 1;
B = 2;
C = 3;
f = @(x) A*x.^2+B*x+C; % Added the period to vectorize f
result1 = integral(f, -1, 2)
I suspect you want the expression of the result to contain symbolic variables A, B, and C. If so, use int instead of integral. For this, I do need to create symbolic variables.
syms A B C x
f = A*x^2+B*x+C;
result2 = int(f, x, -1, 2)
We can confirm that result2 gives the same value as result1 when we substitute the values from the first section of code into the symbolic answer.
check = subs(result2, [A B C], [1 2 3])

6 Kommentare

Walter Roberson
Walter Roberson am 23 Sep. 2019
To expand slightly:
It is not possible to do a numeric integral of a symbolic function or expression that contains more than one unresolved variable. If you can get the expression down to one unresolved variable, such as by using subs() to give specific values for the other variables, then matlabFunction() can (often) transform the single-variable expression into an anonymous function that you could then use integral() with.
If you were integrating with respect to two variables (and all other symbolic variables had been resolved to specific values) then again you could use matlabFunction() to transform the two-variable expression into an anonymous function that you could then use integral2() with. I do, though, recommend that if you are planning to do this, that you use the 'vars' option of matlabFunction in order to force the free variables to appear in a particular order, as the default order can be a bit surprising.
Biyi Wang
Biyi Wang am 29 Jan. 2024
Dear Walter,
Your expand explanation is very useful.
However, if the number of unresolved variable increases, the speed of *int* will be much slower.
Are there any faster method to achieve the same goal?
Best regards,
Biyi
Walter Roberson
Walter Roberson am 29 Jan. 2024
If you have nested int() calls then typically it will search for the int() at each level; sometimes that works but often it does not. It can be beneficially to call int() with 'hold', 'on', and then to finally call release()
If you have nested int() calls, and the number of levels of int() is the same as the number of free variables, and you want to do a numeric evaluation, then change the int() to vpaintegral()
If you have nested int() calls and you want to use matlabFunction() then unfortunately the int() calls are turned into 1D integral() calls, which is slow. In the cast that you have exactly two variables to integrate over, then you are better off writing the formula to be integrated over as a symbolic expression, and matlabFunction() that, and integral2() the result.
Internally, MATLAB really only has 1D integrals and 2D integrals: all 3D and higher integrals are internally rewritten as 1D integrals if you ask for numeric output. (Well, except possibly for vpaintegral() -- it is not specified whether it rewrites to 1D or can use 2D.)
Dear Walter,
Thanks for your nice reply!
However, It's not the case that nested int() calls, just a 1D integrals (integral over variable t). See the following code.
clear
size = 3;
alpha = 1;
syms t real
x = sym('x', [size,1]);
wr = sym('wr', [size,1]);
wi = sym('wi', [size,1]);
assume(x>=-1 & x<=1)
fun = FUN(size,alpha,t,x,wr,wi);
fun1 = int(fun^2,t,-Inf,-1);
fun2 = int(fun^2, t,-1,1 ) + int( -2*fun, t,-1,1 ) + 2;
fun3 = int(fun^2,t,1,Inf);
function fun = FUN(size,alpha,t,x,wr,wi)
fun = 0;
for j = 1:size
fun = fun + (wr(j) + wi(j)*1i)/(t-x(j)*(1 + alpha*1i)) + (wr(j) - wi(j)*1i)/(t-x(j)*(1 - alpha*1i));
end
end
When the size <= 2, the results will be computed fast.
However, It's very slow to compute fun1 when size=3. (I spent the whole afternoon doing calculations on my laptop and still couldn't figure it out.) And I wish the value of size could be bigger.
If you have any suggestions to improve the efficiency of the code, I would be very appreciated!
I am looking forward to your reply.
Best regards,
Biyi
Walter Roberson
Walter Roberson am 29 Jan. 2024
The number of terms in the integration goes up as roughly (2 times size) quantity squared, and most of those terms involve division by t. The integration involves comparing each of the terms to each of the other terms, trying to figure out whether each can be used to complete the division derivative.
Biyi Wang
Biyi Wang am 31 Jan. 2024
Thanks for your nice reply!
Could you please make it more clear about this following comment?
"The integration involves comparing each of the terms to each of the other terms, trying to figure out whether each can be used to complete the division derivative."

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 23 Sep. 2019

Kommentiert:

am 31 Jan. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by