当前位置:编程学习 > Matlab >>

Assignment1 key to Introduction to MATLAB(MIT OPEN COURSEWARE)

shortProblem.m

%1. Scalar variables.
a=10;
b=2.5*10^23;
c=2+3i;
d=exp(i*2*pi/3);
%2. Vector variables.
aVec=[3.14 ,15, 9, 26];
bVec=[2.71; 8; 28; 182];
cVec=[5:-0.2:-5];
dVec=logspace(0,1,101);
eVec=['H' 'e' 'l' 'l' 'o'];
%3. Matrix variables.
aMat=ones(9,9)*2;
bMat=diag([1,2,3,4,5,4,3,2,1]);
cMat=reshape([1:1:100],10,10);
dMat=nan(3,4);
eMat=[13 -1 5;-22 10 -87];
fMat=randi([-3:3],3,5);
%4. Scalar equations.
x=1/(1+e^(-(a-15)/6));
y=(a^(0.5)+b^(1/21))^pi;
z=log(real((c+d)*(c-d))*sin(a*pi/3))/(c*conj(c));
%5. Vector equations.
xVec=1/(2*pi*2.5^2)^0.5*exp(-cVec.^2/(2*2.5^2));
yVec=((aVec').^2+bVec.^2).^0.5;
zVec=log10(1./dVec);
%6. Matrix equations.
xMat=(aVec*bVec)*aMat^2;
yMat=bVec*aVec;
zMat=det(cMat)*(aMat*bMat)';
%7. Common functions and indexing.
sum(cMat,1);
mean(eMat,2);
eMat(1,:)=[1 1 1];
cSub=cMat(2:9,2:9);
lin=[1:1:20];
lin(2:2:end).*=-1;
r=rand(1,5);
r(find(r<0.5));
r(find(r==0));

TwoLinePlot.m

 


figure;
t=[0:0.1:2*pi];
plot(t,sin(t));
hold on;
plot(t,cos(t),'--r');
xlabel('Time(s)');
ylabel('Function values');
title('Sin and Cos functions');
xlim([0,2*pi]);
ylim([-1.4 1.4]);
legend('Sin','Cos');

 


CalculateGrades.m

 


%9. Optinal: Manipulating Variables.
namesAndGrades=zeros(5,5);
load('classGrades.mat','namesAndGrades');
namesAndGrades(1:5,:)
grades=namesAndGrades(:,2:end);
mean(grades)
meanGrades=nanmean(grades)
meanMatrix=ones(size(grades,1),1)*meanGrades
curvedGrades=3.5*(grades./meanMatrix);
nanmean(curvedGrades)
curvedGrades(find(curvedGrades>5))=5;
totalGrade=ceil(nanmean(curvedGrades,2));
letters='FDCBA';
letterGrade=totalGrade;
for i=1:size(letters,2)
    letterGrade(find(letterGrade==i))=letters(i);
end
letterGrade=char(letterGrade')

 

 

 

ThrowBall.m

 


%11. Optional: Throwing a ball.
h=1.5;
g=9.8;
v=4;
angle=45;
t=[0:0.001:1];
x=v*cos(angle*pi/180)*t;
y=h+v*sin(angle*pi/180)*t-0.5*g*(t.^2);
X=x(find(y>0));
Y=y(find(y>0));
fprintf('The ball hits the ground at a distance of %f meters.\n',X(end));
figure;
plot(x,y,'-b');
hold on;
plot([0,max(x)],[0,0],'--k');
xlabel('Distance(m)');
ylabel('Ball Height(m)');
title('Ball Trajectory');

 


encrypt.m

 


%12. Optional: Write a simple shuffling 'encryption' algorithm
original='This is my top secret message!'
vector=randperm(length(original));
encoded=zeros(size(original));
for i=1:size(vector,2)
    encoded(i)=original(vector(1,i));
end
encoded=char(encoded)
temp=[vector',[1:1:length(original)]'];
sorted=sortrows(temp,1);
enc=sorted(:,2);
decoded=zeros(size(encoded));
for i=1:size(vector,2)
    decoded(i)=encoded(enc(i,1));
end
decoded=char(decoded)
fprintf('Decoded correctly (1 true, 0 false):%d\n',strcmp(num2str(original),char(decoded)));

 

seriesConvergence.m


%Optional: Convergence of infinite series.
p=0.99;
k=[0:1:1000];
geomSeries=p.^k;
G=1/(1-p)
plot([0,max(k)],[G,G],'-r');
hold on;
plot(k,cumsum(geomSeries),'-b');
xlabel('Sum');
ylabel('Index');
title('Convergence of geometric series with p=0.99');
legend('Infinite sum','Finite sum');
p=2;
n=[1:1:500];
pSeries=1./n.^p;
P=pi^2/6;
figure;
plot([0,max(n)],[P,P],'-r');
hold on;
plot(n,cumsum(pSeries),'-b');
xlabel('Index');
ylabel('Sum');
title('Convergence of p-series with p=2');
legend('Infinite sum','Finite sum');

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,