How to evaluate an array filled with symbolic integrals?

3 次查看(过去 30 天)
I managed to use symbolic math for calculating a bunch of integrals (which have all the same structure but differs only on a few parameters) one by one like this:
syms x y s ;
I = zeros(3,3);
% 1)
t = 2e-3;
s_f = 0.3;
x = 0.42;
y = -0.19 + s;
I(1,1) = int((t*y^2), s, 0, s_f);
I(2,1) = int((t*x^2), s, 0, s_f);
I(3,1) = int((t*x*y), s, 0, s_f);
% 2)
t = 2e-3;
s_f = 0.6;
x = 0.42 - s;
y = 0.11;
I(1,2) = int((t*y^2), s, 0, s_f);
I(2,2) = int((t*x^2), s, 0, s_f);
I(3,2) = int((t*x*y), s, 0, s_f);
% 3)
t = 1e-3;
s_f = 0.3;
x = -0.18;
y = 0.11 - s;
I(1,3) = int((t*y^2), s, 0, s_f);
I(2,3) = int((t*x^2), s, 0, s_f);
I(3,3) = int((t*x*y), s, 0, s_f);
Result being:
I =
5.4600e-006 14.5200e-006 2.7300e-006
105.8400e-006 53.2800e-006 9.7200e-006
-10.0800e-006 15.8400e-006 2.1600e-006
But now I'd like to make it a bit more concise and elegant. I thought I could define t,s_f,x,y as vectors and build the resulting I array someway; I read docs and tutorials ... but I'm lost. I cannot figure out how to deal with it.
I'd like to come to something like this (which is pseudo-code only):
T = [2e-3 2e-3 1e-3 ];
X = [(0.42) (0.42-ss) (-0.18) ];
Y = [(-0.19+ss) (0.11) (0.11-ss)];
S_F = [0.3 0.6 0.3 ];
I = int((T*Y^2), S, 0, S_F);
in which I define the parameters I need first, and later build the I array in some concise way (without duplicating the same code over and over ...) , resulting in the same I array as above. But I cannot figure out a way.
Tried several constructs/functions/syntax but without success. I surely have to learn more about that symb-math.
Could you please point me in the correct direction? Is this kind of expressions even thinkable with Matlab?
I am new to Matlab, so please excuse me if I result being naive.

回答(2 个)

madhan ravi
madhan ravi 2018-12-1
编辑:madhan ravi 2018-12-1
Numerical integration is faster compared to symbolic integration. See matlabFunction() to convert symbolic expressions to function handle thereby achieving to adapt to numerical methods.
syms ss
T = [2e-3 2e-3 1e-3 ];
X = [0.42 0.42-ss -0.18 ];
Y = [-0.19+ss 0.11 0.11-ss];
S_F = [0.3 0.6 0.3 ];
func=@(T,Y)(T.*Y.^2);
func=func(T,Y);
func=matlabFunction(func);
I=cell(1,numel(func)); %pre-allocation
for i = 1:numel(func)
I{i} = integral(@(ss)func(i), 0, S_F(i),'ArrayValued',true);
end
final_result = vpa([I{:}])
command window:
>> COMMUNITY
final_result =
[ 0.00039365999999999999516692161805054, 0.0000072599999999999982526911052049812, 0.00023762999999999995931476703958651]
>>
  3 个评论
madhan ravi
madhan ravi 2018-12-1
编辑:madhan ravi 2018-12-1
so remove the line with matlabFunction integral and replace it with int() , it would be the same approach that i showed you

请先登录,再进行评论。


YT
YT 2018-12-1
I don't know if this is exactly what you're looking for, but I would do something like this
clear all;
syms ss;
T = [2e-3 2e-3 1e-3];
X = [(0.42) (0.42-ss) (-0.18)];
Y = [(-0.19+ss) (0.11) (0.11-ss)];
S_F = [0.3 0.6 0.3];
I = zeros([3 length(T)]); %3-by-amount-of-data-columns
for i = 1:length(T)
I_ty = int((T(i)*Y(i)^2), ss, 0, S_F(i));
I_tx = int((T(i)*X(i)^2), ss, 0, S_F(i));
I_xy = int((T(i)*X(i)*Y(i)), ss, 0, S_F(i));
I(:,i) = [I_ty; I_tx; I_xy];
end
If you for some reason ever want to add more data to T, X, Y, S_F, you can just simply add it to those columns and it still works fine:
T = [2e-3 2e-3 1e-3 4e-3];
X = [(0.42) (0.42-ss) (-0.18) (-0.20)];
Y = [(-0.19+ss) (0.11) (0.11-ss) (0.33+ss)];
S_F = [0.3 0.6 0.3 0.4];
  1 个评论
Roberto Inzerillo
Roberto Inzerillo 2018-12-1
I thought about using "for" loops too. It's just that it doesn't look "elegant" enough. I was fishing for a more Mathlab-ish solution if you know what I mean :-)
But you're on point. Those few lines do the job I was expecting.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by