Solve a system of symbolic variables

조회 수: 2 (최근 30일)
Ian Wood
Ian Wood 2012년 10월 16일
댓글: Walter Roberson 2014년 1월 26일
Hi,
I have been trying hard at this for a while now. Does anybody know if there is some way to solve symbolic variables in a matrix, resulting in a double format?
I want to solve for the unknowns of F and d. The amount of unknowns depends on the values specified in the input:
% This program calculates any straight equal-length element analysis
% for a bar with two fixed ends
syms Freact1 Freact2;
A = input('Cross-sectional area (mm^2): ');
E = input('Elastic Modulus (MPa): ');
L = input('Length of the system (mm): ');
num_ele = input('Enter the number of elements to be analyzed: ');
num_nodes = num_ele + 1;
k = (E*A)/(L/num_ele);
F = zeros(num_nodes,1);
d = zeros(num_nodes,1);
d(1) = 0; d(num_nodes) = 0;
F1 = vpa(Freact1); Fend = vpa(Freact2);
F(1) = F1; F(num_nodes) = Fend;
disp(' ');
for i = 2:num_ele
str = sprintf('Node %d: ', i);
disp(str);
F(i) = input('Enter the force at the node (N): ');
disp(' ');
end
glbl_stiff = 0*diag(num_nodes,num_nodes-1) + 2*eye(num_nodes);
glbl_stiff(1,1) = 1; glbl_stiff(num_nodes,num_nodes) = 1;
for j = 1:num_ele
glbl_stiff(j,j+1) = -1;
glbl_stiff(j+1,j) = -1;
end
glbl_stiff = k*glbl_stiff;
F = glbl_stiff*d
d = F\glbl_stiff
and the error that always results:
The following error occurred converting from sym to
double:
Error using mupadmex
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.
Error in FEM_IA1 (line 20)
F(1) = F1; F(num_nodes) = Fend;
It's confusing because I did use the VPA function. It just seems like nothing's working.. any ideas would greatly be appreciated.
Thanks, Ian

답변 (3개)

Star Strider
Star Strider 2012년 10월 16일
편집: Star Strider 2012년 10월 16일
The variables Freact1 and Freact2 in that line are symbolic variables. You haven't assigned any numeric values to them.
  댓글 수: 14
Ian Wood
Ian Wood 2012년 10월 17일
I need to experiment with these functions a little bit before I can confirm. Thanks a lot for your help to date.
Star Strider
Star Strider 2012년 10월 17일
I'll keep this open until we're happy we've converged on a solution then.

댓글을 달려면 로그인하십시오.


Matt Fig
Matt Fig 2012년 10월 16일
Use symbolic arrays instead:
F = sym(zeros(num_nodes,1));
d = sym(zeros(num_nodes,1));
  댓글 수: 3
Phan HaNhut
Phan HaNhut 2014년 1월 26일
if memory of computer is slow, code can not end? (matlab: "busy") And there is not any result?
I have to add more RAM?
Walter Roberson
Walter Roberson 2014년 1월 26일
If your memory is limited, then you can speed up operations by turning off virtual memory (or configuring it to be size 0). Swapping memory to disk is very very slow, and my practical experience on MS Windows systems is that once you swap enough program memory to disk then you cannot make any progress because you run into "thrashing" (part of memory you need for the calculation is swapped out in order to bring in something else you need, but then that has to get swapped out in order to bring the first back in in order to proceed, but then that needs... etc.) Turning off virtual memory would result in the calculation failing cleanly with complaints about insufficient memory instead of running for days getting nowhere.
Expanding physical memory is usually good. You may need to switch to a 64 bit operating system (with the extra memory) to make real progress.

댓글을 달려면 로그인하십시오.


Ian Wood
Ian Wood 2012년 10월 17일
OK, so working through this I came up with another code, and I provided some comments to explain what I did. The code changed quite a bit, but the desired end result is still the same.
Still don't know how to solve for the unknowns (F and d), but I've figured out how to eliminate the entries that are not needed to solve the equations.
Here's my updated code:
% This program calculates any series-element analysis for a bar
A = input('Cross-sectional area (mm^2): ');
E = input('Elastic Modulus (MPa): ');
L = input('Length of the system (mm): ');
num_ele = input('Enter the number of elements to be analyzed: ');
num_nodes = num_ele + 1;
% initialize the element length vector
ele_length = zeros(num_ele,1);
% input the length of each element, and if the sum is not equal to
% specified input, quit the program
for n=1:num_ele
disp(' ');
str = sprintf('Element %d: ', n);
disp(str);
ele_length(n) = input('Enter the length of the element (mm): ');
end
if sum(ele_length) ~= L
error('error: lengths are not equal');
end
k = (E*A)./(ele_length); % stiffness constant for each element length
% initialize force, displacement, and degree of freedom vectors
F = zeros(num_nodes,1);
d = zeros(num_nodes,1);
DOF = zeros(num_nodes,1);
% prompt for nodal forces and degrees of freedom for each node. A degree of
% freedom of zero means a fixed location, and thus displacement of zero.
for m = 1:num_nodes
disp(' ');
str = sprintf('Node %d: ', m);
disp(str);
F(m) = input('Enter the force at the node in newtons: ');
DOF = input('Enter the degrees of freedom (0 or 1): ');
if DOF == 0
d(m) = 0;
% if there is a degree of freedom, there exists displacement
elseif DOF == 1
d(m) = input('Enter the displacement at the node in mm: ');
else
error('error: invalid input');
end
end
% obtain the global stiffness matrix
glbl_stiff = 0*diag(num_nodes,num_nodes-1) + 2*eye(num_nodes);
glbl_stiff(1,1) = 1; glbl_stiff(num_nodes,num_nodes) = 1;
for j = 1:num_ele
glbl_stiff(j,j+1) = -1;
glbl_stiff(j+1,j) = -1;
end
glbl_stiff = sum(k)*glbl_stiff;
ind = find(d==0); % find the indices where the elements are zero
% remove elements corresponding to boundary condition
glbl_stiff(ind,:) = [];
glbl_stiff(:,ind) = [];
F(ind) = [];
d(ind) = [];
% d = lsqr(glbl_stiff,F)
% F = glbl_stiff*d
  댓글 수: 3
Ian Wood
Ian Wood 2012년 10월 18일
편집: Ian Wood 2012년 10월 18일
Well I do want to input some F and d values, but not all values. Certain values will be desired. glbl_stiff will always have completely known values. As an example for a two-element analysis, F could be [F1; 1000; F3] and d [0 d2 0]. I would need to find F1, F3, and d2 with the use of glbl_stiff.
This is why I thought that the use of symbolic variables could be implemented, but apparently there is no need to use them to solve this system (according to my professor).
Star Strider
Star Strider 2012년 10월 18일
Scanning the book you referred me to earlier (the discussions on pages 46-7), it seems that you would solve independently for the forces with zero (or other defined) displacements and then for the displacements with known forces as separate procedures.
So you would for instance solve for d2 as a linear equation with a force of 1000, then use sparse matrix techniques to solve for F1 and F3 with displacements = 0. That seems to be what the book suggests, and that's certainly how I would do it. It doesn't seem mathematically possible to do both simultaneously, since that would mean having unknowns on both sides of the equation.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by