Skip to main content

Day 27: Live Telemetry and MPU 9255 | C++ Classes with Overloaded Operators

Hardware: Live Telemetry and MPU 9255

Real Time Telemetry

Telemetry seemed complicated, but it was a simple matter of hooking up the sensors correctly and getting Putty to communicate with the serial port. This portion was done fairly quickly.

MPU 9255 + BMP280


Software: C++ Classes with Overloaded Operators

Complex Class with Overloaded Operators

  • Complex numbers needed to solve many problems in science and engineering, esp. physics and electrical engineering, therefore useful to define complex class in C++
Recall: complex number has form 
a+bi where i=sqrt(-1) and a and b are real numbers
Figure 1.1
Real and imaginary axis
X is real axis and Y is imaginary axis

Complex Class Definition

  • Class declaration must include private members for real and imaginary components of complex number
  • Public members include a function to input complex number from keyboard and function to print complex number on screen
  • Also need member functions to define arithmetic operators
Example 1.1.1
// These statements define a class for complex numbers.
// This declaration is stored in complex.h.
#include <iostream>
#include <cmath>
using namespace std;
class complex
{
// Declare function prototypes for public members.
public:
complex();
complex(double a, double b);
void print();
void input();
double magn(complex);
double angle(complex);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
// Declare private members.
private:
double real, imag;
};
//
// Class implementation
//
// These statements define implementation of a complex class.
// This function is the default constructor to initialize a
// complex number that is not given a value.
complex::complex()
{
real = 0;
imag = 0;
}
// This function is the constructor to initialize a complex
// number to a specified value.
complex::complex(double a, double b)
{
real = a;
imag = b;
}
// This function prints a complex number.
void complex::print()
{
if (imag > 0)
cout << real << "+" << imag << "i" << endl;
else
if (imag == 0)
cout << real << endl;
else
cout << real << imag << "i" << endl;
}
// This function reads two values for a complex number.
void complex::input()
{
cin >> real >> imag;
}
// This function defines the sum of complex numbers.
complex complex::operator+(complex c)
{
// Definition of complex addition.
complex temp;
temp.real = c.real + real;
temp.imag = c.imag + imag;
return temp;
}
// This function defines the difference of complex numbers.
complex complex::operator-(complex c)
{
// Definition of complex subtraction.
complex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
// This function defines the product of complex numbers.
complex complex::operator*(complex c)
{
// Definition of complex multiplication.
complex temp;
temp.real = (real*c.real - imag*c.imag);
temp.imag = (imag*c.real + real*c.imag);
return temp;
}
// This function defines the quotient of complex numbers.
complex complex::operator/(complex c)
{
// Definition of complex division.
complex temp;
temp.real = (real*c.real + imag*c.imag)/
(pow(c.real,2) + pow(c.imag,2));
temp.imag = (imag*c.real - real*c.imag)/
(pow(c.real,2) + pow(c.imag,2));
return temp;
}

Example 1.1.2
// This program demonstrates the use of
// the complex number class and its operations.
#include <iostream>
#include "complex.h"
using namespace std;
int main(void)
{
// Declare and initialize variables.
complex c1(4,1), c2(-3,-2), c3;
// Print initial values.
cout << "c1:";
c1.print();
cout << "c2:";
c2.print();
cout << "c3:";
c3.print();
// Compute and print new values.
c3 = c1 + c2;
cout << "c1+c2 = ";
c3.print();
c3 = c1 - c2;
cout << "c1-c2 = ";
c3.print();
c3 = c1*c2;
cout << "c1*c2 = ";
c3.print();
c3 = c1/c2;
cout << "c1/c2 = ";
c3.print();
system("pause"); //Add system pause
return 0; //Exit program
}
Figure 2.1
Output from example 1.1

Complex Roots for Quadratic Equations

The song to find the roots of a quadratic equation is permanently burned into my head from 7th grade Algebra 1. "X equals negative b, plus or minus square root, b squared minus four a c, divided by two a." In mathematical terms, x=[-b (+/-) sqrt(b^2-4ac)]/2a. The term under the square root symbol is called the discriminant. If it is greater than or equal to zero, the two roots are real, but if it is less than zero, the two roots are complex values. 

Example 2.1.1
I kept getting an error that stated that 'real' and 'imag' were private within the context. I tried changing real and imag to public members, which seemed to fix the problems, though I don't know if that will have any adverse effects on the program. My computer hasn't exploded, though, so that's a good sign. 

// These statements define a class for complex numbers.
// This declaration is stored in complex.h.
#include <iostream>
#include <cmath>
using namespace std;
class complex
{
// Declare function prototypes for public members.
public:
complex();
complex(double a, double b);
void print();
void input();
double magn(complex);
double angle(complex);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
double real, imag;
};
//
// Class implementation
//
// These statements define implementation of a complex class.
// This function is the default constructor to initialize a
// complex number that is not given a value.
complex::complex()
{
real = 0;
imag = 0;
}
// This function is the constructor to initialize a complex
// number to a specified value.
complex::complex(double a, double b)
{
real = a;
imag = b;
}
// This function prints a complex number.
void complex::print()
{
if (imag > 0)
cout << real << "+" << imag << "i" << endl;
else
if (imag == 0)
cout << real << endl;
else
cout << real << imag << "i" << endl;
}
// This function reads two values for a complex number.
void complex::input()
{
cin >> real >> imag;
}
// This function defines the sum of complex numbers.
complex complex::operator+(complex c)
{
// Definition of complex addition.
complex temp;
temp.real = c.real + real;
temp.imag = c.imag + imag;
return temp;
}
// This function defines the difference of complex numbers.
complex complex::operator-(complex c)
{
// Definition of complex subtraction.
complex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
// This function defines the product of complex numbers.
complex complex::operator*(complex c)
{
// Definition of complex multiplication.
complex temp;
temp.real = (real*c.real - imag*c.imag);
temp.imag = (imag*c.real + real*c.imag);
return temp;
}
// This function defines the quotient of complex numbers.
complex complex::operator/(complex c)
{
// Definition of complex division.
complex temp;
temp.real = (real*c.real + imag*c.imag)/
(pow(c.real,2) + pow(c.imag,2));
temp.imag = (imag*c.real - real*c.imag)/
(pow(c.real,2) + pow(c.imag,2));
return temp;

}

Example 2.1.2
// This program computes and prints the roots of a quadratic
// equation. It uses the user-defined complex class.
#include <iostream>
//#include <cmath>
#include "complex.h"
using namespace std;
int main(void)
{
// Declare and initialize variables.
double a, b, c, term1, disc;
complex root1, root2;
// Read values for a, b, c from the keyboard.
cout << "Enter real values a, b, c:" << endl;
cin >> a >> b >> c;
// Compute roots of quadratic equation.
term1 = -b/(2*a);
disc = b*b - 4*a*c;
if (disc >= 0)
    {
        root1.real = term1 + sqrt(disc)/(2*a);
        root2.real = term1 - sqrt(disc)/(2*a);
    }
    else
    {
        root1.real = term1;
        root1.imag = sqrt(-disc)/(2*a);
        root2.real = term1;
        root2.imag = -sqrt(-disc)/(2*a);
    }
// Print roots.
cout << "Roots:" << endl;
root1.print();
root2.print();
//Add System Pause
system("pause");
// Exit program.
return 0;

}
Figure 3.1
Output from Example 2.1

Figure 3.2
Output from Example 2.1

Homework

Hardware

No homework

Software

1. Test the program with an equation that will give a double root. Does it handle that situation correctly?

Figure 4.1
Double root example

2. Modify the program so that it also prints one of these phrases: "real roots", "double roots", or "complex roots".

Code:
// This program computes and prints the roots of a quadratic
// equation. It uses the user-defined complex class.
#include <iostream>
//#include <cmath>
#include "complex.h"
using namespace std;
int main(void)
{
// Declare and initialize variables.
double a, b, c, term1, disc;
// Read values for a, b, c from the keyboard.
cout << "Enter real values a, b, c:" << endl;
cin >> a >> b >> c;
complex root1, root2;
// Compute roots of quadratic equation.
term1 = -b/(2*a);
disc = b*b - 4*a*c;
if (disc >= 0)
    {
        root1.real= term1 + sqrt(disc)/(2*a);
        root2.real = term1 - sqrt(disc)/(2*a);
        if(disc==0)
        {
        cout << "Double Roots" << endl;
        }
        else
        {
         cout << "Real Roots" << endl;
        }
    }
    else
    {
        root1.real = term1;
        root1.imag = sqrt(-disc)/(2*a);
        root2.real = term1;
        root2.imag = -sqrt(-disc)/(2*a);
        cout << "Complex Roots" << endl;
    }
// Print roots.
cout << "Roots:" << endl;
root1.print();
root2.print();
//Add System Pause
system("pause");
// Exit program.
return 0;
}

3. Modify the program so that it prints two decimal positions for the roots. 



Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 6: Analog Sensors | Functions and Modularity

Functions Functions are sets of statements that typically perform operation or compute value. They can help to make programs more accessible and usable for non-programmers e.g. create function that allows user to type "go forward" and move a robot forward. Modules -Functions can be split up into modules, "divide and conquer" -Each module has specific purpose, can be written and tested separately -Smaller than complete solution, therefore testing is easier -Can be used in new problem solutions without being retested -Reduces overall length of program -Allows for increased collaboration; modules can be worked on in parallel Debugging Longer Programs Use a compiler that gives meaningful information about errors. Adding comments around some sections of code can allow for better focus on other parts of the program. Test complicated functions by themselves. Programmer Defined Functions Execution of program always begins with main function. Additional ...

Day 3: Linear Interpolation, Mathematical Functions, and Arduino Buttons

Figure 1.1 Linear interpolation  assumes that a straight line joins two points f(a) and f(c), and that the value of f(b), where b lies between a and c, lies on this line. Figure 1.2 Cubic spline interpolation  is when the points f(a) and f(c) are joined by a cubic polynomial, and the value of f(b), a function of b, which lies between points a and c, lies on this curve. Formula Assumes a<b<c f(b)=f(a)+((b-a)/(c-a))[f(c)-f(a)] Example 1 The data was given, shown in Figure 1.1 at right. Figure 2.1 Example 2 In the example on the left, we used linear interpolation to find the freezing temperature of certain salinities of water. 1. Problem Statement Use linear interpolation to determine the freezing temperature of water with a certain salinity. 2. Input/Output Description Inputs: first salinity, second salinity, first freezing temperature, second freezing temperature, new salinity. Output: new freezing temperature 3. Hand Example See...