Selasa, 13 Januari 2015

contoh program matlab untuk koordinat MAP-DEPTH -KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060



contoh program matlab untuk koordinat MAP-DEPTH

 (jIndex-yCentre)*(jIndex-yCentre);
      if (abs(distFromCentre-radius^2)<=1)
         newMap(jIndex,iIndex) = …
             depth+0.02*difference;
      elseif (distFromCentre < radius^2)
         newMap(jIndex,iIndex) = …
              depth*(100.0+randn)/100.0;
      else
         newMap(jIndex,iIndex)=oMap(jIndex,iIndex);
      end;
   end;
end;
  
27.15     River Addition – Algorithm
• Add a river flowing across the map
• Notes:
- user may specify general direction of flow, width of river, and meandering quality of flow
- at any time the river may be flowing in one of the 8 cardinal compass direction (N,NE,E,…NW). An appropriate data structure is set up for that. At any instance in its creation it can change course by 45 degrees, run straight, or return to its original general direction.
Top-Level Algorithm
set up compass
handle variable input arguments
initialise initial depth and meander chances
start river in random location based on direction of flow (along opposite edge to flow)
while the river has not reached a map edge
               extend the river
Extend the River
Make river deeper (increment depth)
determine a new direction
move current location one step in that direction
set current location to current river depth
extend river to appropriate width (tangent to
    direction of current flow)
Determine a new Direction
use current direction, a random number and meandering measure to determine new direction:
   - continue in current direction
   - altered left or right by 45-degrees
   - reset to the general direction of the river
27.16     River Addition – Example
afterRiver=mapRiver(afterMeteor,’north’,40,3);
afterRiver=mapSmooth(afterRiver,2);
surf(afterRIver);
shading interp;
colormap copper;
axis off;
<print>
contour(afterRiver);
colormap default;
axis off;
<print>

27.17     Map with River

27.18     River Addition – Code
% mapRiver - ADD A RIVER TO AN EXISITING MAP
%
%  newmap=mapRiver(omap)
%            Create a new map through addition of a random
%            river to an exisiting (omap) map.
%
%  newmap=mapRiver(omap,direction)
%            Creates a map heading in a general direction.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email
Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.   Email: kursusmatlab@gmail.com

%            Possible directions are 'north', 'south',
%            'east' and 'west'. 'north' is the default
%            value.
%
%  newmap=mapRiver(omap,direction,meander)
%            As above but control the degree of meandering
%            of the river. Meander should lie between
%            1 and 100, with 1 indicating almost straight
%            and 100 highly variable in direction. 50 is
%            the default value.
%
%  newmap=mapRiver(omap,direction,meander,width)
%            As above but the width of the river is now
%            width pixels, rather than the default 1.
%
%  Starts a river at the edge of the map and
%  allows it to meander around till it touches
%  one of the four edges.
%
% Author: Spike
% Date:   31/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function map = mapRiver(map,directionName,meander,width)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Take care of default argument values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin==0
  error('Must supply an exisiting map');
end;

River Addition Code (Cont)
if nargin<4
  width=1;
end;
if nargin<3
  meander=50;
end;
if nargin==1
  directionName='north';
end;
width=abs(width);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A river may meander all over the place. Define
% the different possible directions and what
% they mean in a 2D sense. Then group them into
% a single circular vector (i.e., run off one
% end come back  on the other) vector called
% compass.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email
Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.   Email: kursusmatlab@gmail.com

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NORTH=[0 1]; NORTH_EAST=[1 1];
EAST=[1 0]; SOUTH_EAST=[1 -1];
SOUTH=[0 -1]; SOUTH_WEST=[-1 -1];
WEST=[-1 0]; NORTH_WEST=[-1 1];
compass={NORTH NORTH_EAST EAST SOUTH_EAST SOUTH SOUTH_WEST WEST NORTH_WEST};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define how much our river might wander. Reset
% is the chance the river will return the the
% original direction it started on the map.
% crookedness is the chance that it will change
% direction by 45 degrees left or right.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
meander=min(abs(meander),100);
reset=(100-meander)/500;
default=(100-meander)*0.004+0.3;
crookedness=(1.0-reset-default)/2;

River Addition Code (Cont)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determine how deep the river will run.
% Basically it starts at the depth of the lowest
% point on the map and continues to descend at a
% rate proportional to the difference between
% the highest & lowest points on the map.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dimensions=size(map);
lowPoint=min(min(map));
highPoint=max(max(map));
depth=lowPoint;
if highPoint~=lowPoint
  drop=(highPoint-lowPoint)/(max(dimensions));
