Salil Chawla
Salil Chawla

Reputation: 1

How do I manually insert variables I have declared symbolically into a zero matrix in MATLAB?

I have been trying to insert symbolic variable into a zero matrix.

syms k1 k2 k3 k4
global_k1 = zeros(4,4);
global_k2 = zeros(4,4);
global_k3 = zeros(4,4);
global_k4 = zeros(4,4);
global_k1(1,1) =  k1;
global_k1(1,2) = -k1;
global_k1(2,1) = -k1;
global_k1(2,2) =  k1;
global_k2(2,2) =  k2;
global_k2(2,3) = -k2;
global_k2(3,2) = -k2;
global_k2(3,3) =  k2;
global_k3(2,2) =  k3;
global_k3(2,3) = -k3;
global_k3(3,2) = -k3;
global_k3(3,3) =  k3;
global_k4(2,2) =  k4;
global_k4(2,4) = -k4;
global_k4(4,2) = -k4;
global_k4(4,4) =  k4;
k_stiff = global_k1+global_k2+global_k3+global_k4;
disp('The stiffness matrix is: ');
disp(k_stiff);

It displays the following error when I run the program:

The following error occurred converting from sym to double:
Unable to convert expression into double array.

Error in HW1_1 (line 9)
global_k1(1,1) =  k1;

How should I add symbolic variables into a zero matrix?

Upvotes: 0

Views: 64

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

Initialise them as symbolic matrices containing zeros if you later intend to replace its elements. i.e

global_k1 = sym(zeros(4,4));

It would be better to create a single 3D matrix instead of dynamic global_k1, global_k2, global_k3 and global_k4 matrices i.e.

global_k = sym(zeros(4,4,4));

Now global_k(:,:,1) will be your global_k1 and so on.

Upvotes: 2

Related Questions