Perform double integral of a symbolic equation?

2 ビュー (過去 30 日間)
Justin Bruh
Justin Bruh 2017 年 4 月 15 日
回答済み: John D'Errico 2017 年 4 月 15 日
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?

回答 (1 件)

John D'Errico
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.

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by