% Ch 4. Assignment #9 due 11/7
clear, clc
Date = date
Date = 07-Nov-2007
% Given: price of stock over a ten day period. price = [19, 18, 22, 21, 25, 19, 17, 21, 27, 29]; % Start with 1,000 shares. s_initial = 1000; % Condition 1: If price of stock < $20, then buy 100 shares. % Condition 2: If price of stock > $25, then sell 100 shares. % a. Find amount spent for condition 1. position_20 = find(price < 20); spent_on_shares = sum(100.*price(position_20)) % b. Find amount made for condition 2. position_25 = find(price > 25); made_on_shares = sum(100.*price(position_25)) % c. Find total number of shares (S_tot) owned after ten days. % s_tot = starting number + shares bought - shares sold. s_tot = s_initial + 100*length(position_20) - 100*length(position_25) % d. Find net increase in portfoloio (net). % net = (total shares owned on last day)*(worth of stock on last day) - ... % (total shares owned on first day)*(worth of stock on first day). net = s_tot*price(10) - s_initial*price(1)
spent_on_shares =
7300
made_on_shares =
5600
s_tot =
1200
net =
15800
% Use example values x, y, z. x = 2, y = 5, z = 9 if (x < y) & (z < 10) w = x*y*z end
x =
2
y =
5
z =
9
w =
90
% Purpose of program: to determine whether a given year is a leap year. % extra_day = 1 means year is a leap year, extra_day = 0 means it is not. % Use example year values given in text. years = [1800, 1900, 2100, 2300, 2500, 2400]; for position = 1:1:6 year = years(position) % year is evenly divisible by 400. if (mod(years(position),400) == 0) extra_day = 1 % year is evenly divisible by 100 but not by 400. elseif (mod(years(position),100) == 0) extra_day = 0 % year is evenly divisible by 4 but not by 100 or 400. elseif (mod(years(position),4) == 0) extra_day = 1 % year does not satisfy any of the three conditions. else extra_day = 0 end % (mod(years(position),400) == 0) end % position = 1:1:6
year =
1800
extra_day =
0
year =
1900
extra_day =
0
year =
2100
extra_day =
0
year =
2300
extra_day =
0
year =
2500
extra_day =
0
year =
2400
extra_day =
1
% Given: position function p(t) = (x(t), y(t)) of an object, where % x(t) = 5*t - 10, % y(t) = 25*t^2 - 120*t + 144, and % the interval (I) is (0 <= t <= 4), i.e. for t I = [0,4]. % Find: the time and the distance at which the position of the object is % closest to the origin (0,0). % Distance formula = ((x_1 - x_2)^2 + (y_1 - y_2)^2)^(1/2), where % (x_2, y_2) = (0, 0) i.e. the origin, and % (x_1, y_1) = (x(t), y(t)) i.e. the position of the object. % Therefore the minimum distance is computed as follows: % min((x(t)^2 + y(t)^2)^(1/2)) % a. use a loop to find the solution. % set innitial value for minima so it is defined. minima = 100000; % for (loop variable) = m:s:n - from m to n, with an incrament of s. for t = 0:.01:4 x = 5*t - 10; y = 25*t^2 - 120*t + 144; dis = (x^2 + y^2)^(1/2); if dis < minima time = t; minima = dis; end % dis < minima end % t = 0:.01:4 Time_at_min_distance_a = time Minimum_distance_a = minima % b. do not use a loop to find the solution. t = [0:.01:4]; x = 5.*t - 10; y = 25.*t.^2 - 120.*t + 144; [Minimum, position] = min((x.^2 + y.^2).^(1/2)); Time_at_min_distance_b = t(position) Minimum_distance_b = Minimum
Time_at_min_distance_a =
2.2300
Minimum_distance_a =
1.3581
Time_at_min_distance_b =
2.2300
Minimum_distance_b =
1.3581
% Given: array A A = [3, 5, -4; -8, -1, 33; -17, 6, -9]; % Find: array B with elements subject to two conditions. % 1. ln(X), where X are the elements of A >= 1. % 2. X + 20, where X are the elements of A >= 1. % a. use a for loop with conditional statements. for m = (1:size(A,1)) for n = (1:size(A,2)) if (A(m,n) >= 1) B(m,n) = log(A(m,n)) + 20; else B(m,n) = A(m,n); end % (A >= 1) end % n = (1:size(A,2)) end % m = (1:size(A,1)) B % b. use a logical array as a mask. C = (A >= 1); A(C) = log(A(C)) + 20
B = 21.0986 21.6094 -4.0000 -8.0000 -1.0000 23.4965 -17.0000 21.7918 -9.0000 A = 21.0986 21.6094 -4.0000 -8.0000 -1.0000 23.4965 -17.0000 21.7918 -9.0000