Search code examples
rinversionrgui

(R) Error in solve() 'a' must be a numeric matrix


Here's the problem: I want to calculate the inverse of a 3x3 matrix. I tried to use solve(J), but it gives an error message:

Error in solve.default(J) : 'a' must be a numeric matrix.

Here's the matrix J and the code:

T1<-20
T2<-50
T3<-50
T4<-50
T5<-350

        df1dx1<- Deriv(-(14.6+0.0635*(T2+T1))*T2-(14.6+0.0635*(T2+T1))*T1+(14.6+0.0635*(T3+T2))*T3-(14.6+0.0635*(T3+T2))*T2, "T2")
        df1dx2<- Deriv(-(14.6+0.0635*(T2+T1))*T2-(14.6+0.0635*(T2+T1))*T1+(14.6+0.0635*(T3+T2))*T3-(14.6+0.0635*(T3+T2))*T2, "T3")
        df1dx3<- Deriv(-(14.6+0.0635*(T2+T1))*T2-(14.6+0.0635*(T2+T1))*T1+(14.6+0.0635*(T3+T2))*T3-(14.6+0.0635*(T3+T2))*T2, "T4")
        df2dx1<- Deriv(-(14.6+0.0635*(T3+T2))*T3-(14.6+0.0635*(T3+T2))*T2+(14.6+0.0635*(T4+T3))*T4-(14.6+0.0635*(T4+T3))*T3,"T2")
        df2dx2<- Deriv(-(14.6+0.0635*(T3+T2))*T3-(14.6+0.0635*(T3+T2))*T2+(14.6+0.0635*(T4+T3))*T4-(14.6+0.0635*(T4+T3))*T3,"T3")
        df2dx3<- Deriv(-(14.6+0.0635*(T3+T2))*T3-(14.6+0.0635*(T3+T2))*T2+(14.6+0.0635*(T4+T3))*T4-(14.6+0.0635*(T4+T3))*T3,"T4")
        df3dx1<- Deriv(-(14.6+0.0635*(T4+T3))*T4-(14.6+0.0635*(T4+T3))*T3+(14.6+0.0635*(T5+T3))*T5-(14.6+0.0635*(T5+T4))*T4,"T2")
        df3dx2<- Deriv(-(14.6+0.0635*(T4+T3))*T4-(14.6+0.0635*(T4+T3))*T3+(14.6+0.0635*(T5+T3))*T5-(14.6+0.0635*(T5+T4))*T4,"T3")
        df3dx3<- Deriv(-(14.6+0.0635*(T4+T3))*T4-(14.6+0.0635*(T4+T3))*T3+(14.6+0.0635*(T5+T3))*T5-(14.6+0.0635*(T5+T4))*T4,"T4")

    J<-matrix(c(df1dx1,df1dx2,df1dx3,df2dx1,df2dx2,df2dx3,df3dx1,df3dx2,df3dx3),nrow=3,ncol=3,byrow=TRUE)

invJ<- solve(J)  ##this gives the problem

What's the problem??? Thank you very much, guys. Obs: I can't substitute T2,T3 and T4 by 50 because I need to recalculte this multiple times changing the T2, T3 and T4 values.


Solution

  • Just as the error message says. solve expects a numeric matrix, but you've given in a matrix of expressions. You could do

    eval(Deriv(-(14.6+0.0635*(T2+T1))*T2-(14.6+0.0635*(T2+T1))*T1+(14.6+0.0635*(T3+T2))*T3-(14.6+0.0635*(T3+T2))*T2, "T2"))
    

    etc. to calculate the numeric values