else
  drop=lowPoint/100;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Choose the initial direction for the river
% and a semi-random starting point on the
% appropriate edge of the map.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email
Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.   Email: kursusmatlab@gmail.com

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
directionName=lower(directionName);
if strcmp(directionName,'north')
  direction=1;
  location=[round(dimensions(1)/2+ dimensions(1)*randn/6) 1];
elseif strcmp(directionName,'east')
  direction=3;
  location=[1 round(dimensions(2)/2+ dimensions(2)*randn/6)];
elseif strcmp(directionName,'south')
  direction=5;
  location=[round(dimensions(1)/2+ dimensions(1)*randn/6) dimensions(2)];
elseif strcmp(directionName,'west')
  direction=7;
  location=[dimensions(1) round(dimensions(2)/2+ dimensions(2)*randn/6)];
else

River Addition Code (Cont)
 
warning(['Unknown direction: ' directionName ', setting to north']);
  direction=1;
  location=[round(dimensions(1)/2+ dimensions(1)*randn/6) 1];
end;
cardinalDirection=direction;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start the river off at the edge location and
% march it in one pixel in the cardinal
% direction.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
map(location(1),location(2))=depth;
location=location+compass{direction};
map(location(1),location(2))=depth;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If the river is wide then we also need to
% makes its sides for this compulsory march in
% of one pixel.
% Tangent is the direction at right-angles to
% the flow. Loop for however wide it needs to be
% out from the centre, calculating the point
% (channel) and testing if that point is still
% on the map. If it is then set the depth at
% that location.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tangent=direction+2;
if tangent>length(compass)
  tangent=tangent-length(compass);
end;
sideRange=-floor((width-1)/2):floor((width-1)/2);
sideRange=sideRange(sideRange~=0);
for sides=sideRange
  channel=location+sides*compass{tangent};
  if channel(1)>=1 & channel(1)<=dimensions(1) & channel(2)>=1 & channel(2)<=dimensions(2)
    map(channel(1),channel(2))=depth;
  end;

River Addition Code (Cont)
 channel=location-compass{direction}+sides*compass{tangent};
 if channel(1)>=1 & channel(1)<=dimensions(1) & channel(2)>=1 & channel(2)<=dimensions(2)
    map(channel(1),channel(2))=depth;
  end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Keep extending the river till we hit one of
% the four edges. Extension is done randomly
% based on the settings of 'reset' and
% crookedness.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while location(1)>1 & location(1)<dimensions(1) & location(2)>1 & location(2)<dimensions(2)
  depth=depth-drop;
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determine the new direction based on a random
% event. The direction may reset to the
% original, deviate by 45 degrees either left or
% right, or continue straight
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  chance=rand;
  if chance<reset
    direction=cardinalDirection;
  elseif chance<crookedness+reset
    direction=direction-1;
  elseif chance<2*crookedness+reset
    direction=direction+1;
  end;
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Ensure wrap-around in our compass vector
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  if direction>length(compass)
    direction=1;
  elseif direction<1
    direction=length(compass);
  end;

River Addition Code (Cont)
 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Find the new point on the map through which
  % river passes & set its depth appropriately.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email
Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.   Email: kursusmatlab@gmail.com

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  location=location+compass{direction};
  map(location(1),location(2))=depth;
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % If the river is wide then we must mark out
  % not only the centre of the channel but out
  % to the sides. Tangent is the direction at
  % right-angles to the flow. Loop for however
  % wide it needs to be out from the centre,
  % calculating the point (channel) and testing
  % if that point is still on the map. If it is
  % then set the depth at that location.
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  tangent=direction+2;
  if tangent>length(compass)
    tangent=tangent-length(compass);
  end;
  sideRange=-floor((width-1)/2):floor((width-1)/2);
  sideRange=sideRange(sideRange~=0);
  for sides=sideRange
    channel=location+sides*compass{tangent};
    if channel(1)>=1 & channel(1)<=dimensions(1) & channel(2)>=1 & channel(2)<=dimensions(2)
      map(channel(1),channel(2))=depth;
    end;
  end;
