东南大学短学期MATLAB作业MatlabWorksheet2.docx
《东南大学短学期MATLAB作业MatlabWorksheet2.docx》由会员分享,可在线阅读,更多相关《东南大学短学期MATLAB作业MatlabWorksheet2.docx(39页珍藏版)》请在冰豆网上搜索。
东南大学短学期MATLAB作业MatlabWorksheet2
MatlabWorksheet2
PartA(In-classroomexercises)
1.Usingplottodisplaythefollowingvoltagewithappropriatelinetype,titleandlabels.Alsopresentthegraphwithsuitablerangesofaxis.
wheref=50Hz.
(Hint:
timeintervalmustbesmallenough,e.g.0.001seconds.Therefore,t=0:
0.001:
1isappropriate);
closeall
f=50;
t=0:
1/3000:
1;
v=220*sin(2*pi*f*t);
plot(t,v);
xlabel('t');
ylabel('v');
title('v(t)=220sin2\pift');
axis([00.03-max(v)*1.5max(v)*1.5]);
2.Usingplottodisplaythefollowingvoltagewithappropriatelinetype,titleandlabels.Alsopresentthegraphwithsuitablerangesofaxis.
wheref=50Hz.
Inaddition,onthesamegraph,drawtheenvelopeoftheoscillationandaddlegends.
closeall
f=50;
t=0:
1/1000:
1;
v=220*exp(-5*t).*sin(2*pi*f*t);
plot(t,v);
xlabel('t');
ylabel('v');
title('v(t)=220sin2\pift');
holdon;
v2=220*exp(-5*t);
plot(t,v2,'r');
plot(t,-v2,'r');
legend('theoscillation','theenvelopeoftheoscillation');
axis([00.5-max(v)*1.5max(v)*1.5]);
3.Usesubplot,drawa2by2arrayofplotsforthefollowingfunctions:
Applyappropriatelinetype,title,labelsandaxisrangesforthegraphs.
closeall
t=0:
0.01:
1;
v1=cos(10*pi*t);
v2=exp(-5*t).*cos(10*pi*t);
v3=exp(-10*t).*cos(10*pi*t);
v4=exp(-20*t).*cos(10*pi*t);
subplot(221);
plot(t,v1);xlabel('t');ylabel('v1');title('v(t)=cos(10\pit)');
holdon;
subplot(222);
plot(t,v2);xlabel('t');ylabel('v2');title('v(t)=e^{-5}cos(10\pit)');
subplot(223);
plot(t,v3);xlabel('t');ylabel('v3');title('v(t)=e^{-10}cos(10\pit)');
subplot(224);
plot(t,v4);xlabel('t');ylabel('v4');title('v(t)=e^{-20}cos(10\pit)');
4.Useplot3toplot2spiralcurveslikebelowwithappropriatelinewidthandcolour.
closeall
t=0:
0.01*pi:
20*pi;
x=t.^2.*sin(t);
y=t.^2.*cos(t);
z=5*t;
subplot(121);
plot3(x,y,z,'g');
holdon;
x=t.^0.5.*sin(t);
y=t.^0.5.*cos(t);
subplot(122);
plot3(x,y,z,'r');
5.Displaythesurfaceusingmeshandcontourwithasuitableresolution:
closeall
[x,y]=meshgrid(0:
1:
100,0:
1:
100);
z=exp(-0.005*((x-50).^2+(y-50).^2));
figure
(1);mesh(x,y,z);
figure
(2);contour(x,y,z);
6.Load2ofyourphotosintoMatlabWorkSpaceusingimread.a)Changebrightnesslocallyorglobally.b)Overlapthemtoproduceanewphoto.c)writeanewphotointoafileusingimwrite.(Note:
lowerversionofMatlabsuchas6.5isnotallowedtodosomedirectimageoperations.)
closeall
small=imread('small.png');
big=imread('big.png');
b_size=size(big);
s_size=size(small);
small(b_size
(1),b_size
(2),b_size(3))=0;
form=1:
s_size
(1)
forn=1:
s_size
(2)
forp=1:
s_size(3)
big(m,n,p)=small(m,n,p);
end
end
end
imshow(big);
imwrite(big,'new.png');
7.Forlinearsimultaneousequations
theequationcoefficients:
A=[1-143
-545-6
07-89
-13-26];(M=N)
a)FindthedeterminantofA,
b)FindtheinverseofAandcheckformatrixsingularity,
c)IfB=[5;1;-2;3],findtheunknownxintheequation.
Answer:
a)>>det(A)
ans=
-765.0000
b)>>inv(A)
ans=
0.3333-0.00000.3333-0.6667
0.20000.11760.2706-0.3882
0.20000.05880.0353-0.0941
0.0222-0.0392-0.06800.2183
Aisnotsingular.
c)>>A\B
ans=
-1.0000
-0.5882
0.7059
0.8627
8.Forlinearsimultaneousequations,
M>N,theequationcoefficients:
A=[1-143
-545-6
07-89
-13-26
1-253
147-3];
a)FindthedeterminantofA’*A,
b)FindtheinverseofA’*Aandcheckformatrixsingularity,
c)IfB=[5;1;-2;3;4;0],findthesolutionoftheequation.
Answer:
a)>>det(A'*A)
ans=
2.1051e+07
b)>>inv(A'*A)
ans=
0.08840.0321-0.0013-0.0219
0.03210.02310.0002-0.0099
-0.00130.00020.00850.0053
-0.0219-0.00990.00530.0144
>>cond(A'*A)
ans=
32.5278
c)>>X=A\B
X=
-0.8903
-0.4898
0.5755
0.7083
9.Dataof10recordsareshownbelow
y=[3.54.33.75.46.67.38.78.89.49.010.012.011.39.913.3],
Usepolyfitwithdifferentorders(from1to3)ofpolynomialstofindacurveofbestfit.Checkthetotaldistancebetweenthefittedcurvezandrecordsdefinedby
.
Answer:
m-file:
closeall
x=1:
1:
15;
y=[3.54.33.75.46.67.38.78.89.49.010.012.011.39.913.3];
p1=polyfit(x,y,1);z1=polyval(p1,x);
p2=polyfit(x,y,2);z2=polyval(p2,x);
p3=polyfit(x,y,3);z3=polyval(p3,x);
plot(x,y,'ro');holdon;
plot(x,z1,'b','LineWidth',2);
plot(x,z2,'m','LineWidth',2);
plot(x,z3,'k','LineWidth',2);
legend('point','polyfit1','polyfit2','polyfit3',2);
s1=sqrt(sum((z1-y).^2))
s2=sqrt(sum((z2-y).^2))
s3=sqrt(sum((z3-y).^2))
CommandWindow:
s1=
3.2807
s2=
3.0399
s3=
3.0398
10.Createasetof20pointsfromacurvebyMatlabcode:
x=1:
20;y=2*exp(-0.3*(x-5).^2)+0.7*exp(-0.2*(x-12).^2);
Theninterpolatethecurveto60pointsusing‘linear’and‘spline’options,respectively.Seethequalityofdifferenttypesofinterpolation.
Answer:
closeall
x=1:
20;y=2*exp(-0.3*(x-5).^2)+0.7*exp(-0.2*(x-12).^2);
xi=1:
1/3:
20;
y1=interp1(x,y,xi,'linear');
y2=interp1(x,y,xi,'spline');
plot(x,y,'k.');holdon;
plot(xi,y1,'r');
plot(xi,y2,'b');
legend('initial','linear','spline',1);
PartB
1.Usingtheplotandsubplotfunctionscreate4plotsona2by2arrayofsubplots,forthefunctionexp(-t)sin(5t)showingineachplotthefunctioninthecorrespondingintervalsofti.e.(-2,-1),(-1,0),(0,1)and(1,2).
Answer:
closeall;clearall;
t=-2:
0.01:
-1;
y=exp(-t).*sin(5*t);
subplot(221);plot(t,y);
t=-1:
0.01:
0;
subplot(222);plot(t,y);
t=0:
0.01:
1;
subplot(223);plot(t,y);
t=1:
0.01:
2;
subplot(224);plot(t,y);
2.Athreephaseinductionmotorcharacteristicisgivenintermsofmechanicalshaftoutputtorque
(NMNewton-meter)asafunctionofrotationalspeedω(rad/sradianpersecond).Thisisapproximatedby3piece-wiselinearequationsasfollows:
Thismotorisdirectlycoupledtoaload
whichcanberepresentedas
Write2separateMatlabfunctionm-filesinwhich:
a)themotorcharacteristic,b)theloadcharacteristicaredefinedonlyasfunctionsofω.Namethemmotor.mandsysload.m,respectively.
Answer:
function[Tm]=motor(w)
forn=1:
length(w)
ifw(n)>=0&&w(n)<=90*pi
Tm(n)=w(n)/(90*pi)+4.0;
elseifw(n)<110*pi
Tm(n)=95*w(n)/(20*pi)-422.5;
elseifw(n)<=120*pi
Tm(n)=-10*w(n)/pi+1200;
end
end
sysload.m:
function[Tl]=sysload(w)
Tl=-50*(w/(120*pi)).^3+100*(w/(120*pi)).^2+4*w./(120*pi);
3.WriteaMatlabscripgt-mfilewhichcallsyourfunctionm-filesfromQuestion2.Andplotthemotorandloadcharactersticsonthesamefigure,givingsuitablelabellingandtitle.Namethisscriptm-filesystemplot.m
Answer:
systemplot.m:
closeall;
w=0:
pi/3:
120*pi;
plot(w,motor(w),'r');holdon;
plot(w,sysload(w),'k');
legend('motor','sysload',2);
4.Writeasriptm-filewhichfindsallthemathematicallpossiblepoints(valuesofω)where
5.
fortherange0≤ω≤120π(rad/s)withthecharacteristicgiveninQuestion2.Thesystemcouldtheoreticallyoperateataspeedωwhere
Mathematically,thisinvolvesfindingtherootsoftheequation
Giveanametothism-fileposspoints.m.Callposspoints.minsystemplot.mandshowallofthepointsonasystemplotusingthe‘o’symbol.
Answer:
posspoints.m:
t=0:
0.01:
120*pi;
sub=motor(t)-sysload(t);
ansLoc=1;
forn=2:
length(sub)
p=sub(n);
q=sub(n-1);
ifp*q<0
xSet(ansLoc)=t(n);
ansLoc=ansLoc+1;
end
end
systemplot;holdon;
plot(xSet,motor(xSet),'o');
xSet
CommandWindow:
>>posspoint
xSet=
74.0200307.6800360.7900
PartC
IntroductiontoDSP
1.Basicdigitalsignals
Unitimpulsefunction
Exercise1-1:
DisplaytheunitimpulsefunctionwithMatlabcode.ThecodecanbeeithertypedunderMatlabpromptorwrittenintoascriptMatlabfile,thenrunthefile.
n=-10:
10;
fork=1:
21
x(k)=0;
end;
x(11)=1;
stem(n,x);
axis([-101002]);
Problem1-1:
Forthesignal
writetheMatlabcodeandcopytheresultfigureintothefollowingboxes.
n=-10:
10;
fork=1:
21
x(k)=0;
end;
x(9)=2;
stem(n,x);
axis([-101002]);
Unitstepfunction
Exercise1-2:
DisplaytheunitstepfunctionwithMatlabcode:
n=-10:
10;
fork=1:
10
x(k)=0;
end;
fork=11:
21
x(k)=1;
end;
stem(n,x);
axis([-101002]);
Problem1-2:
Forthesignal
writetheMatlabcode,anddisplaythecorrespondingsignalintothefollowingboxes.
n=-10:
10;
fork=1:
12
x(k)=0;
end;
fork=13:
21
x(k)=-1;
end;
stem(n,x);
axis([-1010-21]);
Sinewave
where-frequency(rad/second),T-samplinginterval(seconds)
Exercise1-3:
LetT=0.02,=6.28.Usingthefollowingcode,plotonthescreenintheregion0n100.
n=0:
100;
T=0.02;
omega=6.28;
fork=1:
101
x(k)=sin((k-1)*omega*T);
end;
stem(n,x);
Problem1-3:
Modifytheabovecodetodisplaythesignal:
whereT=0.02,=6.28.Executethecodeandplotthesignalonthescreen.Copythecode,thendisplaythesignalintothefollowingboxes.
n=0:
100;
T=0.02;
omega=6.28;
x=2*sin(n*omega*T+pi/2);
stem(n,x);
Timeshift,impulseandstepresponses
Exercise1-4
Fortheunitimpulse,
andtheunitstep
writetwoMatlabfunctionsandsaveas‘impseq.m’and‘stepseq.m’files,respectively.
%impseq.m
%UnitImpulse
%[n1:
n2]simplenumberrange
%n0-timeshift
function[x,n]=impseq(n0,n1,n2)
n=[n1:
n2];
x=[(n-n0)==0];
%end;
%stepseq.m
%UnitStep
%[n1:
n2]simplenumberrange
%n0-timeshift
function[x,n]=stepseq(n0,n1,n2)
n=[n1:
n2];
x=[(n-n0)>=0];
%end
Giventhefollowingdifferenceequation:
a)Calculateandplottheimpulseresponseatn=-20,…,120;and
b)Calculateandplotthestepresponseatn=-20,…,120,usingthefollowingcode
b=[1];a=[1,-0.7,0.9];
x=impseq(0,-20,120);