User:Eml4500.f08.team.foskey.ckf/HW3

Note and Acknowledgements
The following MATLAB code was based on example programs written by X. G. Tan and Team Meshworks. By reviewing their earlier work, the basic methodology of creating MATLAB plots was found, and understood. The program for the two bar truss problem borrows heavily from the work of X. G. Tan.

The programs by X. G. Tan and Team Meshworks are provided near the end of this article.

Creating A MATLAB Program For The Two Bar Truss
This MATLAB program shows the original unloaded truss, as well as the truss that has deformed as a result of the load P. Essentially, the program calculated the coordinates of the elemental nodes for the loaded and unloaded trusses. From there it assigns those points to each element, and then gives the elemental nodes their corresponding global nodes. The program then connects the global nodes. In the figure, the dotted lines represent the elements of the unloaded truss, and the solid lines are the deformed elements from the loaded truss.

Matlab Program Code
% Matlab Script %*********************************************************************  % filename: two_bar_truss_prob.m   % % PURPOSE: %   Example of plotting with Matlab %   Input nodes and connectivity information %   Plot the two bar truss structure %  % AUTHOR: C. Foskey %  %   % Created on :  Sept. 29, 2008 %  %   %*********************************************************************   clear; close; % model with 2-D beam elements with 2 dofs per node dof = 2;        %  dof per node % 1 : axial disp x,  2 : disp y    % input the nodal coordinates n_node = 4; % total number of nodes (2 fixed point nodes,                        %   1 unfixed-undeformed, 1 unfixed-deformed) n_elemUND = 2;            % total number of elements(undeformed) n_elemD=2;              %total number of elements (deformed) total_dof = 2 * n_node; % total dofs of system Ax=4*cos(30*pi/180); %x-cord of node 2 Ay=4*sin(30*pi/180); %y-cord of node 2 Fx=2*cos(45*pi/180); %x-cord of node 3(called point F)   % must be added to Ax Fy=-2*sin(45*pi/180);  %x-cord of node 3(callled point F)  % must be added to Ay %%undeformend points position(:, 1) = [ 0; 0; 0]; % location of node 1 (origin) position(:, 2) = [ Ax; Ay; 0];   % location of node 2 position(:, 3) = [ Ax+Fx; Ay+Fy; 0]; %location of node 3 %%calculating deformed points p1=5.124*cos(30*pi/180); p2=6.276*sin(45*pi/180); k1=3/4; k2=5; q1=p1/k1; % length AC   q2=p2/k2;   %lenght AB   Bx=-q2*cos(45*pi/180)+Ax;   %x-cordinate of point B   By = q2*sin(45*pi/180)+Ay;   %y-cordinate of point B   Cx=q1*cos(30*pi/180)+Ax;   %x-cordinate of point C   Cy=q1*sin(45*pi/180)+Ay;   %y-cordinate of point C    %find intersect xval = 7.7844912; yAB = (-(tan(-45*pi/180))^-1)*(xval-Bx) +By; yAC = (-(tan(30*pi/180))^-1)*(xval-Cx) + Cy; Dx=xval; Dy=yAB; %%deformed points position(:, 4) = [ Dx; Dy; 0]; %location of node 4 %node 4 = deformed node 3 % nodes 1, 2 remain same for deformed because they are fixed % set up the nodal coordinate arrays x, y  for i = 1 : n_node x(i) = position(1,i); y(i) = position(2,i); end % set up the element connectivity array node_connect, defined as % follows: % node_connect(local node number, element number) = global node number node_connect(1, 1) = 1;  % element 1 node_connect(2, 1) = 2; node_connect(1, 2) = 2;  % element 2 node_connect(2, 2) = 3; node_connect(1, 3) = 1;  % element 3= deformed element 1 node_connect(2, 3) = 4; node_connect(1, 4) = 4;  % element 4=deformed element 2 node_connect(2, 4) = 3; % plot the whole truss system % loop over the elements for i = 1 : n_elemUND % for element i, do the following: % node_1 = global node number corresponding to the local node 1 node_1 = node_connect(1,i); % node_2 = global node number corresponding to the local node 1 node_2 = node_connect(2,i); % xx : 1x2 array containing x coordinates of node_1 and node_2 xx = [x(node_1),x(node_2)]; % yy : 1x2 array containing y coordinates of node_1 and node_2 yy = [y(node_1),y(node_2)]; % plot the element i in 2-D using command plot % use axis command to avoid having the plot window too tight axis([-1 10 -1 10]) % dotted line plot(xx,yy,':') % hold the current figure for the next element hold on  end %%loop for deformed elements for i = (1+2) : (n_elemD+2) % for element i, do the following: % node_1 = global node number corresponding to the local node 1 node_1 = node_connect(1,i); % node_2 = global node number corresponding to the local node 1 node_2 = node_connect(2,i); % xx : 1x2 array containing x coordinates of node_1 and node_2 xx = [x(node_1),x(node_2)]; % yy : 1x2 array containing y coordinates of node_1 and node_2 yy = [y(node_1),y(node_2)]; % plot the element i in 2-D using command plot % use axis command to avoid having the plot window too tight axis([-2 10 -2 10]) % solid line plot(xx,yy,'-') % hold the current figure for the next element hold on  end % label nodes, elements text(-.5,-.5,0, 'Node 1') text(Ax, Ay+.5, 0 ,'Node 2') text(Fx+Ax+.5, Fy+Ay-.5,0, 'Node 3') text(Dx-2, Dy+.5,0, 'Displaced Node 2') text(.5*Ax-.5, .5*Ay, 0, 'Element 1') text(Ax+.5*(Fx)-1, Ay+.5*(Fy),0,'Element 2') text(.5*Dx-2, .5*Dy, 0,'Deformed Element 1') text(Dx-.5*(Dx-Fx), Dy-.5*(Dy-Fy), 0,'Deformed Element 2') % put the title on the figure title('Two bar truss') % label x axis xlabel('x') % label y axis ylabel('y') % label z axis zlabel('z') view([0 0 1])     % xy plane view

