Rewrite an array with for

float y[11] = {1, 0.95, 0.85, 0.76, 0.71, 0.65, 0.52, 0.43, 0.32, 0.22, 0.11};

void setup(){
 Serial.begin(9600);
 delay(200);

 for(int i=0; i<11; i++){
  y[i]=-log10(y[i]/y[0]);
  Serial.println(y[i]);
 }

I'm trying to rewrite the y[i] using for but isnt working, how can I do this properly?

Your code is incomplete.

Full code:

// EXEMPLO - http://en.wikipedia.org/wiki/Simple_linear_regression

float x[11] = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1};
float y[11] = {1, 0.95, 0.85, 0.76, 0.71, 0.65, 0.52, 0.43, 0.32, 0.22, 0.11};
float lrCoef[2] = {0, 0};

// INICIANDO AS VARIÁVEIS
float sum_x = 0;
float sum_y = 0;
float sum_xy = 0;
float sum_xx = 0;

void setup(){
 Serial.begin(9600);
 delay(200);

 for(int i=0; i<11; i++){
  y[i]=-log10(y[i]/y[0]);
  Serial.println(y[i]);
 }
 
 // CHAMA O ALGORITMO DE REGRESSÃO LINEAR
 simpLinReg(x, y, lrCoef, 11);
 Serial.println("Equação Linear:");
 Serial.println("");
 Serial.print("Abs = ");
 Serial.print(lrCoef[0], 11);

 if (lrCoef[1] > 0){
  Serial.print("*Conc + ");
 }
 else{
  Serial.print("*Conc ");
 }
 
 Serial.println(lrCoef[1], 4);
}

void loop(){
}

void simpLinReg(float* x, float* y, float* lrCoef, int n){
 // CALCULOS PARA REGRESSÃO LINEAR
 for (int i=0; i<n; i++){
  sum_x += x[i];
  sum_y += y[i];
  sum_xy = sum_xy+x[i]*y[i];
  sum_xx = sum_xx+x[i]*x[i];
 }
 lrCoef[0]=(n*sum_xy-sum_x*sum_y)/(n*sum_xx-sum_x*sum_x);
 lrCoef[1]=(sum_y/n)-((lrCoef[0]*sum_x)/n);
}

Tells us nothing, except that you are unhappy.

What did you expect to happen and what happens instead?

The problem is with this part of the code

 for(int i=0; i<11; i++){
  y[i]=-log10(y[i]/y[0]);
  Serial.println(y[i]);
 }

When I run the code with this for this happens


But if I remove the for all works well. What I'm trying to do is to convert the y[i] for y[i]=-log10(y[i]/y[0]). But I think the problem is with the y[0] that I want to fix in this line code

The first entry you replace is y[0]. Perhaps you should save that value first:

  float y0 = y[0];
  for(int i=0; I < 11; i++)
  {
    y[i] = -log10(y[i]] / y0);
    Serial.println(y[i]);
  }
1 Like

What is log10(1), and what happens when you divide any nnumber by log10(1)?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.