'integral' with piecewise expressions
12 次查看(过去 30 天)
显示 更早的评论
I have the following expression for the angle Beta:
L = 1;
syms y
Beta = piecewise(0<=y<L/2, pi/2, ...
L/2 <=y <L*(3/4),pi/2+0.3491, ...
(3/4)*L <=y <=L,pi/2-0.3491);
i want to compute this integral:
a = pi/4;
fun_f=@(y) (1./(50*(1+y)*(cos(a)*cos(Beta)+sin(a)*sin(Beta))+470));
int= integral(fun_f,0,L)
Does anybody know how to calculate the integral with using integral, but with Beta defined as a piecewise function?
I looked for documentation for piecewise but I don't really know if it's needed to use it. I'd prefer not to.
If you need further information i'll be happy to provide it in order to solve my problem
0 个评论
采纳的回答
Torsten
2023-1-13
编辑:Torsten
2023-1-13
L = 1;
Beta = @(y,L) pi/2.*((0<=y) & (y<L/2)) + (pi/2+0.3491).*((L/2<=y) & (y<3/4*L)) + (pi/2-0.3491).*((3/4*L<=y) & (y<=L));
figure(1)
plot((0:0.01:L),Beta(0:0.01:L,L))
a = pi/4;
fun_f=@(y,L) (1./(50*(1+y).*(cos(a)*cos(Beta(y,L))+sin(a)*sin(Beta(y,L)))+470));
figure(2)
plot((0:0.01:L),fun_f(0:0.01:L,L))
format long
int_value= integral(@(y)fun_f(y,L),0,L)
int_value_improved = integral(@(y)fun_f(y,L),0,L/2) + integral(@(y)fun_f(y,L),L/2,3*L/4) + integral(@(y)fun_f(y,L),3/4*L,L)
5 个评论
Torsten
2023-1-14
编辑:Torsten
2023-1-14
I doubt that Beta is what you want since the result is of type "logical".
What function do you want to use (in a mathematical notation) ?
But if you think everything is as wanted - here is the result:
LATO = 1;
intersezione_34LATO= (3/4)*LATO;
i = 1;
p = [0.125000000000000 0];
L_AB= 0.625000000000000;
alpha_1 = pi/2;
Beta = @(l,p,alpha_1,LATO) (pi/2+0.3491).*(LATO/2<=(p(i,1)+l.*sin(alpha_1(i)))) & ((p(i,2)+l.*sin(alpha_1(i)) <intersezione_34LATO)) + (pi/2-0.3491).*(intersezione_34LATO<=(p(i,2)+l.*sin(alpha_1(i)))) & ((p(i,2)+l.*sin(alpha_1(i))<= LATO));
fun_f=@(l) (1./(cos(double(Beta(l,p,alpha_1,LATO)))));
l=0:0.01:L_AB;
plot(l,fun_f(l))
tau_f(i) = integral(@(l)fun_f(l),0,L_AB(i))%,'AbsTol',0,'RelTol',1e-20,'ArrayValued',false)
更多回答(1 个)
Walter Roberson
2023-1-14
Use matlabFunction with the piecewise expression, giving the 'file' option and 'optimize' false. And when you integral specify 'arrayvalued' true
matlabFunction can convert piecewise to if/else but only when writing to file, and the result cannot accept vectors
2 个评论
Walter Roberson
2023-1-14
format long g
L = 1;
syms y
Pi = sym(pi);
Beta = piecewise(0<=y<L/2, Pi/2, ...
L/2 <= y < L*(3/4), Pi/2 + sym(3491)/10^4, ...
(3/4)*L <= y <=L, Pi/2 - sym(3491)/10^4);
a = Pi/4;
fun_f = (1./(50*(1+y)*(cos(a)*cos(Beta)+sin(a)*sin(Beta))+470))
result_symbolic = int(fun_f, y, 0, L)
result_vpa = vpa(result_symbolic, 16)
fun_f_h = matlabFunction(fun_f, 'vars', y, 'File', 'fun_f.m', 'optimize', false)
result_numeric = integral(fun_f_h, 0, L, 'arrayvalued', true)
dbtype fun_f.m
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!