%Here's an overview of what you need to complete Assignment 1

%Creating an array of numbers going from 1 to 4 with a step of .3
dx = (4-1)/10; %.3 = (4-1)/10; we are subdividing [1..4] into 10 sub-intervals  
x = [1:0.3:4];

%The semicolon at the end of the previous line suppreses the output.
%If you remove the semicolon, Matlab will output the contents of x:
%[1.  1.3  1.6  1.9  2.2  2.5  2.8  3.1  3.4  3.7  4.]

%Declaring a function (you don't need to define any functions in
%this assignment, but you need to understand where the definition 
%is made)

f = @(x) x.^2;
%The function squares every element in x (the operation .^ works 
%on arrays of numbers instead of a single one)


%Applying function to an array.

y = f(x)
%Here, the output of y is shown because there was no semicolon 
%at the end of the line


%Plotting the values
plot(x,y)

%(note that this produces an inaccurate plot, since the step size dx=0.3
% is too large. To get a nice plot, use another array with step 0.01 or smaller.
% What you see in the figure is useful: the area under this broken line is
% the average of left and right Riemann sums. Can you see why?)


%Array indexing: getting parts of the array
xl = x(1:end-1) %get all elements of x but the LAST, and store them in xl
xr = x(2:end)   %all elements of x but the FIRST, and store them in xr

%Multiply array by a contant
f(xleft).*dx

%Sum values of an array 
sum(f(xleft).*dx)

%(the sum above is a left Riemann sum for f on the interval 1..4 with N=10)

%declaring an array manually
x = [1.  1.36  1.45  1.69  1.51  2.05  2.32  2.68  2.83  3.34  3.46  3.7  4.]  

%(this overwrites the data in x we had before)

%Getting an array of differences:
dx = diff(x)

%(now dx is not a single number, but an array containing consequtive differences in x values)

%Multiplying values in  two arrays element-by-element:
Aright = f(x(2:end)).*dx

%(now Aright contains the areas of rectangles in the right Riemman sum of f over this custom
%partition of [1..4]. Note that f(x).*dx would give an error, since dx=diff(x) contains
%one FEWER element han x.

RR = sum(Aright)

%The right Riemann sum is stored in RR.
