Question
C++ (Curve Fitting) In an engineering problem, we know that there is the following quadratic relationship between X and Y. Y = a*X^2 + b*X
C++
(Curve Fitting) In an engineering problem, we know that there is the following quadratic relationship between X and Y.
Y = a*X^2 + b*X + 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):
X | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 |
Y measured | 9.9 | 6.1 | 3.1 | 1.35 | 0.6 | 0.8 | 2.1 | 4.1 | 7.5 | 11.5 | 16.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, b = -1 and c = 0.5. Then based on the formula if we put X = 3, then Y will be:
Y = 1*(3)^2 + (-1)*3 + 0.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 called errorSumFunc() 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 found.
Interesting to know: After you are done with this simple code, it will be interesting to know that you have solved a problem that is the foundation of many data science algorithms. You iPhones Siri or OK Google in Android (voice recognition) and many Artificial Intelligence applications are extensions of this problem with two differences.
Dif 1) The function that is used in the real applications is not as simple as a quadratic equation we used here.
Dif 2) We do not (cannot) use a for loop to find the optimal parameters. It takes a lot of time when we have so many (like 80) parameters (It will require 80 nested loops!).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started