% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  MATLAB TUTORIAL  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %  This is a tutorial with code examples to give an introduction to the
% %  Matlab programming environment. We will address several topics,
% %  including:
% %
% %   1) The Matlab programming environment.
% %   2) Variable initialization.
% %   3) Basic programming.
% %   4) Mathematics.
% %   5) Plotting.
% %   6) Solving differential equations.
% %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% By Doug Rubino
% Edited by Jeffrey Bush
%
% %_______________________________________________________________________%%
% 
% 
% 
% 
% %_______________________________________________________________________%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  SECTION 1  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% % THE MATLAB PROGRAMMING ENVIRONMENT
% 
% % 1) Screen Setup.
% 
% % On the left hand side of the screen there should be two
% % windows. One allows you to toggle between the workspace and the current
% % directory. Set this to the workspace. Below, there should be a command
% % history window. If either of these are not visible. Go to the tabs at
% % the top, select 'Window', and open the appropriate windows.
% 
% % 2) Current Directory.
% 
% % Above the main screen is the 'current directory' bar.
% % Make a directory for your classwork, and then make that your current
% % directory.
% 
% % 3) The Path
% 
% % Go to: File -> Set Path
% 
% % Add your current directory to the path. Also add any class folders to
% % the path. If there are subfolders, remember to 'Add with subfolders'.
% 
% % 4) The command line.
% 
% % Type in the command line:
% 
%   x = 2/3;
%   disp(int2str(x));
%   disp(num2str(x,3));
% 
% % A variable should appear in your workspace. The value should be
% % displayed in two different ways below the command line.
% 
% % 5) Scripts
% 
% % Anything that can be typed into the command line can be stored as a
% % sequence of commands in a script. Type in the command line:
% 
%   edit garbage;
% 
% % Copy + Paste the three lines of code above into the m-file editor
% % window. Save the file. Type in the command line:
% 
%   garbage;
% 
% % You should see the same output as you did in the command line section.
% 
% % 6) Functions
% 
% % In Matlab, one generally writes libraries of short, specific functions.
% % Here's a template for a function. Type in the command line:
% 
%   edit testfun;
% 
% % In the m-file editor, type:
% 
%   function y = testfun(x1,x2)
%   %y = testfun(x1,x2);
%   %
%   %testfun is a test function that does absolutely nothing.
% 
%   y = x1*log2(x2.^2 + 1) + x2*log2(x1.^2 + 1);
%   end
% 
% % Save the function. Type in the command line:
% 
%   help testfun;
% 
% % Then type:
% 
%   y1 = testfun(2,2);
%   y2 = testfun(4,8);
% 
% % Play around with testfun by changing it. Add in extra output variables:
% % [y1 y2] = testfun(x1,x2);
% 
% % 7) Saving Data
% 
% % The data storage file type is called a .mat file. They can be accessed
% % using the save and load commands.
% 
% % Type in the command line:
% 
%   x = 1;
%   y = [1:10];
%   save testfile x y;
% 
%   clear;
% 
%   load testfile;
% 
%   clear;
% 
%   load testfile x;
% 
% % Just typing 'save testfile;' will save everything in the workspace in
% % testfile.mat. To delete the file, type:
% 
%   delete testfile.mat;
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  END SECTION 1  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %_______________________________________________________________________%%
% 
% 
% 
% 
% %_______________________________________________________________________%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  SECTION 2  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% % VARIABLE INITIALIZATION
% 
% % 1) Vectors
% 
% % Open garbage.m: 'edit garbage;'
% 
% % Highlight anything that is written. Go to:
% 
% % Text -> Comment
% 
% % Or use Ctrl+R / Ctrl+T to comment / uncomment.
% 
% % In garbage, type:
% 
%   z1 = [0 0 0 0 0];
%   z2 = zeros(1,5);
%   z3 = ones(1,5)-1;
%   z4 = repmat(0,[1 5]);
%   z5 = linspace(0,0,5);
% 
%   x1 = [1 2 3 4 5];
%   x2 = [1:5];
%   x3 = [1:1:5];
%   x4 = fliplr([5:-1:1]);
%   x5 = linspace(1,5,5);
% 
% % Run garbage. All of the z variables should be identical and all of the x
% % variables should be identical. In the command line, type 'help repmat;'
% % or use help for any other function to figure out what they each do.
% 
% % In general, one uses either the 'zeros', 'repmat', or 'linspace'
% % functions to initialize vectors.
% 
% % Vectors also act like one-dimensional variables. Add to your garbage
% % file the following lines:
% 
%   t = linspace(0,6*pi,1000);
%   plot(t,sin(t));
% 
% % 2) Matrices
% 
% % Comment out everything in garbage. Type:
% 
%   M = 10; N = 10;
%   x1 = zeros(M,N) + 5;
%   x2 = repmat(5,[M N]);
% 
% % Run garbage. x1 and x2 should be identically 5 everywhere.
% 
% % Matrices can also be to dimensional variables. To initialize them, use
% % the meshgrid function. Type in garbage:
% 
%   M = 10; N = 10;
%   [X Y] = meshgrid([-M:M],[-N:N]);
% 
%   figure;
%   subplot(1,2,1);
%     imagesc(X);
%   subplot(1,2,2);
%     imagesc(Y);
% 
% % Run garbage. The colormaps returned by imagesc should label the x and y
% % axes of the plane.
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  END SECTION 2  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %_______________________________________________________________________%%
% 
% 
% 
% 
% %_______________________________________________________________________%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  SECTION 3  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% % BASIC PROGRAMMING
% 
% % 1) for, while, if
% 
% % Comment out garbage. Type:
% 
%    N = 10;
%    t = linspace(0,6*pi,1000);
%    for k = 1:N
%       plot(t,sin(t + 2*pi*k/N));
%       axis tight;
%       pause;
%    end
% 
% % Run the code, and scroll through using any key - I prefer the arrows for
% % pause. Then comment it out, and type this into garbage:
% 
%    t = linspace(0,6*pi,1000);
%    N = 10;
%    k = 1;
% 
%    done = 0;
%    while(~done)
%       plot(t,sin(t + 2*pi*k/N));
%       axis tight;
%       pause;
%       k = k + 1;
%       if(k >= N)
%          done = 1;
%       end
%    end
% 
% % The two pieces of code should do the same thing. As always, use 'help'
% % to figure out how to interpret any of the functions you don't know.
% 
% % Here's one more piece of code to run in garbage:
% 
%    for k = 1:20
%       if(mod(k,3) == 0)
%          disp(int2str(k));
%       elseif(mod(k,2) == 0)
%          disp(int2str(-k));
%       else
%          disp('0');
%       end
%    end
% 
% % This one should give you a good sense of the elseif command.
% 
% % 2) and, or, not, equals, not equals
% 
% % Comment out garbage. Type:
% 
%    disp(['~0  :  ' int2str(~0)]);
%    disp(['~1  :  ' int2str(~1)]);
% 
%    disp(' ');
% 
%    disp(['0 & 0  :  ' int2str(0 & 0)]);
%    disp(['0 & 1  :  ' int2str(0 & 1)]);
%    disp(['1 & 0  :  ' int2str(1 & 0)]);
%    disp(['1 & 1  :  ' int2str(1 & 1)]);
% 
%    disp(' ');
% 
%    disp(['0 | 0  :  ' int2str(0 | 0)]);
%    disp(['0 | 1  :  ' int2str(0 | 1)]);
%    disp(['1 | 0  :  ' int2str(1 | 0)]);
%    disp(['1 | 1  :  ' int2str(1 | 1)]);
% 
%    disp(' ');
% 
%    disp(['0 == 0  :  ' int2str(0 == 0)]);
%    disp(['0 == 1  :  ' int2str(0 == 1)]);
%    disp(['1 == 0  :  ' int2str(1 == 0)]);
%    disp(['1 == 1  :  ' int2str(1 == 1)]);
% 
%    disp(' ');
% 
%    disp(['0 ~= 0  :  ' int2str(0 ~= 0)]);
%    disp(['0 ~= 1  :  ' int2str(0 ~= 1)]);
%    disp(['1 ~= 0  :  ' int2str(1 ~= 0)]);
%    disp(['1 ~= 1  :  ' int2str(1 ~= 1)]);
% 
% % Run garbage. And use good, old fashioned logic. Which one's which?
% 
% % 3) find
% 
% % find is one of the more useful functions in Matlab. It takes any logical
% % statement on a vector and returns the elements in that vector satisfying
% % the logical statement. Type in the command line:
% 
%    x = [1:10]; fnd = find(x > 5); x(fnd)
% 
% % Also type:
% 
%    find([1 0 1 0 1])
% 
% % Then:
% 
%    x > 5
% 
% % Then:
% 
%    find([-1 0 1 0 2])
% 
% % Okay, so what does find really do? (It's a simple function)
% 
% % 4) Reference
% 
% % There basically is no reference. If you type:
% 
%    x = 5;
%    y = x;
%    x = 10;
% 
% % y will not change to 10 because x did.
% 
% % When you set a variable equal to a second variable, the second variable
% % is copied into the new one. Basically no reference at all, which means
% % you need to be efficient with memory usage yourself. It also makes
% % Matlab a safe (though inefficient) language, from the memory management
% % standpoint.
% 
% % 5) Strings
% 
% % A string is just a character vector. Just like you can write a vector
% % as:
% 
%    v = [1 2 3 4 5];
% 
% % you can write a string as
% 
%   s = '1 2 3 4 5';
% 
% % All the string operator is doing is making sure numbers are interpreted
% % with respect to their ascii codes. You can trip this up and it's fun:
% 
%   s = 'abcdefgABCDEFG'; disp(s*1);
% 
% % Many functions take string arguments in Matlab. An interesting example
% % is the 'eval' function, which takes a string of code and evaluates it.
% % Type 'help eval;' for more information - or even 'eval('help eval;');'.
% %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  END SECTION 3  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %_______________________________________________________________________%%
% 
% 
% 
% 
% %_______________________________________________________________________%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  SECTION 4  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% % MATHEMATICS
% 
% % 1) Basic Operations
% 
% % Numbers:           a = 1;
% %                    b = 1;
% % Addition :         a + b;
% % Subtraction:       a - b;
% % Multiplication:    a * b;
% % Division:          a / b;
% % Exponentiation:    a^b;
% %
% %
% % Vectors:           x = [1:10];
% %                    y = [1:10];
% % Addition :         x + y;
% % Subtraction:       x - y;
% % Multiplication:    x .* y;
% % Division:          x ./ y;
% % Exponentiation:    x.^y;
% % Transpose:         x';
% % Inner Product:     x * y';
% % Outer Product:     x' * y;
% 
% % Many basic operations require a dot (x .* y) to specify that they should
% % be applied elementwise to vectors or matrices of the same dimensions.
% 
% % Most mathematical functions in Matlab are general, in the sense that
% % they operate elementwise on vectors and matrices.
% 
% % Matlab also supports complex operations:
% 
%    a = 1;
%    b = 1;
%    z = a + i*b;
% 
% % produces a complex number. All of the basic operations will change to
% %  their complex counterparts.
% 
% % Here are some useful mathematical functions:
% 
% % sin, cos, tan, atan2, exp, log, log2, real, imag, abs, angle, unwrap,
% % mod, min, max, mean, std, var, cov, corrcoef, median, conv, gradient,
% % fft, ifft, hilbert, filtfilt.
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  END SECTION 4  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %_______________________________________________________________________%%
% 
% 
% 
% 
% %_______________________________________________________________________%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  SECTION 5  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% % PLOTTING
% 
% % One of the most useful functionalities of Matlab is its professional
% % plotting library.
% 
% % 1) The plot function
% 
% % Comment out garbage. Type:
% 
%     t = linspace(0,100*pi + pi/2,5000);
% % Make some interesting data
%     f = (exp(-t/100)+1/2).*sin(t);
%     F = hilbert(f);
%     a = abs(F);
%     tau = unwrap(angle(F));
%     s = cos(tau);
% 
% 
%     figure;
%     plot(t,f);
%     axis tight;
%     xlabel('Time');
%     ylabel('Signal');
%     title('A Dampened Sine Wave');
% %
% % plot has a lot of conditions that can be set. Type 'help plot;' to take
% % a look. Here is an examples.
% 
%     figure;
%     plot(t,a,'r',t,s,'b',t,f,'k');
%     axis tight;
%     legend('Amplitude','Cos(Phase)','Signal');
%     xlabel('Time');
%     title('The Hilbert Factorization');
% 
% % You can make subplots too:
% 
%     figure;
%     subplot(1,2,1);
%         plot(t,f,'k');
%         axis tight;
%         xlabel('Time');
%         ylabel('Signal');
%         title('The Signal');
%     subplot(1,2,2);
%         plot(t,a,'r',t,s,'b');
%         axis tight;
%         legend('Amplitude','Cos(Phase)');
%         xlabel('Time');
%         title('The Hilbert Factorization');
% 
% % To clear the figures, type:
% 
%     close all;
%     
% 2) imagesc, contourf, and surf functions.
% 
% % Try this code:
% 
%     ind = linspace(-5*pi,5*pi,50);
%     [X Y] = meshgrid(ind,ind);
%     R = sqrt(X.^2 + Y.^2);
% 
%     figure;
%     subplot(2,2,1);
%         imagesc(ind,ind,cos(R));
%         set(gca,'YDir','normal');
%         colorbar;
%         title('The imagesc function');
%     subplot(2,2,3);
%         contourf(ind,ind,cos(R),5);
%         set(gca,'YDir','normal');
%         colorbar;
%         title('The contourf function');
%     subplot(1,2,2);
%         surf(ind,ind,cos(R));
%         set(gca,'FontSize',14);
%         title('The surf function');
% 
% % imagesc and contourf plots the y-axis in the reverse direction.
% % set(gca,'YDir','normal'); flips it. All three functions can be run without
% % the axis limits. Here are also two examples of other colormaps. Only one
% % colormap can exist per figure;
% 
%     figure;
%     imagesc(sin(R));
%     colormap('hot');
%     colorbar;
% 
%     figure;
%     imagesc(sin(R));
%     colormap('bone');
%     colorbar;
% 
% % Axes can be adjusted. This is done with the axis and caxis functions. In
% % the command line type:
% 
%     figure;
%     imagesc(ind,ind,cos(R));
%     axis([-2*pi 2*pi -4*pi 4*pi]);
%     caxis([-3 1]);
%     colorbar;
% 
% % Other plots: plot3, scatter, scatter3, contour, feather, quiver.
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  END SECTION 5  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %_______________________________________________________________________%%
% 
% 
% %_______________________________________________________________________%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  SECTION 6  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% % SOLVING DIFFERENTIAL EQUATIONS
% 
% % 1) inline and the @ function
% 
% % Functions can be specified as data structures and passed as variables.
% % They can then take vectors and matrices as inputs, just like any other
% % function. This can be done one of two ways:
% 
%     t = linspace(0,1,1000);
%     f1 = inline('10*sin(2*pi*10*t)','t');
%     f2 = @(t) 10*sin(2*pi*10*t);
% 
%     figure;
%     set(gcf,'Position',[337 212 936 418]);
%     subplot(1,2,1);
%         plot(t,f1(t),'b','LineWidth',2);
%         axis tight;
%         title('The inline function');
%     subplot(1,2,2);
%         plot(t,f2(t),'k','LineWidth',2);
%         axis tight;
%         title('The @ function');
% 
% % Type 'help inline' to learn more.
% 
% % 2) ode45
% 
% % Here is some sample code for running ode45:
% 
% % Solves the equations:
% %   du/dt = v
% %   dv/dt = -u
% % With the initial conditions 0, 0 from t = 0 to 10
% 
%     step = @(t,X) [ X(2); -X(1) ];
%     [t F] = ode45(step,[0 10],[0 1]); % step function, time bounds, initial conditions
%     figure;
%     plot(t, F);
%     axis tight;
% % 
% % With more complex systems of differential equations you will need to
% % make a seperate file with a function that performs the step. Make a
% % new file with a function that solves the system:
% %
% %   du/dt = v-0.1u
% %   dv/dt = -u-0.1v
% %
% % Lastly, recreate the Lorentz Attractor.
% % 
% % Constants
% % sigma = 10;  % Prandlt number
% % rho = 28;    % Rayleigh number
% % beta = 8.0/3;
% % 
% % Integrate!
% % lorenz = @(t, n) [sigma*(n(2)-n(1)); n(1)*(rho-n(3))-n(2); n(1)*n(2) - beta*n(3)];
% % [t, n] = ode45(lorenz, [0 200], [1; 1; 1]);
% % 
% % x = n(:, 1);
% % y = n(:, 2);
% % z = n(:, 3);
% % plot3(x, y, z)
% % 
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %  END SECTION 6  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 