# Finding Max/Min of interpolated function and average value above/below the x axis

I have the below code used to create an interpolation function that will plot the difference between two interpolated functions (to compare between different periods of time).

However, I would like to find the max and minimum values within specific ranges (for this data between 4 and 23 on the x-axis).

I would also like some sort of average negative and positive value so I can find the average positive and negative change between the two data sets. I have been unsuccessful in finding examples of what I need online (or it is possible I haven't been able to understand them fully in how I can implement it with my own data)

The max min and averages are for  DamOne1u2119 at the end of the code

DamOne1u2021={{-3., 52.584}, {-2., 52.584}, {-1., 52.584}, {0., 52.584}, {0.690359,
52.129}, {1.35808, 51.935}, {2.15814, 51.785}, {2.83443,
51.765}, {3.39709, 51.703}, {3.81911, 51.45}, {4.31593,
51.458}, {4.91879, 50.931}, {5.48303, 50.638}, {6.05773,
50.506}, {7.40077, 49.892}, {7.66482, 49.873}, {8.36334,
49.792}, {8.78798, 49.792}, {9.38703, 49.887}, {9.79445,
49.912}, {10.3076, 49.967}, {10.8794, 49.971}, {11.2876,
50.086}, {11.801, 50.108}, {12.5217, 50.166}, {13.0028,
50.204}, {13.4898, 50.211}, {14.0177, 50.281}, {14.6184,
50.303}, {15.0467, 50.306}, {15.7232, 50.349}, {16.0804,
50.389}, {16.7198, 50.437}, {17.3869, 50.424}, {17.7724,
50.377}, {18.1859, 50.381}, {18.8983, 50.345}, {19.193,
50.289}, {21.36, 50.87}, {21.7722, 51.061}, {21.8349,
51.078}, {22.384, 51.351}, {22.8442, 51.574}, {23.3637,
51.866}, {24., 51.866}, {25., 51.866}, {26., 51.866}};
DamOne1u2019={{26., 52.143}, {25., 52.143}, {24., 52.143}, {23.3859,
52.143}, {23.1698, 51.942}, {21.4912, 51.068}, {21.0132,
50.936}, {20.4527, 50.861}, {19.0339, 50.846}, {18.3098,
50.8}, {17.8306, 50.793}, {17.4658, 50.762}, {17.0203,
50.722}, {16.4767, 50.717}, {15.98, 50.732}, {15.4513,
50.756}, {14.9287, 50.731}, {14.5165, 50.699}, {13.9742,
50.742}, {13.4617, 50.732}, {12.9475, 50.715}, {12.4889,
50.709}, {12.0645, 50.681}, {11.5034, 50.651}, {11.0292,
50.631}, {10.4117, 50.637}, {9.95895, 50.625}, {9.49877,
50.552}, {8.9786, 50.536}, {8.50658, 50.518}, {8.05728,
50.454}, {7.51495, 50.422}, {7.05679, 50.455}, {6.55668,
50.519}, {6.01821, 50.547}, {5.60583, 50.617}, {5.44458,
50.632}, {5.12869, 51.166}, {4.74713, 50.97}, {3.94958,
51.495}, {2.66635, 51.909}, {2.20125, 51.93}, {1.75397,
51.898}, {1.24684, 52.044}, {0.932995, 52.128}, {0.372399,
52.39}, {-0.109343, 52.729}, {-0.565013, 52.883}, {-1.04139,
53.041}, {-2., 53.041}, {-3., 53.041}, {-4., 53.041}};
SpDamOne1u2021 =
Interpolation[DamOne1u2021, Method -> "Spline",
InterpolationOrder -> 3];
SpDamOne1u2019 =
Interpolation[DamOne1u2019, Method -> "Spline",
InterpolationOrder -> 3];
DamOne1u2119 = SpDamOne1u2021[x] - SpDamOne1u2019[x]

• Use FindMinimum[{SpDamOne1u2021[x] - SpDamOne1u2019[x], 4 <= x <= 23}, {x, 9}] with out {-0.746626, {x -> 8.68487}}, and FindMaximum[{SpDamOne1u2021[x] - SpDamOne1u2019[x], 4 <= x <= 23}, {x, 4.5}] with out {0.569714, {x -> 4.42633}} Apr 15, 2023 at 4:59
• @AlexTrounev May I ask what the {x, 9} and {x, 4.5} do? Apr 15, 2023 at 20:10
• They are local definitions where to search. But if you don't know and prefer Automatic choice, then it could be better to compute global min/max on the mesh defined on 4<=x<=23 with using MinimalBy, MaximalBy. Apr 16, 2023 at 3:24
• First we prepare xgrid = Range[4, 23, 10^-3]; lst = Table[{x, SpDamOne1u2021[x] - SpDamOne1u2019[x]}, {x, xgrid}];. Then we use MinimalBy[lst, Last] // N with out {{8.685, -0.746626}} , and MaximalBy[lst, Last] // N with out {{4.426, 0.569713}}. Apr 17, 2023 at 1:40
• This is a typical error of your model. So you can reduce or increase this parameter regarding to accuracy of your model. Apr 17, 2023 at 2:25

\$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global*"]

DamOne1u2021 = {{-3., 52.584}, {-2., 52.584}, {-1., 52.584}, {0.,
52.584}, {0.690359, 52.129}, {1.35808, 51.935}, {2.15814,
51.785}, {2.83443, 51.765}, {3.39709, 51.703}, {3.81911, 51.45}, {4.31593,
51.458}, {4.91879, 50.931}, {5.48303, 50.638}, {6.05773,
50.506}, {7.40077, 49.892}, {7.66482, 49.873}, {8.36334,
49.792}, {8.78798, 49.792}, {9.38703, 49.887}, {9.79445,
49.912}, {10.3076, 49.967}, {10.8794, 49.971}, {11.2876, 50.086}, {11.801,
50.108}, {12.5217, 50.166}, {13.0028, 50.204}, {13.4898,
50.211}, {14.0177, 50.281}, {14.6184, 50.303}, {15.0467,
50.306}, {15.7232, 50.349}, {16.0804, 50.389}, {16.7198,
50.437}, {17.3869, 50.424}, {17.7724, 50.377}, {18.1859,
50.381}, {18.8983, 50.345}, {19.193, 50.289}, {21.36, 50.87}, {21.7722,
51.061}, {21.8349, 51.078}, {22.384, 51.351}, {22.8442, 51.574}, {23.3637,
51.866}, {24., 51.866}, {25., 51.866}, {26., 51.866}};
DamOne1u2019 = {{26., 52.143}, {25., 52.143}, {24., 52.143}, {23.3859,
52.143}, {23.1698, 51.942}, {21.4912, 51.068}, {21.0132,
50.936}, {20.4527, 50.861}, {19.0339, 50.846}, {18.3098, 50.8}, {17.8306,
50.793}, {17.4658, 50.762}, {17.0203, 50.722}, {16.4767, 50.717}, {15.98,
50.732}, {15.4513, 50.756}, {14.9287, 50.731}, {14.5165,
50.699}, {13.9742, 50.742}, {13.4617, 50.732}, {12.9475,
50.715}, {12.4889, 50.709}, {12.0645, 50.681}, {11.5034,
50.651}, {11.0292, 50.631}, {10.4117, 50.637}, {9.95895,
50.625}, {9.49877, 50.552}, {8.9786, 50.536}, {8.50658, 50.518}, {8.05728,
50.454}, {7.51495, 50.422}, {7.05679, 50.455}, {6.55668,
50.519}, {6.01821, 50.547}, {5.60583, 50.617}, {5.44458,
50.632}, {5.12869, 51.166}, {4.74713, 50.97}, {3.94958, 51.495}, {2.66635,
51.909}, {2.20125, 51.93}, {1.75397, 51.898}, {1.24684,
52.044}, {0.932995, 52.128}, {0.372399, 52.39}, {-0.109343,
52.729}, {-0.565013, 52.883}, {-1.04139, 53.041}, {-2., 53.041}, {-3.,
53.041}, {-4., 53.041}};
SpDamOne1u2021 =
Interpolation[DamOne1u2021, Method -> "Spline", InterpolationOrder -> 3];
SpDamOne1u2019 =
Interpolation[DamOne1u2019, Method -> "Spline", InterpolationOrder -> 3];
DamOne1u2119[x_?NumericQ] := SpDamOne1u2021[x] - SpDamOne1u2019[x]


Plotting,

Legended[
ResourceFunction["CombinePlots"][
Plot[{SpDamOne1u2021[x], SpDamOne1u2019[x]}, {x, -3, 26},
Frame -> True],
Plot[DamOne1u2119[x], {x, -4, 26},
Frame -> True,
FrameStyle -> Red,
PlotStyle -> Red],
"AxesSides" -> "TwoY"],
Placed[
LineLegend[{ColorData[97][1], ColorData[97][2], Red},
{"SpDamOne1u2021", "SpDamOne1u2019", "DamOne1u2119"}],
{0.6, 0.75}]]


Min, Max, and average values in the interval {4, 23}

{min2019, max2019} = #[{SpDamOne1u2019[x], 4 <= x <= 23}, x] & /@
{MinValue, MaxValue}

(* {50.4218, 51.7481} *)

avg2019 = NIntegrate[SpDamOne1u2019[x], {x, 4, 23}]/(23 - 4)

(* 50.7602 *)

{min2021, max2021} = #[{SpDamOne1u2021[x], 4 <= x <= 23}, x] & /@
{MinValue, MaxValue}

(* {49.7823, 51.6661} *)

avg2021 = NIntegrate[SpDamOne1u2021[x], {x, 4, 23}]/(23 - 4)

(* 50.3677 *)

{mindif, maxdif} = #[{DamOne1u2119[x], 4 <= x <= 23}, x] & /@
{MinValue, MaxValue}

{MinValue[{DamOne1u2119[x], 4 <= x <= 23}, x],
MaxValue[{DamOne1u2119[x], 4 <= x <= 23}, x]}

avgdif = NIntegrate[DamOne1u2119[x], {x, 4, 23}]/(23 - 4)

(* -0.392483 *)

avgdif - (avg2021 - avg2019)

(* -1.72071*10^-6 *)


The average positive difference in the interval {4, 23} is

NIntegrate[Boole[DamOne1u2119[x] > 0]*DamOne1u2119[x], {x, 4, 23},
MinRecursion -> 10, MaxRecursion -> 50,
Method -> {"MonteCarloRule", "Points" -> 200}]/
NIntegrate[Boole[DamOne1u2119[x] > 0], {x, 4, 23},
MinRecursion -> 10, MaxRecursion -> 50,
Method -> {"MonteCarloRule", "Points" -> 200}]

(* 0.204013 *)


The average negative difference in the interval {4, 23} is

NIntegrate[Boole[DamOne1u2119[x] < 0]*DamOne1u2119[x], {x, 4, 23},
MinRecursion -> 10, MaxRecursion -> 50,
Method -> {"MonteCarloRule", "Points" -> 200}]/
NIntegrate[Boole[DamOne1u2119[x] < 0], {x, 4, 23},
MinRecursion -> 10, MaxRecursion -> 50,
Method -> {"MonteCarloRule", "Points" -> 200}]

(* -0.451114 *)

• When I try to use the average negative and average positive difference I just get back error messages. I am on Version 13 on a windows 11 64-bit computer. I have no plugins or add ons Apr 15, 2023 at 19:58
• It might be easier and actually better (not that I think about it) to just do the average y value in the range, rather than average positive and average negative y values. I tried using this section of the code avgdif = NIntegrate[DamOne1u2119[x], {x, 4, 23}]/(23 - 4) but it is not giving me the output 1/19 NIntegrate[DamOne1u2119[x], {x, 4, 23}] Apr 15, 2023 at 21:11
• "I just get back error messages" doesn't help us troubleshoot your problem. You need to tell us what the error messages are. If you are seeing 1/19 NIntegrate[DamOne1u2119[x], {x, 4, 23}], did you clear the old definition and use the revised definition? Use DamOne1u2119[x_?NumericQ] := SpDamOne1u2021[x] - SpDamOne1u2019[x]` Apr 15, 2023 at 23:42
• Sorry for the vagueness. I cleared the older definition for the function and it is working as expected now Apr 16, 2023 at 20:14