# Examples by accinent

VIEWS: 134 PAGES: 5

• pg 1
```									                                       Examples
   BumgeeJump: G – BMEN_MATLAB - BumgeeJump
   array: a=10:1:3000; length(a) a (291) a(2981:end)
   linspace(x_initial, x_final, n):       linspace(0, 1, 6)
   logspace(x_initial, x_final, n):       logspace(-1, 2, 4)
   x=0:0.1:10;      y=2*exp(-0.2*x);                  y1=2*sqrt(y); plot(x,y,x,y1,’r--‘)

   function [mean, stdev] = statistics(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/(n-1)));

>>y = [8 5 10 12 6 7.5 4];
>> [m,s] = statistics (y)

   function fprintfdemo
x = 1:5;
y = [20.4 12.6 17.8 88.7 120.4];
z = [x;y];
fprintf(‘  x   y\n’)
fprintf(‘%5d %10.3f\n’,z);

   Sample problem: Medical studies have established that a bungee jumper’s
chances of sustaining a significant vertebrae injury increase significantly if
the free fall velocity exceeds 36 m/sec after 4 sec of free-fall. SAFEFIRST
Bungee Company has contracted Aggie Biomedical Engineering
Consulting Associates to study the safety issues of bungee jumpers’
vertebrae injury. As an engineer, you have learned that the time-
   gcd                gcd   
         t                 t
                            
    m    
e        m                    gcd 
v t  
gm e                                          gm
      tanh
 m t

cd  

gcd   
t




gcd   
t

cd          
e
m                    m
e                              

where g = 9.81 m/sec2, m is the jumper’s body mass in kg, and cd, which
is equal to 0.25 kg/m, is the air drag coefficient. At what critical mass (or
equivalent body weight in pounds) that the velocity will exceeds 36 m/sec
in 4 seconds? As a further precaution measure, the company uses 90%
of this weight as the cut-off for jumpers, what is this cutoff weight (rounded
downward to the closest 10’s in pounds)? (see MATLAB: freefallfuncfun &
freefallvel)

   fzero, GraphicMethod, bisection, incsearch, Newton, newtraph,
miscfunctions, Multi_roots, Fixed_Point, F_Point1, F_Point2
   fplot(inline('x^3-2*x^2 +1'), [-3,3]), grid
   fzero(inline(‘'x^3-2*x^2 +1'), 0)
   fzero(inline(‘'x^3-2*x^2 +1'), 1)
   fzero(inline(‘'x^3-2*x^2 +1'), 2)

   freefallinteract, freefallvel, freefallfunc, freefallfuncfunc, euler, euler1,
euler2
   fminbnd, fminsearch

% Eample for fminbnd
function y=fBMEN289_4(x)
y=exp(-x)-2*cos(x)-1;
format long
% fplot('fBMEN289_4',[0,10]);hold on
%%xx=[0,10]; yy=[0,0]; plot(xx,yy); hold off
% [x,f]=fminbnd('fBMEN289_4',5,7)

% Example of using fminbnd to find a maximum
function f=fBMEN289_6(x)
f=-(0.4./sqrt(1+x.^2)-sqrt(1+x.^2).*(1-0.4./(1+x.^2))+x);
% NOTE: . in front of an operator-array operation
format short
% max f=(0.4/sqrt(1+x^2)-sqrt(1+x^2)*(1-0.4/(1+x^2))+x);
% min -f
% fplot('fBMEN289_6',[0,10])
% fminbnd('fBMEN289_6',0,2)
% z=1:5;
% fBMEN289_6(z)

% s=1:10;
%% Try - x=s^1.5;
% x=s.^1.5;
%% Try - y=(0.4/sqrt(1+x^2)+sqrt(1+x^2)*(1-0.4/(1+x^2))+x);
% y=(0.4./sqrt(1+x.^2)+sqrt(1+x.^2).*(1-0.4./(1+x.^2))+x);

   Examples for plots

% Example for plot
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
% r: red
% s: square
% k: black
% g: green

% subplot(m,n,p) creates an axes in the p-th pane of a figure divided into
% an m-by-n matrix of rectangular panes. The new axes become the current axes.
% income = [3.2 4.1 5.0 5.6]; outgo = [2.5 4.0 3.35 4.9];
% subplot(2,1,1); plot(income); subplot(2,1,2); plot(outgo)

% Example of 3D plot: meshgrid, contour, and the function fminsearch
% Find Maximum of f(x)=(2*x(1)*x(2)+2*x(1)-x(1)^2-2*x(2)^2);
function f=fBMEN289_9(x)
f=-(2*x(1)*x(2)+2*x(1)-x(1)^2-2*x(2)^2);
format long
% x=-10:.5:10;
% y=-10:.5:10;
% [X,Y]=meshgrid(x,y);
% Z=2*X.*Y+2*X-X.^2-2*Y.^2;
% contour(x,y,Z);
% contour3(x,y,Z);
% surfc(x,y,Z);
% [xy,f]=fminsearch('fBMEN289_9',[2.5,1])
% [xy,f_opt]=fminsearch(@fBMEN289_9,[2.5,1])
% [xy,f]=fminsearch(inline('-(2*x(1)*x(2)+2*x(1)-x(1)^2-2*x(2)^2)'),[2.5,1])
%%
% 2.00003177079141 1.00001034131676

%% ezplot: function plot
% ezplot('x^2-y^4')
%% ezcoutour: function contourplot
% f = ['3*(1-x)^2*exp(-(x^2)-(y+1)^2)',...
% '- 10*(x/5 - x^3 - y^5)*exp(-x^2-y^2)',...
% '- 1/3*exp(-(x+1)^2 - y^2)'];
% ezcontour(f,[-3,3],49)
%% [-3,3]: range of the plot
%% 49: 49 by 49 grids
% ezcontourf(f,[-3,3],49)
%% ezcontourf: filled contour plot
% ezmesh: 3Dmesh plot
% colormap: color code-[R,G,B]
% ezmeshc('y/(1 + x^2 + y^2)',[-5,5,-2*pi,2*pi])
%% ezmeshc: mesh & contour functional plots with [xmin,xmax,ymin,ymax]
% ezplot3('sin(t)','cos(t)','t',[0,6*pi])
%% ezplot3: 3D parametric plot
% ezpolar('1+cos(t)')
%% ezpolar: polar plot
% ezsurf('real(atan(x+i*y))')
%% ezsurf: surface plot
% ezsurfc('y/(1 + x^2 + y^2)',[-5,5,-2*pi,2*pi],35)
%% ezsurfc: surface & contour plots

% Example of defining and plotting two functions simultaneously
function y=fBMEN289_12(x)
y(:,1)=x(:)^2-2*x(:)*exp(-x(:))+exp(-2*x(:));
y(:,2)=cos(x(:)+sqrt(2))+x(:)*(0.5*x(:)+sqrt(2));
% fplot('fBMEN289_12',[0,1]),grid

   factor
function fout=factor(n)
% compute factorial (n)
x=1;
for i=1:n
x=x*i;
end
fout=x;

% system function is factorial(n)

if a==0
if b~=0
r1=-c/b
else
end
else
d=b^2-4*a*c;
if d >= 0
r1=(-b+sqrt(d))/(2*a)
r2=(-b-sqrt(d))/(2*a)
else
r1=-b/(2*a)
r2=r1
i1=sqrt(abs(d))/(2*a)
i2=-i1
end
end

% system function is roots([a b c])

   Polynomial: Example7_7

% Example 7.7
a=[1 -3.5 2.75 2.125 -3.875 1.25];
b=[1 0.5 -0.5];
fplot('x^5-3.5*x^4+2.75*x^3+2.125*x^2-3.875*x+1.25',[-1.1,2.1]); hold on
xx=[-2,4];yy=[0,0]; plot(xx,yy);grid on; hold off
% polyder(a)
% [d,e]=deconv(a,b); d = a/b and e is the remainder
% roots(a); roots(b); roots(d)
%% roots can only be used in polynomials
% conv(b,d); which gives back a, the original polynomial

   Matrix operations: A*B (matrix multiplication), A.*B (array multiplication),
MInversion, Gauss_Seidel, GAussSeidel, GaussPivot, LU
   Regressions: linregr, fSSE
   Interpolations: Interp (spline, interp1)