1D vector example

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Vec1dex.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 it's useful GREAT if not sorry.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% This script gives an idea of how to carry out vector operations in MATLAB
% using Blasius boundary layer profile as an example. Here delta =f(x). That is
% the boundary layer profile is plotted at different x locations for a single
% freestream velocity. You can choose between four ways of doing this
% (i) for loop operations - the standard C/Fortan method of doing it - This
% is extremely slow. PLEASE, I repeat PLEASE do not use for loops for
% anything more than a 1000 points without variable declarations/ memory
% allocation. The reason is that MATLAB evaluates for loops line by line
% and allocates memory as the vector grows. one word - NO!!!!!
% (ii) Vectorized operations for which MATLAB is optimized to work
% (iii) For loop operations with variable declaration (memory
% allocation) - this as it turns out is almost as efficient as the
% vectorized option
% (iv) Using variable declaration as well as vectorized operations - no real

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=1; % 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

U_inf=20;% in m/s

switch (method)
case 1
x=linspace(0,10,n);% in m

for (i=1:1:length(x))
delta(i)=3.5*sqrt(mu*x(i)/rho/U_inf);
end
smethod='for loop operations';
case 2
x=linspace(0,10,n);% in m

delta=3.5.*sqrt(mu.*x./rho/U_inf);
smethod='vectorized operations';
case 3
x=zeros(1,n);
delta=zeros(1,n);
x=linspace(0,10,n);% in m

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

smethod='for loop operations with variable decleration';
case 4
x=zeros(1,n);
delta=zeros(1,n);
x=linspace(0,10,n);% in m

delta=3.5.*sqrt(mu.*x./rho/U_inf);
smethod='vectorized operations with variable declerations';
end

figure
plot(x,delta)
xlabel('x in m')
ylabel('\delta_{99} in m');
title('Blasius - Boundary layer thickness on flat plate');
legend('\delta_{99}',4);
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)]