MatLab assignment
MatLab assignment
1(a)
l=10;
w=20;
x=0:.5:l;
v=0.5*w*l-w*x;
m=(0.5*w*l*x)-(0.5*w*x.*x);
plot(x,v,'-*m','MarkerEdgeColor','c','MarkerFaceColor','r');
hold on
plot(x,m,'-<g','MarkerEdgeColor','b','MarkerFaceColor','k');
xlabel('Length of Beam','Fontsize',20);
ylabel('SFD and BMD','Fontsize',20);
title('SFD and BMD Diagram','Fontsize',20);
legend('SMD','BMD');
grid on
SFD and BMD Diagram
250
SMD
BMD
200
SFD and BMD
150
100
50
-50
-100
0 1 2 3 4 5 6 7 8 9 10
Length of Beam
1(b).
w=10;
p=10;
l=10
x1=0:0.5:l/2;
v1=p/2;
m1=0.5*x1*p;
x2=l/2:0.5:l;
v2=-p/2;
m2=0.5*p*l-0.5*p*x2
plot(x1,v1,'sk',x2,v2,'sk','MarkerEdgeColor','g','MarkerFaceColor','b')
hold on
plot(x1,m1,'or',x2,m2,'or','MarkerEdgeColor','c','MarkerFaceColor','m')
20
15
SFD, BMD
10
-5
0 1 2 3 4 5 6 7 8 9 10
Length of Beam
2(a,b)
l=10;
w=20;
x=0:0.5:l;
v=0.5*w*l-w*x;
m=(0.5*w*l*x)-(0.5*w*x.*x);
plot(x,v,'-*k');
hold on
plot(x,m,'-<r');
xlabel('Length of Column','FontSize', 15);
ylabel('SFD, BMD', 'FontSize', 15);
title('SFD & BMD Diagram ', 'FontSize', 15);
legend('SFD','BMD')
grid on
150
SFD, BMD
100
50
-50
-100
0 1 2 3 4 5 6 7 8 9 10
Length of Column
Result:
x=
30.5263
65.7895
-131.5789
-25.0000
-50.0000
30.5263
-65.7895
91.0526
-121.0526
1.
function [a,r2] = linregr(x,y)
n=length(x);
if length(y)~=n, error('x and y must be same length'); end
x=x(:);
y=y(:);
sx=sum(x);
sy=sum(y);
sx2=sum(x.*x);
sxy=sum(x.*y);
sy2=sum(y.*y);
a(1)=(n*sxy-sx*sy)/(n*sx2-sx^2);
a(2)=sy/n-a(1)*sx/n;
r2=((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;
xp=linspace(min(x),max(x),2);
yp=a(1)*xp+a(2);plot(x,y,'o',xp,yp);
grid on
a)
command window
x=[1.3 1.8 3 4.5 6 8 9];
y=[.07 .13 .22 .275 .335 .35 .36];
[a,r2]=linregr(x,y)
a=
0.0356 0.0775
r2 =
0.8814
>>
b.
for log log type
[a,r2] = linregr(log10(x),log10(y))
c.
x=[1.3 1.8 3 4.5 6 8 9];
y=[0.07 0.13 0.22 0.275 0.335 0.35 0.36];
z=[ones(length(x), 1) x']
a=z\y'
p=polyval(a,x)
hold on
plot(x,y,'-*',z,p,'-<m')
grid on
a=
0.0775
0.0356
p=
z=
1.0000 1.3000
1.0000 1.8000
1.0000 3.0000
1.0000 4.5000
1.0000 6.0000
1.0000 8.0000
1.0000 9.0000
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
1 2 3 4 5 6 7 8 9
subplot(221);
x=[1.3 1.8 3 4.5 6 8 9];
y=[0.07 0.13 0.22 0.275 0.335 0.35 0.36];
y1=0.0356*x+0.0775; %from a
plot(x,y,'o',x,y1,'-
r','LineWidth',1,'MarkerEdgeColor','m','MarkerFaceColor','b','MarkerSize',4
);
title('Linear regration','FontSize',10);
xlabel('X axis','FontSize',10);
ylabel('Y axis','FontSize',10);
legend('Scatter Data');
grid on
subplot(222);
x=[1.3 1.8 3 4.5 6 8 9];
y=[0.07 0.13 0.22 0.275 0.335 0.35 0.36];
y2=0.7961*x-1.1322; %from b
plot(x,y,'-s',x,y2,'-
k','LineWidth',1,'MarkerEdgeColor','b','MarkerFaceColor','g','MarkerSize',5
);
title('Linear regration with log log scale','FontSize',10);
xlabel('X axis','FontSize',10);
ylabel('Y axis','FontSize',10);
legend('Scatter Data');
grid on
subplot(223);
x=[1.3 1.8 3 4.5 6 8 9];
y=[0.07 0.13 0.22 0.275 0.335 0.35 0.36];
y3=0.0775*x+0.356; %from c
plot(x,y,'-<',x,y3,'-
g','LineWidth',1,'MarkerEdgeColor','b','MarkerFaceColor','g','MarkerSize',5
);
title('Linear regration with z matrix','FontSize',10);
xlabel('X axis','FontSize',10);
ylabel('Y axis','FontSize',10);
legend('Scatter Data');
grid on
Y axis
0.2
0
0.1
0 -5
0 5 10 0 5 10
X axis X axis
Linear regration with z matrix
1.5
Scatter Data
1
Y axis
0.5
0
0 5 10
X axis
2.
a)
x=[1.3 1.8 3 4.5 6 8 9];
y=[0.07 0.13 0.22 0.275 0.335 0.35 0.36];
z=[ones(length(x),1) x' x'.^2 x'.^3 x'.^4];
a=z\y'
result:
a=
-0.1251
0.1923
-0.0349
0.0033
-0.0001
b)
x=[1.3 1.8 3 4.5 6 8 9];
y=[0.07 0.13 0.22 0.275 0.335 0.35 0.36];
z=[ones(length(x),1) x' x'.^2 x'.^3 x'.^4];
a=z\y'
p=polyval(a,x)
plot(x,y,'sk',x,p,'-b')
legend('scatter');
grid on
C & d)
x=[1.3 1.8 3 4.5 6 8 9];
y=[0.07 0.13 0.22 0.275 0.335 0.35 0.36];
a=polyfit(x,y,4)
result:
a=
>>
3.
y=[1920 1930 1940 1950 1960 1970 1980 1990];
p=[106.46 123.08 132.12 152.27 180.67 205.05 227.23 249.46];
a=polyfit(y,p,7);
polyval(a,2000)
result
ans =
181.1719
1.
a,b)
x=[1.3 1.8 3 4.5 6 8 9];
y=[0.07 0.13 0.22 0.275 0.335 0.35 0.36];
a=polyfit(x,y,3)
d1=polyder(a)
d2=polyder(d1)
b=polyfit(x,y,4)
d_1=polyder(b)
d_2=polyder(d1)
result:
a=
0.0007 -0.0163 0.1423 -0.0835
d1 =
0.0020 -0.0326 0.1423
d2 =
0.0040 -0.0326
b=
-0.0001 0.0033 -0.0349 0.1923 -0.1251
d_1 =
-0.0005 0.0100 -0.0698 0.1923
d_2 =
0.0040 -0.0326
>>
2.
%By Trapeziodal Rule
x=[0 200 400 600 800 1000 1200 1400 1600 1800 2000 2200 2400 2600 2800 3000
3200 3100 3150 2900 2700 2500 2300];
y=[4200 4100 4050 4000 3900 3500 3300 3250 3200 3000 2850 2750 2450 2400
2250 2150 1500 1200 1300 1000 600 200 0 ];
plot(x,y,'--r')
trapz(x,y)
Result:
ans =
9380000
>>
%simpsons 1/3 rule
function f=simpsons(x,y)
n=length(x);
if mod(n,2)==0,error ('needed odd num');end;
f=0;
for i=1:2:n-2
f=f+((x(i+1)-x(i))*(y(i)+4*y(i+1)+y(i+2)))/6;
end
command window:
>>x=[0 200 400 600 800 1000 1200 1400 1600 1800 2000 2200 2400 2600 2800
3000 3200 3100 3150 2900 2700 2500 2300];
>>y=[4200 4100 4050 4000 3900 3500 3300 3250 3200 3000 2850 2750 2450 2400
2250 2150 1500 1200 1300 1000 600 200 0 ];
>>f=simpsons(x,y)
f =
4.5908e+006
3.
z=0:15:30; % for interval 15
f=200.*(z./(5+z)).*exp(-2*z./30);
Area=trapz(z,f)
Result:
Area =
1.0017e+003
4.
a)
function f=sumpsons_4_a(x,y)
n=length(x);
if mod(n,2)==0, error('odd number of multiple required : '), end
f=0;
for i=1:2:n-2
f=f+((x(i+1)-x(i))*(y(i)+4*y(i+1)+y(i+2)));
end
% command window
x=0:2.5:20;
v=[0.914 0.890 0.847 0.795 0.719 0.543 0.427 0.204 0];
y=2*3.1416*x.*v;
f=sumpsons_4_a(x,y)
f =
1.7254e+003
b)
Trapezoidal Rule
Command window
>> x=0:2.5:20;
>> v=[0.914 0.890 0.847 0.795 0.719 0.543 0.427 0.204 0];
>> y=2*3.1416*x.*v;
>> trapz(x,y)
ans =
571.3785
-- The End--