MATLAB作业 下载本文

第一次作业

1.shortProblems %标量变量生成 a=10

b=2.5*(10^23) c=2+3*i

d=exp(j*2*pi/3)

%向量生成

aVec=[3.14 15 9 26] bVec=[2.71 8 28 182]' cVec=5:-0.2:-5

dVec=logspace(0,1,101) eVec='Hello'

%矩阵变量

aMat=2*ones(9,9) B=[1 2 3 4 5 4 3 2 1]; bMat=diag(B) C=[1:100];

cMat=reshape(C,10,10) dMat=NaN*ones(3,3) E=[13 -22 -1 10 5 -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))^pi

z=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*aVec

zMat=det(cMat)*((aMat*bMat)')

%一般运算及元素索引

cSum=sum(cMat) eMean=mean(eMat,2) eMat(1,:)=[1 1 1] cSub=cMat(2:9,2:9) lin=1:20;

lin(2:2:end)=-lin(2:2:end) r=rand(1,5) r(find(r<0.5))=0

2.twoLinePlot figure;

t=0:pi/100:2*pi; plot(t,sin(t)); hold on;

plot(t,cos(t),'r:'); xlabel('Time(s)');

ylabel('Function value'); title('Sin and Cos');

legend('sin','cos','location','NorthEast'); xlim([0 2*pi]); ylim([-1.4 1.4]);

3.calculateGrades

R=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(curvedGrades>5))=5; totalGrade=(ceil(nanmean(curvedGrades')))' letters='FDCBA';

for k=1:length(totalGrades) switch totalGrades(k) case 1

Grades(k)=letters(1); case 2

Grades(k)=letters(2); case 3

Grades(k)=letters(3); case 4

Grades(k)=letters(4);

case 5

Grades(k)=letters(5); end end

disp(['Grades:',letterGrades]);

4.encrypt

original='This is my top secret message!'; 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(['Decoded correctly (1 true;0 false): ' num2str(correct)]);

5.throwBall clear all; 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'); hold on;

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),'处撞到地面']); hold on;

plot([0 max(x_t)],[0 0],'k--');