Using Arduino to calculate Quadratic function

This is a simple and quick code I made for teach variables with arduino programming,
where we enter the variable numbers a, b, c in

and we get the answer using the Serial Monitor..

I won’t get in details about this code, it’s quite straightforward. But if you have any question, just ask! 🙂

[cc lang=”c”]

/* Bhaskara Calculator
Daniel Spillere Andrade – www.danielandrade.net
Simple code showing how to use arduino to calculate Quadratic function.

X => [-b +- sqrt( b^2 – 4ac)] / 2a

Change the a, b and c to get the result via Serial Monitor
*/
int a = 1;
int b = -10;
int c = 25;
float delta, result1, result2;

#include “math.h” // Math!

void setup() {

Serial.begin(9600);

// Calculte delta
// b^2 – 4 * a * c

delta = pow(b,2) – 4 * a * c;

// Analize delta

if(delta<0) {
Serial.print(“DELTA=”);Serial.println(delta);
Serial.println(“Delta < 0, can't calculate!"); // Ends the program } else if (delta==0) { Serial.println("Delta = 0, we can calculate!!!"); result1 = result2 = (-b) / (2 * a); Serial.print("X1=X2="); Serial.println(result1); } else if (delta>0) {

// X1 != X2

delta = sqrt(delta);

Serial.print(“DELTA=”);Serial.println(delta);
Serial.println(“Delta > 0, we can calculate”);

result1 = (-b + delta) / (2 * a);
result2 = (-b – delta) / (2 * a);

Serial.print(“X1=”); Serial.println(result1);
Serial.print(“X2=”); Serial.println(result2);
}

//Done

}

void loop() { }

[/cc]

🙂

Comments

  1. hello sir…
    how to write this formula(Ï€/3 h(R^2+Rr+r^2)) for coding arduino…

    π=3.1248
    h=14 cm
    R=91 cm
    r= 74 cm

    thanks

  2. Is it possible to enter the following equation in to an Arduino sketch.
    I am using an RTD temp sensor. the output is non linear.
    i generated the equation below with Microsoft Excel.
    y = 28.089×5 – 158.77×4 + 410.33×3 – 426.65×2 + 532.31x – 320.03
    R² = 1

  3. correction.

    y = 28.089x^5 – 158.77x^4 + 410.33x^3 – 426.65x^2 + 532.31x^ – 320.03R² = 1
    Thanks’

    Steven,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.