%% Annotated Matlab transcript -- 1/23 %% CS 323 - Doug DeCarlo % operations on vectors... >> a = 1:5 % scalar multiplication >> 2*a % addition of a scalar to each dimension >> a+5 % * is for multiplying matrices, so this doesn't work here (error) >> a*a % use .* for termwise multiplication >> a.*a % same for exponentiation -- this squares a matrix (but gets an error) >> a^2 % termwise exponentiation >> a.^2 >> a.^3 % For matrix multiplication: this uses ' (transpose) % so that this is just the inner/dot product >> a * a' % and the outer product >> a' * a % you can also apply functions to each term >> exp(a) % Now make a domain for graphing in [0,1] (points separated by 0.01) >> x = 0:0.01:1; % graph example: plot the line y = 2x+5 >> plot(x,2*x+5) % Taylor series example for e^x around x=0, n=1 % plot e^x in red, and its linear taylor polynomial in green % (notice how they intersect at x=0 and have the same tangent..) >> plot(x,exp(x),'r', x,1+x,'g') % the error >> plot(x,exp(x)-(1+x)) % the upper bound on error from the taylor error >> plot(x,x.^2/2.*exp(x)) % the actual error and the upper and lower bounds from the taylor error >> plot(x,exp(x)-(1+x),'r',x,x.^2/2.*exp(x),'b',x,x.^2/2,'b') % let's see how this error varies with the number of terms >> n = 1:10; >> plot(n,exp(1)./factorial(n+1)) % let's get a closer look for the example in lecture >> n = 7:10; >> plot(n,exp(1)./factorial(n+1))