The Resulting Figure
The above MATLAB program generates the following figure:



In the figure, the dotted lines represent the undeformed, unloaded elements of the two-bar truss. The solid lines represent the deformed elements that result from the load being applied at global node 2.

The example given in class used the global node 2 of the undeformed truss as the origin. In this figure, the origin is located at global node 1. This was done for two reasons. The first being that global node 1 is a fixed point, and remains the same for both the unloaded and the loaded trusses. The second reason was for node position calculation simplicity. By choosing the origin to be at node 1, all of the node positions are positive, and the math required to calculate their positions was generally more intuitive.

The Example MATLAB Programs
The following programs were provided as examples to study prior to the creation of the program of the two bar truss.

The Crab-Leg Structure (matlab_plot_example2.m)
% Matlab Script %*********************************************************************  % filename: matlab_plot_example2.m   % % PURPOSE: %   Example of plotting with Matlab %   Input nodes and connectivity information %   Plot the crab-leg structure %  % AUTHOR: X.G. Tan %  % Modified on: Tue, 23 Sep 2003, 16:08:58 EDT % Created on : Tue, 09 Feb 1999, 09:40:47 EST %  % DEPENDENCIES: %  call: %  % REMARKS: %  %*********************************************************************   clear; close; % model with 3-D beam elements with 6 dofs per node dof = 6;        %  dof per node % 1 : axial disp x,  2 : disp y  , 3 : disp z                    %  4 : torsion x    ,  5 : bend y  , 6 : bend z  % % input the nodal coordinates %  n_node = 17;             % total number of nodes n_elem = 20;            % total number of elements total_dof = 6 * n_node; % total dofs of system l_h_1 = 200.e-6;  % horizontal beam length at center l_h_2 = 50.e-6;   % horizontal beam length at corner l_v_1 = 200.e-6;  % vertical beam length at center l_v_2 = 100.e-6;  % vertical beam length at corner position(:, 9) = [     0; 0; 0]; position(:, 8) = [- l_h_1; 0; 0]; position(:,10) = [ l_h_1; 0; 0]; position(:, 4) = [     0;  l_v_1; 0]; position(:, 3) = [- l_h_1; l_v_1; 0]; position(:, 5) = [ l_h_1;  l_v_1; 0]; position(:,14) = [     0; - l_v_1; 0]; position(:,13) = [- l_h_1; - l_v_1; 0]; position(:,15) = [ l_h_1; - l_v_1; 0]; position(:, 2) = [- l_h_1 - l_h_2;  l_v_1; 0]; position(:, 6) = [ l_h_1 + l_h_2;   l_v_1; 0]; position(:,12) = [- l_h_1 - l_h_2; - l_v_1; 0]; position(:,16) = [ l_h_1 + l_h_2; - l_v_1; 0]; position(:, 1) = [- l_h_1 - l_h_2;  l_v_1 + l_v_2; 0]; position(:, 7) = [ l_h_1 + l_h_2;   l_v_1 + l_v_2; 0]; position(:,11) = [- l_h_1 - l_h_2; - l_v_1 - l_v_2; 0]; position(:,17) = [ l_h_1 + l_h_2; - l_v_1 - l_v_2; 0]; % set up the nodal coordinate arrays x, y, x  for i = 1 : n_node x(i) = position(1,i); y(i) = position(2,i); z(i) = position(3,i); end % set up the element connectivity array node_connect, defined as % follows: % node_connect(local node number, element number) = global node number node_connect(1, 1) = 1;  % element 1 node_connect(2, 1) = 2; node_connect(1, 2) = 2;  % element 2 node_connect(2, 2) = 3; node_connect(1, 3) = 3;  % element 3 node_connect(2, 3) = 4; node_connect(1, 4) = 4;  % element 4 node_connect(2, 4) = 5; node_connect(1, 5) = 5; node_connect(2, 5) = 6; node_connect(1, 6) = 6; node_connect(2, 6) = 7; node_connect(1, 7) = 3; node_connect(2, 7) = 8; node_connect(1, 8) = 4; node_connect(2, 8) = 9; node_connect(1, 9) = 5; node_connect(2, 9) = 10; node_connect(1,10) = 8; node_connect(2,10) = 9; node_connect(1,11) = 9; node_connect(2,11) = 10; node_connect(1,12) = 8; node_connect(2,12) = 13; node_connect(1,13) = 9; node_connect(2,13) = 14; node_connect(1,14) = 10; node_connect(2,14) = 15; node_connect(1,15) = 11; node_connect(2,15) = 12; node_connect(1,16) = 12; node_connect(2,16) = 13; node_connect(1,17) = 13; node_connect(2,17) = 14; node_connect(1,18) = 14; node_connect(2,18) = 15; node_connect(1,19) = 15; node_connect(2,19) = 16; node_connect(1,20) = 16; node_connect(2,20) = 17; % plot the whole truss system % loop over the elements for i = 1 : n_elem % for element i, do the following: % node_1 = global node number corresponding to the local node 1 node_1 = node_connect(1,i); % node_2 = global node number corresponding to the local node 1 node_2 = node_connect(2,i); % xx : 1x2 array containing x coordinates of node_1 and node_2 xx = [x(node_1),x(node_2)]; % yy : 1x2 array containing y coordinates of node_1 and node_2 yy = [y(node_1),y(node_2)]; % zz : 1x2 array containing z coordinates of node_1 and node_2 zz = [z(node_1),z(node_2)]; % plot the element i in 3-D using command plot3 % use axis command to avoid having the plot window too tight axis([-3e-4 3e-4 -4.5e-4 4.5e-4]) % solid line plot3(xx,yy,zz,'-') % hold the current figure for the next element hold on  end % put the title on the figure title('Crab-leg structure w/ 3-D beam elements') % label x axis xlabel('x') % label y axis ylabel('y') % label z axis zlabel('z') % attempt to view the plotted 3-D truss structure from different % viewpoint. % plot viewed from z axis, i.e., projection on xy plane view([0 0 1])     % xy plane view % plot viewed from y axis, i.e., projection on xz plane % view([0 1 0])    % xz plane view % plot viewed from x axis, i.e., projection on yz plane % view([1 0 0])    % yz plane view % produce PostScript (PS) file for printing on a PS printer print -dps crab_leg.ps

