0
$\begingroup$

I'm trying to find inverse of a matrix using c++ library armadillo or eigen. But first row of the matrix is wrong, please note that other elements are correct. I'm unable to understand what mistake I committed, please help me.

My code is:

#include <iostream>
#include <armadillo>
#include <cmath>
#include <Eigen/Dense>
using namespace std;
using namespace arma;
using namespace Eigen;

int main()
{
    vec a;
int h=5;

    MatrixXd b(5,5);
    MatrixXd im(5,5);

     a <<-2 << -1 << 0 << 1 << 2;

   for(int i1=0;i1<h;i1++)
   {

            b(i1,0)=1;
            b(i1,1)=a(i1);
            b(i1,2)=a(i1)*a(i1)+1/12;
            b(i1,3)=pow(a(i1),3)+a(i1)/4;
            b(i1,4)=1/80+pow(a(i1),2)/2+pow(a(i1),4);


    }

    im=b.inverse();

    cout<<im<<endl;
    return 0;
}

Output of current code is enter image description here

Correct result is:

   0.00469  -0.06042   1.11146  -0.06042   0.00469
   0.10417  -0.70833   0.00000   0.70833  -0.10417
  -0.06250   0.75000  -1.37500   0.75000  -0.06250
  -0.08333   0.16667   0.00000  -0.16667   0.08333
   0.04167  -0.16667   0.25000  -0.16667   0.04167
$\endgroup$
5
$\begingroup$

Please make following changes in your code and check the answer

        b(i1,0)=1;
        b(i1,1)=a(i1);
        b(i1,2)=a(i1)*a(i1)+1.0/12.0;
        b(i1,3)=pow(a(i1),3)+a(i1)/4;
        b(i1,4)=1.0/80.0+pow(a(i1),2)/2+pow(a(i1),4);
| cite | improve this answer | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.