I want to solve a linear equation system using the Lapack package in C++. I plan to implement it like this using the routines from here, namely dgesv.
This is my code:
unsigned short int n=profentries.size();
double **A, *b, *x;
A= new double*[n];
b=new double[n];
x=new double[2];
// Generate the matrices for my equation
for(int i = 0; i < n; ++i)
{
A[i] = new double[2];
}
for(int i=0; i<n; i++)
{
A[i][0]=5;
A[i][1]=1;
b[i]=3;
}
std::cout<< "printing matrix A:"<<std::endl;
for(int i=0; i<n; i++)
{
std::cout<< A[i][0]<<" " <<A[i][1]<<std::endl;
}
// Call the LAPACK solver
x = new double[n];//probably not necessary
dgesv(A, b, 2, x); //wrong result for x!
std::cout<< "printing vector x:"<<std::endl;
/*prints
3
3
but that's wrong, the solution is (0.6, 0)!
*/
for(int i=0; i<2; i++)
{
std::cout<< x[i]<<std::endl;
}
I have the following problem:
How can it be that dgesv calculates a vector x with the elements {3, 3}? The solution should be {0.6, 0} (checked it with matlab).
Greetings
Edit: dgesv might work for square matrices. My solution below shows how to solve overdetermined systems with dgels.