%% Michael Olinick     March 1, 2022       Intro to MATLAB

% Exercise 1
2^3 - (3 - 6)/2 + 7* .5
(3^2 - 2*3 + 4)/(2 + 7)
(3 + 4.5)/(7^2 - 2*9) - ( 2 * 1.5 - 1)/(3 - 6.2)

%Now try Part 4
( (sqrt(7) - sqrt(6))/4)^2

a = 2^3 - (3 - 6)/2 + 7* .5
b = (3^2 - 2*3 + 4)/(2 + 7)
c = (3 + 4.5)/(7^2 - 2*9) - ( 2 * 1.5 - 1)/(3 - 6.2)
d = ( (sqrt(7) - sqrt(6))/4)^2

%Exercise  2. You can use Copy and Paste to avoid retyping
(a + b)/(c + d^2)
% Exercise 3
rowVec = [1.01, 17, pi, 1/2 ]
colVec = [ 3/4; 15.8; 3; -5]
matrix = [ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12; 13 14 15 16 ]

% Exercise 4
matrix^2
rowVec + colVec
%That's wrong; rowVec and colVec are not of the same dimensions
matrix + colVec

%also an error
rowVec + matrix
% not right either; dimensions do not match
matrix * colVec
% Good. We can multiply the matrix and the column vector but not add them.
rowVec * matrix
% Looks right. Is it?
rowVec * colVec

% Note that MALAB computed the dot product of these two vectors.

% Exercise 6
sin(pi)
sqrt(1 - (cos(pi))^2)
sin(pi) -sqrt(1 - (cos(pi))^2)

% GOOD QUESTION TO THINK ABOUT: WHY DIDN'T MATLAB GET SIGN PI AS 0?

atan(1/sqrt(3))- atan(sqrt(3)/3)

% Again, we should get 0. Let's try looking st 1/sqqrt(3) vs sqrt(3)/3
1/sqrt(3)

sqrt(3)/3

1/sqrt(3) - sqrt(3)/3

% Weird. Numbers look the same but their difference is not exactly 0 according to MATLAB

% Exercise 6
sym(sin(pi))
sym( sqrt(1 - ( cos(pi)^2 )))
sym(1/sqrt(3) - sqrt(3)/3)

% Exercise 7
f = @(x) x^2 - log(x)
f(1)
f(0)
f( exp(1) )
f(pi)

% Exercise 8
f = @(x) x.^2 + log(x)
X = 1:100;
f(X)


% Exercise 9
Xdata = 1:100;
Ydata = f(X);
plot(Xdata,Ydata)
% Figure will appear on screen. If you don't see one, go to Window at top of screen, drop down menu and select Figure 1
help plot;

plot(Xdata, Ydata, 'ro')
% Needed to look under Window to see result

close all
plot(Xdata, Ydata)

plot(Xdata, Ydata, 'ro')

% Exercise 10
% First Task: Create an array of 100 evenly spaced points between 0 and 2 pi
g = @(x) (x .* 2 .* pi .* .01)

NewXdata = g(Xdata);
Ydata = sin(NewXdata);
plot(NewXdata, Ydata)
% That looks better
close all
Zdata = cos(NewXdata);
plot(NewXdata, Ydata, NewXdata, Zdata)
% You can use linspace command to create an interval from a to b with c evenly spaced points: linespace(a,b,c).

NewXdata = linspace(0, 2*pi, 101);
Ydata = sin(NewXdata);
Zdata = cos(NewXdata);
plot( NewXdata, Ydata, NewXdata, Zdata)