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?
采纳的回答
>> 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 个评论
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.
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
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 .
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Assumptions 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
