how to solve this using the for loop and if
Show older comments
the price, in dollars, of a certain stock over a 10 day period is given in 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 everyday the price was below $20 and sold 100 shares every day the price was above $25.
compute:
1-the amount you spent in buying share
2-the amount you received in selling shares
3-total number of shares you own after the 10th day
4-the net increase in the worth of your portfolio
x=[19,18,22,21,25,19,17,21,27,29]
p=0;
for i=1:10;
if x(i)<20;
p=p+(100*(x(i)));
end
U=sum(p)
end
k=0;
for i=1:10;
if x(i)>25
k=k+(100*x(i))
end
end
total=1000
1 Comment
Walter Roberson
on 4 May 2013
What problem are you observing?
Why are you overwriting "U" during each iteration of the first "for" loop ?
Answers (0)
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!