MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# How can I have a symbolic equation as an array element and then compute the determinant?

Asked by Hello on 19 Sep 2013

Hello everyone,

I am trying to populate matrix elements so that I can take its determinant.

```n=2;
r = [];
for (i=1:n)
t = sym(['r' int2str(i)]);
r = [r; t];
end
v=cell(n,1);
for i=1:n
v{i}=zeros(n,n);
end
v{1}(1,1)=r(1)-1; -------->ERROR!
v{1}(2,1)=r(2)-1;
v{2}(1,1)=r(1)-1;
v{2}(2,1)=r(2)-1;
```
```for i=1:n
for j=1:n
%v{i}(j,1)=v(j)-1;
for k=2:n
v{i}(j,k)=p{i}(k,j)-p{i}(1,j);
end
end
end
```
```%above code should replace the below to allow for any values of n. The below code works fine.
v{1}=[v(1)-1,p{1}(2,1)-p{1}(1,1);v(2)-1,p{1}(2,2)-p{1}(1,2)];
v{2}=[v(1)-1,p{2}(2,1)-p{2}(1,1);v(2)-1,p{2}(2,2)-p{2}(1,2)];
```
```%compute determinant
v_det=cell(n,1);
for i=1:n
v_det{i}=det(v{i});
end
```
```Error given is:
??? The following error occurred converting from sym to double:
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
```

If the input expression contains a symbolic variable, use the VPA function instead.

help please!! I tried using the vpa function as suggested with no success.

## Products

No products are associated with this question.

Answer by Walter Roberson on 19 Sep 2013

You initialize v{1} as a numeric array, zeros(). Then you try to store r(1)-1 into it, but r(1) is a symbolic expression. Because it knows that the target v{1}(1,1) is a numeric data type, it tries to convert r(1)-1 to a numeric data type and fails.

Solution: do not initialize v{1} as numeric.

```v{1} = [r(1)-1, 0; r(2)-1, 0];
```

You might have to go as far as

```Z = sym(0);
```
```v{1} = [r(1)-1, Z; r(2)-1, Z];
```

Hello on 19 Sep 2013

Hi Walter,

I understand this but how would I extend this from a 2 x 2 matrix to a n x n matrix? This is what I am trying to do with the nested forloop in the code above.

```v{i}=sym(zeros(n,n));