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 (최근 30일)
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.

채택된 답변

Raj
Raj 2019년 7월 24일
편집: Raj 2019년 7월 24일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by