Fast way to run through a series of number compares

I’m doing a fan control project. I want to mimic something like the afterburner fan curve control. So far I’ve done the following:

double x [6] = {0,25,35,60,80,100};
double y [6] = {0,0,25,50,100,100};

  double yc[5];
  double xc[5];
  double k[5];


void setup() {
Serial.begin(115200);
Serial.println("Started serial and entered the setup");
  
  for (int i=0 ; i<=4 ; i++ ){
    Serial.println();
    Serial.print("Calculating k for ");
    Serial.print(i);
    Serial.print(" line");

    yc[i]=(y[i+1]-y[i]);
    Serial.println();
    Serial.print("yc=");
    Serial.print(yc[i]);
    xc[i]=(x[i+1]-x[i]);
    Serial.println();
    Serial.print("xc=");
    Serial.print(xc[i]);

    if(yc[i]==0){
      Serial.println();
      Serial.print("Y is 0 for this point, so k is also");
      k[i]=0;  
    }

    if(xc[i]==0){
      Serial.println();
      Serial.print("We have a problem huston, I'm trying to devide by 0");
    }
        
    k[i]=((yc[i])/(xc[i]));
    Serial.println();
    Serial.print("Calculated:");
    Serial.print(k[i]);
  }
  Serial.println();
  
  Serial.print("Finished setup, entering loop");

}

void loop() {
    if (Serial.available() > 0) {
    int inputValue = Serial.read();
    Serial.println();
    Serial.println("I got this:")
    Serial.print(inputValue);
    }


  
}

Quite basic so far. I need to now check if the inputted X is somewhere between X points to apply the correct upper bond and k and calculate the y. What would be the best approach to do this fast as to not strain the processor unnecessary with numerous if-s?

If anyone also has any experience with this and would like to pitch in with a better way to do this I’m all ears.

Please clarify your question and provide us with an example.

compare you input to each value in your x array until you find the array value that is greater. You then know you input is between that and the previous array value

Binary chop?

soki3008:
Quite basic so far. I need to now check if the inputted X is somewhere between X points ...

If I understand it correctly, you want to find the nearest value in the array that corresponds to an input value.

If so I think your first test should be with the value in the middle of the array (middle by position) then you only need to check one half of the array for a closer match. If you can check the middle of the half you can shorten the search even further.

...R

k[i]=((yc[i])/(xc[i]));

If you really don’t want to “strain” the processor, then precalculate your k.

Sounds like you want piecewise linear interpolation. You can get that with the MultiMap function.

https://playground.arduino.cc/Main/MultiMap/

LightuC:

k[i]=((yc[i])/(xc[i]));

If you really don't want to "strain" the processor, then precalculate your k.

I'm already doing that. Take a look at the code. I'm doing that the setup process.

gcjr:
compare you input to each value in your x array until you find the array value that is greater. You then know you input is between that and the previous array value

TheMemberFormerlyKnownAsAWOL:
Binary chop?

Thanks guys I'll do this like so. Seems I can really cut down on calculation time. I'm planning of doing this on ESP32 on dual cores so this should be fast enough.