How do I manually insert variables I have declared symbolically into a zero matrix?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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
madhan ravi
on 24 Aug 2018
Edited: madhan ravi
on 24 Aug 2018
>> 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
Salil Chawla
on 24 Aug 2018
I wanted to insert the indexed values in the zero matrix, not just add them. Thanks anyway
madhan ravi
on 24 Aug 2018
Edited: madhan ravi
on 24 Aug 2018
Check the code now , it works.
Salil Chawla
on 24 Aug 2018
Not the result I intended. The addition is based on the indexed position. The way you suggested would just add the matrices
madhan ravi
on 24 Aug 2018
Can you show the result you intend? Just to understand what you actually want.
madhan ravi
on 24 Aug 2018
Plus your actual question was How to resolve the error?
Salil Chawla
on 24 Aug 2018
This is basically to find the elemental and global stiffness matrix for finite element analysis.
madhan ravi
on 24 Aug 2018
Edited: madhan ravi
on 24 Aug 2018
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.
madhan ravi
on 24 Aug 2018
Edited: madhan ravi
on 24 Aug 2018
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.
Salil Chawla
on 24 Aug 2018
sym(zeros(4,4)); is an incorrect command
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
madhan ravi
on 24 Aug 2018
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 .
Salil Chawla
on 24 Aug 2018
Got it, thanks
More Answers (0)
Categories
Find more on Assumptions in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)