2D Vector Example

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Vec2dex.m 
% Written by Ebenezer P Gnanamanickam 
% Dec 2006.
% DISCLAIMER - I do not claim to be an expert on MATLAB but what little I
% have learned during the course of grad school here at Purdue University
% I want to share. If its useful GREAT if not sorry.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This script gives an idea of how to carry out matrix operations in MATLAB 
% using Blasius boundary layer profile as an example. Here I calculate and
% plot the boundary layer profile for various free stream velocities at 
% different x locations. i.e delta=f(U_inf,x) . 
% You can choose between three ways of doing it 
% (i) nested for loop operations with variable declaration/memory
% allocation
% (ii) single for loop operations - so I use vector operations for one set
% of independent variables.
% (iii) Complete vector operations - now this is fancy and please NOTE it
% uses up a LOT of memory. I wouldn't recommend it but it marginally speeds
% up your code . But when the matrix becomes really really large this
% becomes extremely useful. The key to doing this, is to understand that the
% computer essentially treats every single matrix stacked up as a long 
% vector.
% Again you can check out for your self the various run times. It's again a
% good example for plotting stuff. I deliberately made the  plotting more fancy than
% it needs to be to make it a useful learning example

clc; % clears the command window
clear all; % clears all the variables
close all; % close all existing windows

tstart=clock; % starts the clock to calculate the run time

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUTS
n=100000; % number of points
method=3; % choose the method you want to use
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mu=1.73e-5*10e-5; % in N-s/m^2
rho=1.229; % in kg/m^3

switch (method)
    case 1
        x=zeros(1,n);
        x=linspace(0,10,n);% in m
        U_inf=[1 5 10 20 30 40];% in m/s
        delta=zeros(length(U_inf),length(x));

        for (i=1:1:length(U_inf))
            for (j=1:1:length(x))
                delta(i,j)=3.5*sqrt(mu*x(j)/rho/U_inf(i));
            end
        end

        smethod='double for loop operations';
    case 2
        x=zeros(1,n);
        x=linspace(0,10,n);% in m
        U_inf=[1 5 10 20 30 40];% in m/s
        delta=zeros(length(U_inf),length(x));
        
        for (i=1:1:length(U_inf))
            delta(i,:)=3.5.*sqrt(mu.*x./rho/U_inf(i));
        end
        
        smethod='single for loop operations';
    case 3
        x=zeros(1,n);
        x=linspace(0,10,n);% in m
        U_inf=[1 5 10 20 30 40];% in m/s
                
        delta=reshape(3.5*sqrt(mu.*repmat(x,1,length(U_inf))/rho./...
            reshape(repmat(U_inf,length(x),1),1,length(x)*length(U_inf))),...
            length(x),length(U_inf));
        
        smethod='vector operations';    
end

figure
plot(x,delta)
xlabel('x in m')
ylabel('\delta_{99} in m');
title('Blasius - Boundary layer thickness on flat plate');
legend('U_\infty = 1','U_\infty = 5','U_\infty = 10',...
    'U_\infty = 20','U_\infty = 30','U_\infty = 40',2);
grid;

tfinish=clock; % end run time

runtime=etime(tfinish,tstart); % estimates the runtimes

s1=['number of points = '  num2str(n)]
s2=['method = ' smethod]
s3=['run time = ' num2str(runtime)]