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

I have been trying to insert symbolic variable into a zero matrix.
clear all
clc
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 resolve this error?

 Accepted Answer

>> syms k1 k2 k3 k4
global_k1 = sym(zeros(4,4));
global_k2 = sym(zeros(4,4));
global_k3 = sym(zeros(4,4));
global_k4 = sym(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;
global_k1
global_k2
global_k3
global_k4
k_stiff = global_k1+global_k2+global_k3+global_k4;
disp('The stiffness matrix is: ')
disp(k_stiff);
global_k1 =
[ k1, -k1, 0, 0]
[ -k1, k1, 0, 0]
[ 0, 0, 0, 0]
[ 0, 0, 0, 0]
global_k2 =
[ 0, 0, 0, 0]
[ 0, k2, -k2, 0]
[ 0, -k2, k2, 0]
[ 0, 0, 0, 0]
global_k3 =
[ 0, 0, 0, 0]
[ 0, k3, -k3, 0]
[ 0, -k3, k3, 0]
[ 0, 0, 0, 0]
global_k4 =
[ 0, 0, 0, 0]
[ 0, k4, 0, -k4]
[ 0, 0, 0, 0]
[ 0, -k4, 0, k4]
The stiffness matrix is:
[ k1, -k1, 0, 0]
[ -k1, k1 + k2 + k3 + k4, - k2 - k3, -k4]
[ 0, - k2 - k3, k2 + k3, 0]
[ 0, -k4, 0, k4]

13 Comments

I wanted to insert the indexed values in the zero matrix, not just add them. Thanks anyway
Check the code now , it works.
Not the result I intended. The addition is based on the indexed position. The way you suggested would just add the matrices
Can you show the result you intend? Just to understand what you actually want.
Plus your actual question was How to resolve the error?
This is basically to find the elemental and global stiffness matrix for finite element analysis.
Can you show an example of the indexed addition ? If you define the process what has to be found ? It would be narrowed instead of generalisation because it’s totally vague for now.
Something of the effect of this:
clear all
clc
syms k1 k2 k3 k4
global_k1 = [k1 -k1 0 0; -k1 k1 0 0; 0 0 0 0; 0 0 0 0];
global_k2 = [0 0 0 0; 0 k2 -k2 0; 0 -k2 k2 0; 0 0 0 0];
global_k3 = [0 0 0 0; 0 k3 -k3 0; 0 -k3 k3 0; 0 0 0 0];
global_k4 = [0 0 0 0; 0 k4 0 -k4 ; 0 0 0 0; 0 -k4 0 k4];
k_stiff = global_k1 + global_k2 + global_k3 + global_k4;
disp('The stiffness matrix is: ');
disp(k_stiff);
The difference is that here I had to manually input the symbolic values into a zero matrix and then add them. What I intended to do was to declare zero matrices for variable global_k1, k2 etc and then put variables into the matrices by calling variables into different positions by calling the function
global_k1(1,1) = k1;
global_k1(1,2) = -k1;
global_k1(2,1) = -k1;
global_k1(2,2) = k1;
And so on. Hope that clears the picture.
Code edited - first zero matrices were made and then symbolic variables inserted according to the index.If still you didn’t get the required result, just type the desired result in the next comment. I’ll give you the apt code.
The stiffness matrix is:
[ k1, -k1, 0, 0]
[ -k1, k1 + k2 + k3 + k4, - k2 - k3, -k4]
[ 0, - k2 - k3, k2 + k3, 0]
[ 0, -k4, 0, k4]
This is the result I am looking for
see the code I just posted the results you wanted and it works fine for me for your information , I am using Matlab 2017a release .

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!