%Tutorial 1 - CBE 20258 Computer Methods echo on clc; clear; %The following two commands are used to import %data from excel spreadsheet. This is analogous %to the procedure explained in the online tutorial %Importing data from excel spreadsheets... sp500=xlsread('sp500.xls',1,'','basic'); sp500e=xlsread('sp500_energy.xls',1,'','basic'); pause; %Note that if the file was in the appropriate directory (workspace) %you could have double-clicked on it. The "clear" command would %have wiped it out, though, so it often makes sense to explicitly %load the file too. pause; %The subplot command creates a set of plots in the same figure %Subplot(1,2,1) means the figure has 1 row and 2 columns of plots. %The third argument means that the first plot will be specified figure(1) subplot(1,2,1) plot(sp500(:,1),sp500(:,2)); %Datetick command writes the X axis in Date format. %There are 17 formats available, you can take a look at them by %typing 'help datetick' on the command window. datetick('x',12); xlabel('Date') ylabel('S&P 500 Fund Price (USD)') %Now the second plot will be specified subplot(1,2,2) plot(sp500e(:,1),sp500e(:,2)); datetick('x',12); xlabel('Date') ylabel('S&P 500 Energy Fund Price (USD)') pause; %Concatenate two arrays: %The first column has the dates %The second column the S&P 500 data %The third column has the S&P Energy 500 data funds = [sp500(:,1), sp500(:,2), sp500e(:,2)]; %Plot this in a different figure figure(2) plot(funds(:,1),funds(:,2),funds(:,1),funds(:,3)); datetick('x',12); xlabel('Date') ylabel('Fund Price(USD)') legend('S&P 500','S&P 500 Energy') pause; %Extract the first 250 hundred days data from S&P first250=[1:250]; first_250_days_sp500 = [sp500(first250,:)]; %Plot them..." figure(3) plot(first_250_days_sp500(:,1), first_250_days_sp500(:,2)) datetick('x',12); title('First 250 days of the S&P 500 Fund price') xlabel('Date') ylabel('Fund Price (USD)') pause; %Save them into the file "first_days.mat" save('first_days','first_250_days_sp500') %Load the first 250 days data back again load('first_days.mat'); pause; %We can use the data we just extracted to figure out when the minimum %and maximum prices of this stock occurred. %The max function returns two results, the maximum number found in a vector %and its index within this vector. The max function can also be used in %the following way %max(vector) %This would only return the maximum value, no index. Type help max for more %info [max_price,index_max] = max(first_250_days_sp500(:,2)); [min_price,index_min] = min(first_250_days_sp500(:,2)); difference = max_price - min_price; yield = difference*100/min_price; %Datestr works similarly to datetick. Type help datestr for more info. when_to_buy = datestr(first_250_days_sp500(index_min,1),2); when_to_sell = datestr(first_250_days_sp500(index_max,1),2); echo off %Display data in the command line text_string=['The maximum price of the S&P fund was ', num2str(max_price), ' USD']; disp(text_string) text_string=['The minimum price of the S&P fund was ', num2str(min_price), ' USD']; disp(text_string) text_string=['The optimum buying time is ', when_to_buy,'. The optimum selling time is ', when_to_sell]; disp(text_string) text_string=['The inversion yield is ',num2str(yield),'%']; disp(text_string)