end;
27.19     Review
• From (vague) specification to design
• Data-structure matching to real-world object
-              2D array (matrix) of heights for a map
-              wrap-around 1D array as a compass
• Algorithms
• Programming style
-              indentation
-              commenting & identifier choice
• Major Matlab language features:
- function writing
- variable number of arguments
- variable number of return values
- for loops
- if-elseif statements
- while loops
ORF335 Precept  -  MATLAB Basics
Feb 15th, 2011
Preparatory Steps:
1.            Open MATLAB
2.            Locate “Command Window”
3.            Check “Current Directory”
4.            Open new m-file: File ( New(M-file
%%%%%%% Basic Commands %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%% General

clear           %free up system memory
help plot       %display help for the command 'plot'
% Ctrl+C        % To break a computation / infinite loop

%%%% Numbers

1+4*sqrt(16)
a=max(3,sqrt(8));
result=exp(log(3));
result^(1/2)
sin(pi*5/3)

%%%% Vectors

x=[1;2;4]
y=[2;3;4]
length(x)
x(1)
x(1:2)
x'
x_transp=[1 2 4]

% Entry by entry operation
x.*y; %entry by entry product

% Regular operation
x*y'
%x*y;  % This is wrong

x.^y % Entry by entry power

%%%% Matrices

A=[1 1 1 ; 1 2 3 ; 1 3 6] %3 by 3 matrix
size(A)              % dimensions of A, which is 3*3
Q = ones(3,3)        % Matrix with all entries 1
P = zeros(size(A))         % Matrix with the size of A with all entries 0

B = A' % transpose of A
C1 = A * B  % Regular matrix multiplication
C2 = A .* B % Entry by entry

X = inv(A)
I = inv(A) * A

%%%% Randomness

n=5;
u=rand(n); % matrix of n by n independent realizations
           % of a uniform in [0,1]
          
v_0=randn(n); % matrix of n by n independent realizations
           % of a standard gaussian N(0,1)
mu=1;
sigma=0.3;
v=mu+sigma*rand(n); % matrix of n by n independent realizations
                    % of a gaussian N(mu,sigma^2)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Loops, Tests...%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;

%%%% Tests

x=randn(1);
if x>0
    disp('Pick is positive');
    disp(x);
else
    disp('Pick is negative and absolute value is');
    disp(abs(x));
end

%%%% Logical operator
a=1; b=0;
(a==1)
(b~=0)
% '&' is AND
(a==1) & (b~=2)
% '|' is OR
(a>-3) | (b==0)

%%%% Loops
n=5;
sum=0;
for i=1:5
    sum=sum+i;
end
disp(sum)
disp(n*(n+1)/2)

% when step is not 1
% for x=0:0.02:5

i=0; sum=0;
while (i<n)
    i=i+1;
    sum=sum+i;
end
disp(sum)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Plots...%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x=0:0.01:2*pi;
y=zeros(length(x),1);
for i=1:length(x)
    y(i)=sin(x(i));
end

figure(1)
plot(y)

figure(2)
plot(x,y,'b--')
title('Sin(x)')
xlabel('x')
ylabel('sin(x)')

figure(3)
plot(x,y,'r--',x,y.^2,'b')
legend('sin x','sin^2 x')

% equivalently
figure(4)
plot(x,y,'r--'); hold on;
plot(x,y.^2,'b'); hold off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Example %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 4-period Binomial tree, starting at 100, with u=1.1, d=0.9, r=0.05, T=1.

%set parameter values
S0=100;
u=1.1;
d=0.9;
r=0.05;
T=1;
%create matrix to represent stock price tree (only use top right)
S=zeros(5,5);
for n=1:5
for j=1:n
                              S(j,n)=d^(j-1)*u^(n-j)*S0;
end
end
S
%now the tree `looks’ correct (using the top right half of the matrix), and the indices are consistent with lecture notes, except with time and space swapped (time=1st index in notes, 2nd here), and each shifted by 1 (since can’t use 0). 
% S_{n,j} in the notes = S(j+1,n+1) here.  You can use a different convention if you prefer!
%price call option with strike K=93, T=1 (remember to work backwards now):
K=93;
C=zeros(5,5);
for j=1:5
C(j,5)=max(S(j,5)-K,0);   %payoffs
end
C
q=(exp(r*T/4)-d)/(u-d)  %risk neutral prob
for n=4:-1:1
for j=1:n
                              C(j,n)=exp(-r*T/4)*(q*C(j,n+1)+(1-q)*C(j+1,n+1));
end
end
C
%The same looping idea can be used to find the replicating portfolio (a and b) throughout the tree by adding extra lines above.  First define additional matrices.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email
Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060.   Email: kursusmatlab@gmail.com

Tidak ada komentar:

Posting Komentar