Exercise - For Statement
Exercise - For Statement
V=[5 -9 -3 0 8 0 -7 12 15];
for i=1:length(V)
if V(i)>0 && (rem(V(i),3)==0 || rem(V(i),5)==0)
V(i)=2*V(i);
elseif V(i)<0 && V(i)>-5
V(i)=V(i)^3;
end
end
V
V = 1×9
10 -9 -27 0 8 0 -7 24 30
%Problem Example 2
mat=input('Please enter the martix. \nEnclose input with[ ]\n');
[r,c]=size(mat);
for co=1:c
cosum=0;
for ro=1:r
if mat(ro,co)>=0
cosum=cosum+mat(ro,co);
end
end
fprintf('Sum of +ve no. of column %d is %.2f\n',co,cosum)
end
%Problem Example 3
q=input('Enter load intensity, q: ');
d1=input('Enter load start dist., d1: ');
d2=input('Enter load end dist., d2: ');
l=input('Enter beam span., l: ');
n=input('Enter the number of division: ');
if d1 >= 0 && d2 > d1 && d2 <= l
disp('Valid')
else
disp('Invalid. Please check data whether 0 ≤ d1 < d2 ≤ l')
end
Valid
maxbm=0;
qlen=d2-d1;qtot=qlen*q;
rb=(qtot*(d1+d2)/2)/l;
1
ra=qtot-rb;
d=l/n;
for nx=0:n
x=nx*d;
if x<=d1
bmx=ra*x;
elseif x<=d2
bmx=ra*x-q*(x-d1)^2/2;
else
bmx=rb*(l-x);
end
if bmx>maxbm
maxbm=bmx;
xmaxbm=x;
end
end
x_values(nx+1)=x;
bmx_values(nx+1)=bmx;
table(x_values',bmx_values','VariableNames',{'x','bmx'})
1 0 0
2 0.4211 11.3684
3 0.8421 22.7368
4 1.2632 34.1053
5 1.6842 45.4737
6 2.1053 56.7535
7 2.5263 65.9945
8 2.9474 72.3989
9 3.3684 75.9668
10 3.7895 76.6981
11 4.2105 74.5928
12 4.6316 69.6510
13 5.0526 61.8947
14 5.4737 53.0526
15 5.8947 44.2105
16 6.3158 35.3684
17 6.7368 26.5263
18 7.1579 17.6842
19 7.5789 8.8421
2
x bmx
20 8 0
plot(x_values,bmx_values)
xlabel('x')
ylabel('bmx')
title('Bending Moment Diagram')
grid on