The Electric Pylon (electric_pylon.m)
% Matlab Script %*********************************************************************  % filename: electric_pylon.m   % % PURPOSE: %  Plot an electric pylon ("superman") structure with matlab %  Prepare the data for FE analysis %  % AUTHOR: %   Team MeshWorks %   EAS 4200C Aerospace Structures 2, Spring 2000. %  % Modified on: vql, Fri, 03 Oct 2003, 08:37:46 EDT % Created on : Spring 2000 %  % DEPENDENCIES: %  call: %  % REMARKS: %  %*********************************************************************   % Plot the "superman" structure %  clear; close; % model with 3-D beam elements dof = 2;         %  dof per node:  1= axial disp x,  2= disp y  % obtain the coordinates of all nodes %  n_node = 46;             % number of nodes n_elem = 91;            % number of elements total_dof = 2 * n_node; % total dof of system position(:, 1) = [ 1.5; 0; 0]; position(:, 2) = [ 4.53; 0; 0]; position(:, 3) = [ 1.88; 1.18; 0]; position(:, 4) = [ 3.03; 1.18; 0]; position(:, 5) = [ 4.17; 1.18; 0]; position(:, 6) = [ 2.2; 2.181; 0]; position(:, 7) = [ 3.03; 2.181; 0]; position(:, 8) = [ 3.85; 2.181; 0]; position(:, 9) = [ 2.53; 3.181; 0]; position(:, 10) = [ 3.53; 3.181; 0]; position(:, 11) = [ 2.34; 3.681; 0]; position(:, 12) = [ 3.03; 3.681; 0]; position(:, 13) = [ 3.71; 3.681; 0]; position(:, 14) = [ 2.16; 4.181; 0]; position(:, 15) = [ 2.62; 4.181; 0]; position(:, 16) = [ 3.44; 4.181; 0]; position(:, 17) = [ 3.89; 4.181; 0]; position(:, 18) = [ 2.36; 4.5; 0]; position(:, 19) = [ 3.7; 4.5; 0]; position(:, 20) = [ 1.8; 5.172; 0]; position(:, 21) = [ 4.25; 5.172; 0]; position(:, 22) = [ 0; 6; 0]; position(:, 23) = [ 1.06; 6; 0]; position(:, 24) = [ 1.5; 6; 0]; position(:, 25) = [ 1.82; 6; 0]; position(:, 26) = [ 2.22; 6; 0]; position(:, 27) = [ 2.71; 6; 0]; position(:, 28) = [ 3.34; 6; 0]; position(:, 29) = [ 3.84; 6; 0]; position(:, 30) = [ 4.23; 6; 0]; position(:, 31) = [ 4.55; 6; 0]; position(:, 32) = [ 5; 6; 0]; position(:, 33) = [ 6.05; 6; 0]; position(:, 34) = [ .8; 6.23; 0]; position(:, 35) = [ 5.25; 6.23; 0]; position(:, 36) = [ 1.24; 6.355; 0]; position(:, 37) = [ 4.82; 6.355; 0]; position(:, 38) = [ 1.5; 6.43; 0]; position(:, 39) = [ 2; 6.43; 0]; position(:, 40) = [ 2.48; 6.43; 0]; position(:, 41) = [ 3.03; 6.43; 0]; position(:, 42) = [ 3.58; 6.43; 0]; position(:, 43) = [ 4.05; 6.43; 0]; position(:, 44) = [ 4.55; 6.43; 0]; position(:, 45) = [ 1.5; 7.43; 0]; position(:, 46) = [ 4.55; 7.43; 0]; % print the node coord. for i = 1 : n_node; x(i) = position(1,i); y(i) = position(2,i); z(i) = position(3,i); end node_connect(1, 1) = 1;  % element 1 node_connect(2, 1) = 3; node_connect(1, 2) = 1;  % element 2 node_connect(2, 2) = 4; node_connect(1, 3) = 3;  % element 5 node_connect(2, 3) = 4; node_connect(1, 4) = 4;  % element 3 node_connect(2, 4) = 2; node_connect(1, 5) = 4;  % element 6 node_connect(2, 5) = 5; node_connect(1, 6) = 2;  % element 4 node_connect(2, 6) = 5; node_connect(1, 7) = 3;  % element 8 node_connect(2, 7) = 7; node_connect(1, 8) = 5;  % element 9 node_connect(2, 8) = 7; node_connect(1, 9) = 3;  % element 7 node_connect(2, 9) = 6; node_connect(1, 10) = 5;  % element 10 node_connect(2, 10) = 8; node_connect(1, 11) = 6;  % element 11 node_connect(2, 11) = 7; node_connect(1, 12) = 7;  % element 12 node_connect(2, 12) = 8; node_connect(1, 13) = 6;  % element 13 node_connect(2, 13) = 9; node_connect(1, 14) = 7;  % element 14 node_connect(2, 14) = 9; node_connect(1, 15) = 7;  % element 15 node_connect(2, 15) = 10; node_connect(1, 16) = 8;  % element 16 node_connect(2, 16) = 10; node_connect(1, 17) = 9;  % element 17 node_connect(2, 17) = 10; node_connect(1, 18) = 9;  % element 18 node_connect(2, 18) = 11; node_connect(1, 19) = 9;  % element 19 node_connect(2, 19) = 12; node_connect(1, 20) = 10;  % element 20 node_connect(2, 20) = 12; node_connect(1, 21) = 10;  % element 21 node_connect(2, 21) = 13; node_connect(1, 22) = 11;  % element 22 node_connect(2, 22) = 12; node_connect(1, 23) = 12;  % element 23 node_connect(2, 23) = 13; node_connect(1, 24) = 11;  % element 24 node_connect(2, 24) = 14; node_connect(1, 25) = 11;  % element 25 node_connect(2, 25) = 15; node_connect(1, 26) = 12;  % element 26 node_connect(2, 26) = 15; node_connect(1, 27) = 12;  % element 27 node_connect(2, 27) = 16; node_connect(1, 28) = 13;  % element 28 node_connect(2, 28) = 16; node_connect(1, 29) = 13;  % element 29 node_connect(2, 29) = 17; node_connect(1, 30) = 14;  % element 30 node_connect(2, 30) = 15; node_connect(1, 31) = 15;  % element 31 node_connect(2, 31) = 16; node_connect(1, 32) = 16;  % element 32 node_connect(2, 32) = 17; node_connect(1, 33) = 14;  % element 33 node_connect(2, 33) = 20; node_connect(1, 34) = 14;  % element 34 node_connect(2, 34) = 18; node_connect(1, 35) = 15;  % element 35 node_connect(2, 35) = 18; node_connect(1, 36) = 16;  % element 36 node_connect(2, 36) = 19; node_connect(1, 37) = 17;  % element 37 node_connect(2, 37) = 19; node_connect(1, 38) = 17;  % element 38 node_connect(2, 38) = 21; node_connect(1, 39) = 18;  % element 39 node_connect(2, 39) = 20; node_connect(1, 40) = 19;  % element 40 node_connect(2, 40) = 21; node_connect(1, 41) = 20;  % element 41 node_connect(2, 41) = 24; node_connect(1, 42) = 20;  % element 42 node_connect(2, 42) = 26; node_connect(1, 43) = 21;  % element 43 node_connect(2, 43) = 29; node_connect(1, 44) = 21;  % element 44 node_connect(2, 44) = 31; node_connect(1, 45) = 22;  % element 45 node_connect(2, 45) = 23; node_connect(1, 46) = 23;  % element 46 node_connect(2, 46) = 24; node_connect(1, 47) = 24;  % element 47 node_connect(2, 47) = 25; node_connect(1, 48) = 25;  % element 48 node_connect(2, 48) = 26; node_connect(1, 49) = 26;  % element 49 node_connect(2, 49) = 27; node_connect(1, 50) = 27;  % element 50 node_connect(2, 50) = 28; node_connect(1, 51) = 28;  % element 51 node_connect(2, 51) = 29; node_connect(1, 52) = 29;  % element 52 node_connect(2, 52) = 30; node_connect(1, 53) = 30;  % element 53 node_connect(2, 53) = 31; node_connect(1, 54) = 31;  % element 54 node_connect(2, 54) = 32; node_connect(1, 55) = 32;  % element 55 node_connect(2, 55) = 33; node_connect(1, 56) = 22;  % element 56 node_connect(2, 56) = 34; node_connect(1, 57) = 23;  % element 57 node_connect(2, 57) = 34; node_connect(1, 58) = 23;  % element 58 node_connect(2, 58) = 36; node_connect(1, 59) = 24;  % element 59 node_connect(2, 59) = 36; node_connect(1, 60) = 24;  % element 60 node_connect(2, 60) = 38; node_connect(1, 61) = 25;  % element 61 node_connect(2, 61) = 38; node_connect(1, 62) = 25;  % element 62 node_connect(2, 62) = 39; node_connect(1, 63) = 26;  % element 63 node_connect(2, 63) = 39; node_connect(1, 64) = 26;  % element 64 node_connect(2, 64) = 40; node_connect(1, 65) = 27;  % element 65 node_connect(2, 65) = 40; node_connect(1, 66) = 27;  % element 66 node_connect(2, 66) = 41; node_connect(1, 67) = 28;  % element 67 node_connect(2, 67) = 41; node_connect(1, 68) = 28;  % element 68 node_connect(2, 68) = 42; node_connect(1, 69) = 29;  % element 69 node_connect(2, 69) = 42; node_connect(1, 70) = 29;  % element 70 node_connect(2, 70) = 43; node_connect(1, 71) = 30;  % element 71 node_connect(2, 71) = 43; node_connect(1, 72) = 30;  % element 72 node_connect(2, 72) = 44; node_connect(1, 73) = 31;  % element 73 node_connect(2, 73) = 44; node_connect(1, 74) = 31;  % element 74 node_connect(2, 74) = 37; node_connect(1, 75) = 32;  % element 75 node_connect(2, 75) = 37; node_connect(1, 76) = 32;  % element 76 node_connect(2, 76) = 35; node_connect(1, 77) = 33;  % element 77 node_connect(2, 77) = 35; node_connect(1, 78) = 34;  % element 78 node_connect(2, 78) = 36; node_connect(1, 79) = 35;  % element 79 node_connect(2, 79) = 37; node_connect(1, 80) = 36;  % element 80 node_connect(2, 80) = 38; node_connect(1, 81) = 37;  % element 81 node_connect(2, 81) = 44; node_connect(1, 82) = 38;  % element 82 node_connect(2, 82) = 39; node_connect(1, 83) = 39;  % element 83 node_connect(2, 83) = 40; node_connect(1, 84) = 40;  % element 84 node_connect(2, 84) = 41; node_connect(1, 85) = 41;  % element 85 node_connect(2, 85) = 42; node_connect(1, 86) = 42;  % element 86 node_connect(2, 86) = 43; node_connect(1, 87) = 43;  % element 87 node_connect(2, 87) = 44; node_connect(1, 88) = 38;  % element 88 node_connect(2, 88) = 45; node_connect(1, 89) = 39;  % element 89 node_connect(2, 89) = 45; node_connect(1, 90) = 43;  % element 90 node_connect(2, 90) = 46; node_connect(1, 91) = 44;  % element 91 node_connect(2, 91) = 46; % connect all beam elements by connectivity array for i = 1 : n_elem; node_1 = node_connect(1,i); node_2 = node_connect(2,i); xx = [x(node_1),x(node_2)]; yy = [y(node_1),y(node_2)]; zz = [z(node_1),z(node_2)]; plot3(xx,yy,zz,'-') hold on  end title('Electric Pylon') xlabel('x') ylabel('y') zlabel('z') view([0 0 1])     % xy plane view % view([0 1 0])    % xz plane view %view([1 0 0])    % yz plane view % produce postscript file for later printing % print -dps electric_pylon.ps  print -deps electric_pylon.ps