- #1
Const@ntine
- 285
- 18
Homework Statement
Okay, this one is a bit big, and I'm attempting a translation, so I'm going to post a TL;DR version at the bottom just to be sure. Anyway, here goes:
While doing an experiment, we write down the values of a physical/natural size y, for various values of a physical/natural size x. Let's say n is the amount of (x,y) pairs which are the result of how many times we did this experiment. We know that these two sizes are connected through the following formula: y = f(x) = a*x + b
Sadly, it's easy to have lots of faults with such a way of writting our measurments. So, we need to find a linear relation between x & y (so essentially the values of a & b), which is as close as it can to (xi,yi).
So we need to find the formula f(x) = a*x + b that minimizes the average square fault E = √(1/n ** ∑i=1n(f(xi) - yi)2)
This is equilavent to this system:
n*b + (∑i=1nxi)*a = ∑i=1nyi
(∑i=1nxi)*b + (∑i=1nxi2)*a = ∑i=1nxi*yi
You have to write a program in C++ & Fortran that get close to the linear relation of x & y, by using the pairs of (xi,yi). The amount of measurements (n) and the pairs themselves will be entered through the keyboard, by the user.
Use the Program from Exercise 2 to solve the system.
TLD;DR: Create a program that takes n amount of (x,y) pairs, entered through the keyboard, and solves the above equation system, with a & b being the unknown quantities.
PS: That floating n over at the ∑ is just the "limit", it's not Σ to the nth power.
Homework Equations
The Attempt at a Solution
Okay, as far as this program goes, I have this:
C:
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
int i, j;
int n;
double coor[50][2];
cout << "How many measurments did you take ? ";
cin >> n;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
cout << "Enter your coordinates for coor(" << i << ") (" << j << "): ";
cin >> coor[i][j];
}
return 0;
}
Yeah, it's nothing thus far, but I can't "solve" the input problem yet, so I'm stumped.
The program that solves the system, from a previous excercise:
C:
#include <iostream>
using namespace std;
int main()
{
double a11, a12, a21, a22;
double b1, b2;
double x, y, D, Dx, Dy;
cout << "Give the values of a11, a12, a21 & a22: " << endl;
cin >> a11 >> a12 >> a21 >> a22;
cout << endl;
cout << "Give the values of b1 & b2: " << endl;
cin >> b1 >> b2;
cout << endl;
D = (a11*a22 - a12*a21);
Dx = (b1*a22 - b2*a12);
Dy = (b2*a11 - b1*a21);
if (D != 0)
{
x = Dx/D;
y = Dy/D;
cout << "For those values, x & y are: " << x << " & " << y << endl;
}
if (D = 0)
{
if (Dx = 0)
{
if (Dy = 0)
{
cout << "Then the equations/system have infinite solutions." << endl;
}
}
}
if (D = 0)
{
if (Dx != 0)
{
if (Dy != 0)
{
cout << "The equations/system are unsolvable." << endl;
}
}
}
return 0;
}
It's a tad different, but the basic idea is the same.
Anyway, I'd greatly appreciate any kind of help!