how to speed up symbolic integration or turn this to numerical integration
6 views (last 30 days)
Show older comments
Weilin Piao
on 12 Apr 2023
Commented: Weilin Piao
on 13 Apr 2023
Hello, I am new to matlab and I am trying to use int to integrate my symbolic matrix but it is taking too much time to execute.
Here is my code:
%% form shape functions and its matrix
syms m
Ni=sym(length(n)); %n is pre-defined vector and depends on user input
N=sym(zeros(2,length(n)));
for ii=1:length(n)
Ni(ii)=1;
for j=1:length(n)
if j~=ii
Ni(ii)=Ni(ii) * ((m - n(j)) / (n(ii) - n(j)));
end
end
N(1,2*ii - 1)=Ni(ii);
N(2,2*ii)=Ni(ii);
end
%% Jacobian
J=l/2;
%% form matrices B1 and B2
b1=[1 0;0 0;0 1];
b2=(1/J) * [0 0;0 1;1 0];
B1=b1 * N;
B2=b2 * diff(N,m);
%% form SBFEM coefficient matrices
E0=double(int(B1' * D * B1 * J , m , -1 , 1));
E1=double(int(B2' * D * B1 * J , m , -1 , 1));
E2=double(int(B2' * D * B2 * J , m , -1 , 1));
Would appreciate if someone can help :)
1 Comment
Accepted Answer
John D'Errico
on 12 Apr 2023
Edited: John D'Errico
on 12 Apr 2023
As a suggestion, it is not at all uncommon to use a Gauss-Legendre quadrature for these things. Why? The kernel should be polynomial in m. Therefore as long as you use the correct order Gauss-Legendre quadrature, the result will be exact. It is already set up on the correct interval for Gauss-Legendre, so there is no need to transform the weights and nodes to match. Remember that for p nodes, a Gauss-Legendre rule will be order 2*p-1. The beauty is, Gauss-Legendre will run like blazes here compared to a symbolic computation.
help sym.legendreP
The nodes and weights can be derived from the Legendre polynomials.
I recall there are multiplye tools on the FEX that will compute the nodes and weights for such a rule. I see the chebfun toolbox has one (legpts).
format long g
[nodes,W] = legpts(5)
nodes =
-0.906179845938664
-0.538469310105683
0
0.538469310105683
0.906179845938664
W =
Columns 1 through 3
0.236926885056189 0.478628670499366 0.568888888888889
Columns 4 through 5
0.478628670499366 0.236926885056189
But they are also pretty easy to generate too. The wiki page I show should have what you need. As well, they actually have a table of the nodes and weights for the first few rules, and you don't need a high order for it to be exact, as I suggest above.
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!