The price, in dollars, of a certain stock over a 10-day period is given in the f
ID: 1718221 • Letter: T
Question
The price, in dollars, of a certain stock over a 10-day period is given in the following array.
price = [19, 18, 22, 21, 25, 19, 17, 21, 27, 29]
Suppose you owned 1000 shares at the start of the 10-day period, and you bought 100 shares every day the price was below $20 and sold 100 shares every day the price was above $25. Use MATLAB to compute (a) the amount you spent in buying shares, (b) the amount you received from the sale of shares, (c) the total number of shares you own after the 10th day, and (d ) the net increase in the worth of your portfolio.
Explanation / Answer
The below script is the solution for the given problem. save the script in a notepad file ans save it with .m extension and place it iin the owrking directory of MATLAB and run it.
The script is as follows:
close all;clc;
% Displays the given stock prices for the 10 day period
A=[19 18 22 21 25 19 17 21 27 29];
disp('The price, in dollars, of a certain stock over a 10-day period is');
disp(A);
% Let us put all the stock prices whose value is less than $20 in an another array
B=[];
for i=1:10
if A(i)<20;
B(i)=A(i);
i=i+1;
end
end
% Let us put all the stck prices whose value is more than $25 in an another array
C=[];
for i=1:10
if A(i)>25
C(i)=A(i);
i=i+1;
end
end
% The amount spent in buying stocks is the starting stock price(1000 X $19) + stocks bought when price is less than $20(100 X sum of the prices in the array B)
a=1000*A(1)+100*sum(B);
a1=sprintf('The amount you spent in buying shares is $%d',a);
disp(a1);
% The amount received from selling the stocks is the product of sum of the array C with 100
b=100*sum(C);
b1=sprintf('The amount you received from the sale of shares is $%d',b);
disp(b1);
% nnz will give us the number of non zero elements in an array
% nnz(B) is the number of days the stocks are bought
% nnz(C) in the number of days the stocks are sold
b2=nnz(B);
c2=nnz(C);
% The below formula gives the number of shares at the end of the 10 day period
c=1000+100*(b2-c2);
c1=sprintf('The total number of shares you own after the 10th day are %d',c);
disp(c1);
% Change in the amount of the first and the last day will give us the net profit/loss of the portfolio
d=c*A(10)-1000*A(1);
d1=sprintf('The net increase in the worth of your portfolio is $%d',d);
disp(d1);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.