Hi there, I am trying to write a code on composite bars for n cross-sections, could someone help me with a simple code.

2 views (last 30 days)
o = 'Enter the number of cross-sections: ';
n = input(o);
prompt = 'Enter the load in N: ';
F = input(prompt);
if n == 1
y = 'Enter the Youngs modulus of Bar : ';
E1 = input(y);
b = 'Enter the length of Bar: ';
L1 = input(b);
s = 'Enter the diameter of Bar: ';
D1 = input(s);
A = (pi*(D1^2)/4);
Delta = ((F*L1)/(A*E1))
fprintf(' Total extension of the bar: %s.\n' ,Delta );
elseif n==2
y = 'Enter the Youngs modulus of Bar 1: ';
E1 = input(y);
z = 'Enter the Youngs modulus of Bar 2: ';
E2 = input(y);
b = 'Enter the length of Bar 1: ';
L1 = input(b);
d = 'Enter the length of Bar 2: ';
L2 = input(d);
s = 'Enter the diameter of Bar 1: ';
D1 = input(s);
f = 'Enter the diameter of Bar 2: ';
D2 = input(f);
A1 = (pi*(D1^2)/4);
A2 = (pi*(D2^2)/4);
delta1 = ((F*L1)/(A1*E1))
delta2 = ((F*L2)/(A2*E2))
Delta = delta1 + delta2;
fprintf(' Total extension of the bar: %s.\n' ,Delta );
This is the code for 2 cross-sections, the goal is to extend this to n cross-sections. Still learning.

Accepted Answer

Raj
Raj on 24 Jul 2019
Edited: Raj on 24 Jul 2019
Use this:
o = 'Enter the number of cross-sections: ';
n = input(o);
prompt = 'Enter the load in N: ';
F = input(prompt);
A=zeros(n,1);%preallocate A
Delta=A;%preallocate Delta
for ii=1:n
fprintf('Enter the Youngs modulus of Bar %i : \n',ii);
E = input('y=');
fprintf('Enter the length of Bar %i: \n',ii);
L = input('b=');
fprintf('Enter the diameter of Bar %i:\n',ii);
D = input('s=');
A(ii,1) = (pi*(D^2)/4);
Delta(ii,1) = ((F*L)/(A(ii,1)*E));
end
for jj=1:n
fprintf(' Total extension of the bar %i: %f. \n' ,jj,Delta(jj,1) );
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!