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

Assignment2 key to Introduction to MATLAB(MIT OPEN COURSEWARE)

tradeShock.m

 


function endValue=tradeStock(initialInvestment, price, buy, sell)
price=zeros(1,1);
peaks=zeros(1,1);
lows=zeros(1,1);
load('googlePrices.mat','price','peaks','lows');
figure;
x=[1:1:size(price,1)];
plot(x,price,'y-');
hold on;
plot(x(peaks),price(peaks),'r.');
hold on;
plot(x(lows),price(lows),'b.');
initialInvestment=100000;
cash=initialInvestment;
share=0;
tradeTimes=size(peaks,1)+size(lows,1);
pInd=1;
lInd=1;
for i=1:tradeTimes
    if (peaks(pInd,1)<lows(lInd,1))
        %sell
        p=price(peaks(pInd,1),1);
        cash+=p*share;
        share=0;
        fprintf("Sell%d\tCash:%f\tShare value:%f\n",peaks(pInd,1),cash,share*p);
        if(pInd<size(peaks,1))
            pInd++;
        end;
        cash-=12.95;
    else
        %buy
        p=price(lows(lInd,1),1);
        sshare=ceil((double(cash))/p)-1;
        share+=sshare;
        cash-=sshare*p;
        fprintf("Buy%d\tCash:%f\tShare value:%f\n",lows(lInd,1),cash,share*p);
        if(lInd<size(lows,1))
            lInd++;
        end
        cash-=12.95;
    end
end
endValue=cash+share*price(end,1);
end

 


throwBall.m

 


function distance=throwBall(v,theta)
h=1.5;
g=9.8;
v=4;
t=[0:0.001:10];
x=v.*cos(theta*pi/180)*t;
y=h+v.*sin(theta*pi/180)*t-0.5*g*(t.^2);
X=x(find(y>0));
Y=y(find(y>0));
if(x(end)==0)
    fprintf('The ball does not hit the ground in 10 seconds\n');
    distance=NaN;
else
    distance=X(end);
end
end

 


semilog.m

Y1=[15,25,55,115,144];
figure;
semilogy(Y1,'Color','m','LineWidth',4,'Marker','s','MarkerSize',10,'LineStyle','none');
figure;
Y2=random('norm',5,2,5,1)
bar(Y2,'r');

 


RandomSurface.m

 

 

Z0=rand(5,5);
[X0,Y0]=meshgrid(1:5,1:5);
[X1,Y1]=meshgrid(1:.1:5,1:.1:5);
Z1=interp2(X0,Y0,Z0,X1,Y1);
figure;
surf(Z1);
colormap hsv;
shading interp;
hold on;
contour(Z1,15);
zlim([-0.2,1.2]);
colorbar;
caxis([0 1]);

 

 

Olympic.m

 


function []=olympic()
figure;
colormap(jet);
[x,y]=getCircle([-1 0],0.4);
plot(x,y,'LineWidth',10,'Color','b');
hold on;
[x,y]=getCircle([0 0],0.4);
plot(x,y,'LineWidth',10,'Color','k');
hold on;
[x,y]=getCircle([1 0],0.4);
plot(x,y,'LineWidth',10,'Color','r');
hold on;
[x,y]=getCircle([-0.5 -0.5],0.4);
plot(x,y,'LineWidth',10,'Color','y');
hold on;
[x,y]=getCircle([0.5 -0.5],0.4);
plot(x,y,'LineWidth',10,'Color','g');
hold on;
xlim([-2,2]);
ylim([-2,2]);
end


mysmooth.m

 


x=zeros(1,1);
width=2;
load('optionalData.mat','x');
figure;
plot(x(:,1),x(:,2),'.');
y=zeros(size(x(:,1)));
for i=1:size(x,1)
    ind=find(x(:,1)>(x(i,1)-width/2) & x(:,1)<(x(i,1)+width/2))
    %y(i,1)=interp1(x(ind,1),x(ind,2),x(i,1));
    size(ind,1)
    y(i,1)=sum(x(ind,2))/size(ind,1);
end
hold on;
[x(:,2),y]
plot(x(:,1),y,'r-');

 


getCircle.m

 


function [x,y]=getCircle(center,r)
t=(0:2*pi/100:2*pi);
x=r*cos(t)+center(1,1);
y=r*sin(t)+center(1,2);
end

 

fun.m

 


function []=fun()
%findNearest([4,5,6;1,2,3;7,8,9],6.5)
%loopTest(6);
x=zeros(1,1);
load('noisyData.mat','x');
smoothed=rectFilt(x,11);
figure;
plot(x,'LineStyle','none','Color','m','Marker','.');
hold on;
plot(smoothed);
end
function [ind]=findNearest(x, desiredVal)
absMtx=abs(x.-desiredVal);
ind=find(absMtx==min(absMtx(:)));
end
function []=loopTest(N)
for n=1:N
    if (mod(n,2)==0 && mod(n,3)==0)
        fprintf('%d is divisible by 2 AND 3\n',n);
    elseif (mod(n,2)~=0 && mod(n,3)==0)
        fprintf('%d is divisible by 3\n',n);
    elseif (mod(n,2)==0 && mod(n,3)~=0)
        fprintf('%d is divisible by 2\n',n);
    elseif (mod(n,2)~=0 && mod(n,3)~=0)
        fprintf('%d is NOT divisible by 2 or 3\n',n);
    end
end
end
function smoothed=rectFilt(x,width)
if(mod(width,2)==0)
    width++;
end;
smoothed=conv(x,1/width*ones(width,1),'same');
end

 

concentric.m

 


function []=fun()
%findNearest([4,5,6;1,2,3;7,8,9],6.5)
%loopTest(6);
x=zeros(1,1);
load('noisyData.mat','x');
smoothed=rectFilt(x,11);
figure;
plot(x,'LineStyle','none','Color','m','Marker','.');
hold on;
plot(smoothed);
end
function [ind]=findNearest(x, desiredVal)
absMtx=abs(x.-desiredVal);
ind=find(absMtx==min(absMtx(:)));
end
function []=loopTest(N)
for n=1:N
    if (mod(n,2)==0 && mod(n,3)==0)
        fprintf('%d is divisible by 2 AND 3\n',n);
    elseif (mod(n,2)~=0 && mod(n,3)==0)
        fprintf('%d is divisible by 3\n',n);
    elseif (mod(n,2)==0 && mod(n,3)~=0)
        fprintf('%d is divisible by 2\n',n);
    elseif (mod(n,2)~=0 && mod(n,3)~=0)
        fprintf('%d is NOT divisible by 2 or 3\n',n);
    end
end
end
function smoothed=rectFilt(x,width)
if(mod(width,2)==0)
    width++;
end;
smoothed=conv(x,1/width*ones(width,1),'same');
end

 


 

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