您好,欢迎访问三七文档
第一次作业1.shortProblems%标量变量生成a=10b=2.5*(10^23)c=2+3*id=exp(j*2*pi/3)%向量生成aVec=[3.1415926]bVec=[2.71828182]'cVec=5:-0.2:-5dVec=logspace(0,1,101)eVec='Hello'%矩阵变量aMat=2*ones(9,9)B=[123454321];bMat=diag(B)C=[1:100];cMat=reshape(C,10,10)dMat=NaN*ones(3,3)E=[13-22-1105-87];eMat=reshape(E,2,3)F=-3+fix(3*rand(1,15));fMat=reshape(F,5,3)%标量方程x=1/(1+exp(-(a-15)/6))y=(sqrt(a)+b^(1/21))^piz=log(real((c+d)*(c-d))*sin(a*pi/3))/(c*conj(c))%向量方程计算xVec=1/(2.*pi.*(2.5.^2))*exp((-cVec.^2)./(2.*(2.5.^2)))yVec=sqrt((aVec'.^2)+bVec.^2)zVec=log10(1./dVec)%矩阵运算xMat=(aVec*bVec)*(aMat^2)yMat=bVec*aVeczMat=det(cMat)*((aMat*bMat)')%一般运算及元素索引cSum=sum(cMat)eMean=mean(eMat,2)eMat(1,:)=[111]cSub=cMat(2:9,2:9)lin=1:20;lin(2:2:end)=-lin(2:2:end)r=rand(1,5)r(find(r0.5))=02.twoLinePlotfigure;t=0:pi/100:2*pi;plot(t,sin(t));holdon;plot(t,cos(t),'r:');xlabel('Time(s)');ylabel('Functionvalue');title('SinandCos');legend('sin','cos','location','NorthEast');xlim([02*pi]);ylim([-1.41.4]);3.calculateGradesR=xlsread('testdata.xls');ans=R(1:5,:)grades=R(:,2:end);ans=mean(grades)meanGrades=nanmean(grades)meanMatrix=repmat(meanGrades,size(grades,1),1)curvedGrades=3.5*(grades./meanMatrix);ans=nanmean(curvedGrades)curvedGrades(find(curvedGrades5))=5;totalGrade=(ceil(nanmean(curvedGrades')))'letters='FDCBA';fork=1:length(totalGrades)switchtotalGrades(k)case1Grades(k)=letters(1);case2Grades(k)=letters(2);case3Grades(k)=letters(3);case4Grades(k)=letters(4);case5Grades(k)=letters(5);endenddisp(['Grades:',letterGrades]);4.encryptoriginal='Thisismytopsecretmessage!';encodeouter=randperm(length(original));encoded=original(encodeouter);temp=[encodeouter;1:length(original)]';temp=sortrows(temp)decodeouter=temp(:,2);decoded=encoded(decodeouter);disp(['Original:'original]);disp(['Encoded:'encoded]);disp(['Decoded:'decoded]);correct=strcmp(original,decoded);disp(['Decodedcorrectly(1true;0false):'num2str(correct)]);5.throwBallclearall;h=1.5;g=9.8;v=4;a=45;t=linspace(0,1,1000);x_t=v*cos(a*pi/180)*t;plot(t,x_t,'r');holdon;t=linspace(0,1,1000);y_t=h+v*sin(a*pi/180)*t-1/2*g*t.^2;plot(t,y_t);t0=min(find(y_t=0));x0=x_t(t0);disp(x0);xlabel('时间轴');ylabel('函数');title('抛球模型');legend('x(t)','y(t)');disp(['小球在',num2str(x0),'处撞到地面']);holdon;plot([0max(x_t)],[00],'k--');第二次作业1.semilogplot:x=1:5;y=[152555115144];h=semilogy(x,y,'--rs','MarkerFaceColor','g','MarkerEdgeColor','k','LineWidth',2);xlabel('years');ylabel('numberofstudents');title('growthtrend');xlim([06]);2.bargraph:y=rand(5,1);bar(y,'r');x=0:0.1:1;title('Bargraphof5randomvalues');3.插入和面绘制:z0=rand(5);[x0,y0]=meshgrid(1:5,1:5);[x1,y1]=meshgrid(1:0.1:5,1:0.1:5);z1=interp2(x0,y0,z0,x1,y1);surf(z1);colormap(hsv);shadinginterp;holdon;contour(z1);%caxis([01]);colorbar;4.使用find:functionind=findNearest(x,desiredVal)[m,n]=size(x);A=abs(x-desiredVal);min_a=min(A);B=reshape(A,m,n);[ind]=find(B==min_a)x=input('inputx:')desiredVal=input('inputdesiredVal:')ind=findNearest(x,desiredVal);5.循环和流程控制functionloopTest(n)forn=1:nif(mod(n,2)==0)&(mod(n,3)==0)disp([num2str(n),'isdivisibleby2AND3'])elseifmod(n,2)==0disp([num2str(n),'isdivisibleby2'])elseifmod(n,3)==0disp([num2str(n),'isdivisibleby3'])elsedisp([num2str(n),'isNOTdivisibleby2AND3'])endendN=input('inputn:');loopTest(n);6.平滑器:functionsmoothed=rectFilt(x,width)ifrem(width,2)==0;width=width+1;disp('widthmustbeodd!');endforn=1:length(x);ifn(width+1)/2;smoothed(n)=mean(x(n:2*n-1));elseifnlength(x)-(width-1)/2;smoothed(n)=mean(x(n-4:n));elsesmoothed(n)=mean(x(n-(width-1)/2:n+(width-1)/2));endendx=xlsread('noisyData.xls');width=11;smoothed=rectFilt(x,width);plot(x,'b--','Linewidth',2.5);holdon;plot(smoothed,'r-','Linewidth',2.5);xlabel('lndex');ylabel('Datavalue');title('SmoothingDemonstration');legend('OriginalData','Smoothed');7.绘制圆的函数color=['b','k','g','y','r'];r=[1,4,7,10,13];center=[0,0];fori=1:5[x,y]=getCircle(center,r(i));plot(x,y,color(i),'Linewidth',5);holdonendaxis([-1616-1313]);function[x,y]=getCircle(center,r)t=linspace(-2*pi,2*pi,5000);x=cos(t).*r+center(1);y=sin(t).*r+center(2);color=['b','k','g','y','r'];center=[-10;00;10;-0.5-0.5;0.5-0.5];fori=1:5[x,y]=getCircle(center(i,:),0.5);plot(x,y,color(i),'Linewidth',5);holdonendset(gca,'xtick',[-1.5:0.5:1.5]);set(gca,'ytick',[-1.4:0.2:1.0]);axis([-1.51.5-1.41.0]);第三次作业1.线性方程求解:A=[3,6,4;1,5,0;0,7,7];B=[1;2;3];X=A\B2.数值积分:x=0:0.001:5;y=exp(-x./3).*x;trapz(x,y)3.矩阵求逆:A=[1,2;3,4];B=inv(A);C=A*B4.多项式拟合:x=xlsread('randomDatax.xls');y=xlsread('randomDatay.xls');plot(x,y,'.k');holdon;a='bgrym';forn=1:5x1=x;y1=y;[p,S,mu]=polyfit(x1,y1,n);[y1,delta]=polyval(p,x1,S,mu);plot(x1,y1,a(n),'linewidth',2);holdon;endxlabel('x');ylabel('y');title('Plynomialfitstorandomdata');legend('Original','Order1','Order2','Order3','Order4','Order5',4);5.神经脉冲传导过程Hodgkin-Huxley方程仿真functiondy=HHODEfun(t,y)%n,m,h,vC=1;Gk=36;Gna=120;Gl=0.3;Ek=-72;Ena=55;El=-49.4;an=alphan(y(4));bn=betan(y(4));am=alpham(y(4));bm=betam(y(4));ah=alphah(y(4));bh=betah(y(4));dy=zeros(4,1);dy(1)=(1-y(1))*an-y(1)*bn;dy(2)=(1-y(2))*am-y(2)*bm;dy(3)=(1-y(3))*ah-y(3)*bh;dy(4)=-1/C*(Gk*y(1)^4*(y
本文标题:MATLAB作业
链接地址:https://www.777doc.com/doc-4027586 .html