Perform double integral of a symbolic equation?
3 次查看(过去 30 天)
显示 更早的评论
I have the following code producing a 10x10 matrix of symbolic equations (B):
syms x y
phi(1)=x*y*(1-x)*(1-y);
phi(2)=x*phi(1);
phi(3)=y*phi(1);
phi(4)=(x^2)*phi(1);
phi(5)=x*y*phi(1);
phi(6)=(y^2)*phi(1);
phi(7)=(x^3)*phi(1);
phi(8)=(x^2)*(y^2)*phi(1);
phi(9)=(y^3)*phi(1);
phi(10)=(x^4)*phi(1);
for s=1:1:10
dphidx(s)=diff(phi(s),x);
end
for t=1:1:10
dphidy(t)=diff(phi(t),y);
end
for i=1:1:10
for j=1:1:10
B(i,j)=dphidx(i)*dphidx(j)+dphidy(i)*dphidy(j);
end
end
I now want to perform a double integration of each equation on 0<x<1 and 0<y<1 to get a new 10x10 matrix (A) of numbers:
The "integral2" command doesn't seem to like the symbolic format of the equations in B. Is there some way to convert B to a form that this command will accept? Or is there another integration command that will work to calculate matrix A from matrix B?
0 个评论
回答(1 个)
John D'Errico
2017-4-15
Simple. You have created all of these symbolic elements of a matrix. Use int! It does integration. Some things to think about:
It is time for you to learn about preallocation. Don't expand the matrix B one element at a time. This is terribly inefficient, certainly so as your matrices will grow larger. And it is trivial to solve.
Add this in the beginning of your code:
B = zeros(10,10,'sym');
Is there a good reason why you cannot create each element of A in the same loop that creates B? All it requires is a nested call to int, once in x, then in y. WTP?
Yes, you could use arrayfun. But creating A(i,j) requires one extra line of code. (Actually, two lines, because you also want to preallocate A.) And arrayfun won't be that much more efficient, because it is just an implicit loop. The work is in the integrations themselves.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!