C++ Although we know the structure of the relationship, we do not know the param
ID: 3895746 • Letter: C
Question
C++
Although we know the structure of the relationship, we do not know the parameters a, b and c. To find these parameters, we do 11 measurements and for different values of X, record the value of Y. The following is the result of the 11 measurements we have made (there is a little noise, so the measurement is not 100% accurate): Y measured 9.9 6.1 3.1 1.35 0.6 0.8 2.14.17.511.516.9 We want to find the best possible values of a, b, c using the measurement above. The best values will result in the least error sum For example: Lets assume a -1, b1 and c 0.5. Then based on the formula if we put X 3, then Y will be: Y 1(3)42(-1)*30.5 6.5 As you see for a 1, b--1 and c 0.5, the formula for X-3 gives Y-6.5 whereas our measurements has recorded 7.5. So we have abs(7.5-6.5) 1 unit of error. We will have more or less error with other measurements as well The best values of a, b, and c are the ones that result in the least sum of errors Your task: 1) Write a program that uses the vector class to define two vectors X and Y. Initialize the vectors with the measurement result (Row 1 is X and Row 2 is Y) 2) Write a function cle erorSumFunc() that gets vectors X and Y and three double parameters a, b, c. Then using the given a, b and c values finds the error for all the (X, Y) pairs using the quadratic equation discussed above and returns the sum of the error for all the 11 measurement. 3) The only thing we know about a, b and c is that they are between 0 and 1. In your main function write three nested for loops for a, b, and starting at 0, with step size 0.05 and ending at 1. Then for each combination of a, b and c values call the errorSumFunc and then find the set of a, b and c that result in the least error sum. Report the minimum error you have foundExplanation / Answer
solution:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
double errorSumFun(const vector<double> &X, const vector<double> &Y,
double a, double b, double c);
double getY(double x, double a, double b, double c);
int main()
{
vector<double> X = {-5, -4, -3, -2, -1, 0, 1, 2, 3 ,4 ,5};
vector<double> Y = {9.9, 6.1, 3.1, 1.35, 0.6, 0.8, 2.1, 4.1, 7.5, 11.5,
16.9};
double sumErr = -1; //error value
double err;
double a = 0, b = 0 , c = 0;
for(double i = 0; i <= 1; i += 0.05)
{
for(double j = 0; j <= 1; j += 0.05)
{
for(double k = 0; k <= 1; k += 0.05)
{
err = errorSumFun(X, Y, i, j, k);
if(sumErr == -1 || err < sumErr)
{
a = i;
b = j;
c = k;
sumErr = err;
}
}}}
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "sum of errors is " << sumErr << endl;
}
double errorSumFun(const vector<double> &X, const vector<double> &Y,
double a, double b, double c)
{
double err = 0;
double y;
for(int i = 0 ; i < X.size(); i++)
{
y = getY(X[i], a, b, c); //calculate Y
err = err + abs(Y[i] - y); //calculate the sum of error
}
return err;
}
double getY(double x, double a, double b, double c)
{
return a * x * x + b * x + c;
}
output:
a = 0.5
b = 0.7
c = 0.8
sum of errors is 0.85
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.