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 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti

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));
0 Commenti
Risposta accettata
Più risposte (2)
Star Strider
il 23 Dic 2022
Modificato: Star Strider
il 23 Dic 2022
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);
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
intj = integral(j, 0, 8.64E4) % 'j' Integral
fminusj = integral(@(x)f(x)-j(x), 0, 8.64E4) % 'f-j' Integral
fgtj = integral(@(x)(f(x)-j(x)).*(f(x)>j(x)), 0, 8.64E4) % 'f>j' Integral
EDIT — (23 Dec 2022 at 13:56)
Changed limits to [0 8.64E4].
.
John D'Errico
il 23 Dic 2022
Modificato: John D'Errico
il 23 Dic 2022
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.
- Find the point of intersection.
- 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]
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
Of course, I've produced a signed area, depending on which curve is greater in the corresponding region.
Vedere anche
Categorie
Scopri di più su Calculus in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


