%% MATLAB Bootcamp - Homework 2
% k. drushka
% sept 2013


%% 1. Use Matlab to calculate the volume of a cylinder having height 10cm  
% and a radius of 1cm. What about a radius of 3cm? What about 5cm and 9cm? 
% (Hint: create a vector for the radius that contains all values of the radius,
% and use that variable in an equation for the volume of a cylinder). 

% volume of a cylinder = pi*r^2 * height
radius=1:2:9;  % or  radius=[1 3 5 9]; would do the same
height=10;     % height = 10cm
vol=pi*radius.^2*height   % leave the semicolon off so the answers print to the screen


%% 2. Use Matlab to calculate the value of 76-(37-7e^(5+cos?(?/3) )). Gi 
% (Hint: be careful with the parentheses). Give the answer to 8 decimal places 
% (e.g use the command format).
format longg                  % long format without scientific notation
76-(37 - 7*exp(5+cos(pi/3)))  % leave the semicolon off so the answers print to the screen
% the answer is 1751.84352584954

%% 3. Plot the function y=10sin?(x)/x , where x is a vector of length 1000 that 
% ranges from -100 to 100 (Hint, try the linspace command).
x=linspace(-100,100,1000);
y=10*sin(x)./x;  
figure(1),clf
plot(x,y,'k-');

%% 4: Add "random noise" to the vector y from question 3:
% First, use the function randn to create a vector (of the same size as y) 
% of normally-distributed random values; this vector is the "noise".
noise=randn(size(y));   % creates a normally-distributed vector the same size as y 
% Add the "noise" vector to y, creating a new variable called ynoisy.
ynoisy=y+noise;
% Without clearing the figure from question 3, plot ynoisy against x
%  using a different color and pattern.
hold on    % keeps the current plot from being overwritten
plot(x,ynoisy,'r--');

%% 5. 
% a)Load hw2.mat into your workspace.  
load('~/Desktop/matlab-class/hw2.mat')  % * either specify the full directory path *or* 
                                        % make sure that Matlab is running
                                        % in the directory where hw2.mat is
                                        % stored
% b) b.	Find the (row,column) of all elements in K that are greater than 3. 
% (i.e. use find with two outputs). 
[rowind colind]=find(K>3); 
% c)	Find the linear index of all elements in K that are greater than 3. 
% (i.e. use find with only one output).  
ind=find(K>3);
% d)	Using the index found in part c, set all elements in K that are greater than 3 to 10. 
K(ind)=10;
% e)	Calculate the product of each element of K and each element of P 
% (Hint, include a period (.) before the * to do element-by-element multiplication). 
KP=K.*P;   % "KP" is a new variable
% f. Find all elements for which the product of K and P is negative, 
% and set the index of those elements to zero in both K and P.
ni=find(KP<0);  % find the index of the negative values in KP
K(ni)=0;        % set the 
P(ni)=0;


%% 6. 
% a)Create a scalar called p, which can have any value you like. 
% Use Matlab to evaluate the expression f=2p^(-2)+(7p-5)/(3p+1)+ln?(p/3)
p=19;
f=2*p^(-1)+(7*p-5)/(3*p+1)+log(p/3)  % no semi-colon - outputs the answer to the screen
% b) Now, make p a vector of any length >1, and evaluate the expression again. 
% Note that you will have to include periods (.) in the equation so that it
% correctly performs element-by-element math.
p=1:20;
f=2*p.^(-1)+(7*p-5)./(3*p+1)+log(p/3)