How to compute the difference of the integrals of two functions (f(x) and j(x)) only over the portions where f(x)>j(x)

조회 수: 2 (최근 30일)
I would like to compute the difference of integrals of two functions (f(x), shown in thick black, and j(x), shown in thick red (portion shaded in light red), only over the portion where 0<x<86400 and where f(x)>j(x), as indicated in the graph.
Any quick solution? It's been a bit a struggle.
Thank you in advance.
Best regards,
Tim.
(Code given below).
%Part of no interest for the question (given for the code to run)
Tmean_out=30;
Tmax_out=40;
Tadaptive=32;
meg=((2*pi)/86400);
DeltaT_out=Tmax_out-Tmean_out;
Tmin_out=Tmean_out-DeltaT_out;
Tswing_out=DeltaT_out*2;
disp(DeltaT_out);
DeltaT_in_wo=0.15*DeltaT_out;
Tmean_in_wo=1.5+Tmean_out;
Tmax_in_wo=Tmean_in_wo+DeltaT_in_wo;
Tmin_in_wo=Tmean_in_wo-DeltaT_in_wo;
Tmin_in_w=Tmin_in_wo-(0.5*(abs(Tmin_in_wo-Tmin_out)));
Tmax_in_w=Tmax_in_wo-(0.25*(abs(Tmax_in_wo-Tmax_out)));
Tmean_in_w=(Tmin_in_w+Tmax_in_w)/2;
DeltaT_in_w=Tmax_in_w-Tmean_in_w;
%part of interest for the question:
f=@(x) Tmean_out+(DeltaT_out*cos(meg*(x)));
j=@(x) Tmean_in_wo+(DeltaT_in_wo*cos(meg*x));

채택된 답변

Torsten
Torsten 2022년 12월 23일
Integrate
g(x) = max(f(x)-j(x),0)

추가 답변 (2개)

Star Strider
Star Strider 2022년 12월 23일
편집: Star Strider 2022년 12월 23일
Perhaps this —
%Part of no interest for the question (given for the code to run)
Tmean_out=30;
Tmax_out=40;
Tadaptive=32;
meg=((2*pi)/86400);
DeltaT_out=Tmax_out-Tmean_out;
Tmin_out=Tmean_out-DeltaT_out;
Tswing_out=DeltaT_out*2;
disp(DeltaT_out);
10
DeltaT_in_wo=0.15*DeltaT_out;
Tmean_in_wo=1.5+Tmean_out;
Tmax_in_wo=Tmean_in_wo+DeltaT_in_wo;
Tmin_in_wo=Tmean_in_wo-DeltaT_in_wo;
Tmin_in_w=Tmin_in_wo-(0.5*(abs(Tmin_in_wo-Tmin_out)));
Tmax_in_w=Tmax_in_wo-(0.25*(abs(Tmax_in_wo-Tmax_out)));
Tmean_in_w=(Tmin_in_w+Tmax_in_w)/2;
DeltaT_in_w=Tmax_in_w-Tmean_in_w;
%part of interest for the question:
f=@(x) Tmean_out+(DeltaT_out*cos(meg*(x)));
j=@(x) Tmean_in_wo+(DeltaT_in_wo*cos(meg*x));
figure
fplot(f, [0 8.64E4], 'DisplayName','f(x)')
hold on
fplot(j, [0 8.64E4], 'DisplayName','j(x)')
hold off
grid
legend('Location','best')
intf = integral(f, 0, 8.64E4) % 'f' Integral
intf = 2592000
intj = integral(j, 0, 8.64E4) % 'j' Integral
intj = 2.7216e+06
fminusj = integral(@(x)f(x)-j(x), 0, 8.64E4) % 'f-j' Integral
fminusj = -129600
fgtj = integral(@(x)(f(x)-j(x)).*(f(x)>j(x)), 0, 8.64E4) % 'f>j' Integral
fgtj = 1.7262e+05
EDIT — (23 Dec 2022 at 13:56)
Changed limits to [0 8.64E4].
.

John D'Errico
John D'Errico 2022년 12월 23일
편집: John D'Errico 2022년 12월 23일
I don't see why it should be a struggle. Start at the beginning. Break the problem down into small pieces. Then solve each of them. Here is an example problem.
You have two curves.
  1. Find the point of intersection.
  2. Integrate. That might be an analytical solution, it might be numerical.
F1 = @cos;
F2 = @(x) expm1(x);
% plot them
fplot(F1,[-10,2])
hold on
fplot(F2,[-10,2])
expm1 is the function exp(x)-1, a useful special function that seems to get overlooked. So I thought I'd use it here as an example. There are four regions that might be of interest. First, find the points of intersection.
F21 = @(x) F1(x) - F2(x);
We can use numerical tools to find the intersection point, so fzero, or even vpasolve. There will be 4 points of interest.
xstart = [-10 -9.42;-9.42 -8;-4 -3;-3 -2;0 1];
xcross = nan(size(xstart,1),1);
fval = xcross;
for i = 1:size(xstart,1)
[xcross(i),fval(i)] = fzero(F21,xstart(i));
end
format long g
[xcross,fval]
ans = 5×2
-9.43740239690328 0 -9.41199210388775 0 -3.40059290398226 1.11022302462516e-16 -2.78912964643395 0 0.60134676772582 -1.11022302462516e-16
And just perform the integration. Symbolic tools or numerical tools both can apply.
areaBetween = zeros(4,1);
subplot(4,1,1)
for i = 1:4
areaBetween(i) = integral(F21,xcross(i),xcross(i+1));
subplot(4,1,i)
fplot(F21,[xcross(i),xcross(i+1)])
end
areaBetween
areaBetween = 4×1
-1.367117078358e-06 6.2470272332719 -0.0179826714572249 2.5383406282387
Of course, I've produced a signed area, depending on which curve is greater in the corresponding region.
  댓글 수: 1
TIMOTHEE
TIMOTHEE 2023년 1월 2일
Thank you John,
The step by step process will surely come in handy with other similar issues.
Always super helpful to have in one's toolbox.
Best regards,
Tim.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by