ps9solnsSp06

ps9solnsSp06 - %Engineering 6, Spring 2006, Problem 9.1...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
%Engineering 6, Spring 2006, Problem 9.1 Solution %(Stock price analysis) %Suppress extra lines in output format compact ; clc; %Clear command window and variables clear; %Generate a row vector x of 250 random values in a % Gaussian distribution with a mean of 75 and std. deviation % of 12. Set starting state to 30 (no significance to the choice of 30). randn( 'state' ,30); dailyPrice = 75 + 12*randn(1,250); %Define the variables and their initial values shares = 1000; totalCost = 0; totalFees = 0; fee = 0; totalIncome = 0; %Use a for loop to loop through each of the 250 stock prices %(representing the price on each of the 250 days). %Note: There are several ways to write this loop. We give another %version below. for k = 1:250 if dailyPrice(k) < 65 %Buy 100 shares shares = shares + 100; totalCost = totalCost + 100*dailyPrice(k); fee = 20; totalFees = totalFees + fee; %Note on fees: Because the problem statement was ambiguous regarding fees, %you could also calculate the fee as % totalFees = totalfees + 20 + 0.09*shares end if (dailyPrice(k) > 80) & (shares ~= 0) %Sell shares (as long as have some to sell) totalIncome = totalIncome + shares*dailyPrice(k); fee = 0.09*shares; %Calculate fee (greater of $20 or 0.09*shares), but see note below if fee >= 20 totalFees = totalFees + fee; else totalFees = totalFees + 20; end %Note on fees: Because the problem statement was ambiguous regarding fees, %you could also calculate the fee as % totalFees = totalfees + 20 + 0.09*shares shares = 0; %Reset shares owned to 0 (just sold them all)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
end end %Note: A slightly more elegant way to write the for loop is: %for k = dailyPrice % if k < 65 %Buy 100 shares
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

ps9solnsSp06 - %Engineering 6, Spring 2006, Problem 9.1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online