Write down the code & take screenshot for the output MATLAB 8. Animation: Browni
ID: 2291284 • Letter: W
Question
Write down the code & take screenshot for the output MATLAB
8. Animation: Brownian motion. Write a function with the following declaration: brown2D (N) The function takes in a single input N, which is an integer specifying the number of points in the simulation. All the points should initially start at the origin (0,0). Plot all the points on a figure using markers, and set the axis to be square and have limits from -1 to 1 in both the x and y direction. To simulate Brownian motion of the points, write a 1000-iteration loop which will int and will display thesen animation. The new position of each point is obtained by adding a normally distributed random variable with a standard deviation of 0.005 to each x and y value (use randn; if you have 100 points, you need to add 100 distinct random values to the x values and 100 distinct random values to the y values). Each time that the new positions of all the points are calculated, plot them on the figure and pause for 0.01 seconds (it's best to use set and the line object handle in order to update the xdata and ydata properties of the points, as mentioned in lecture). What you will see is a simulation of diffusion, wherein the particles randomly move away from the center of the figure. For example, 100 points look like the figures below at the start, middle, and end of the simulation:Explanation / Answer
% Simulation of Brownian motion of 'np' Particles for T seconds
clear;
clc;
T= input('Enter the Time Period: '); % Time (in Seconds)
N=100.*T; % Number of Impulses/change in track
h=sqrt(T/N);
np=input('Enter the Number Of Particle: '); % Number of Particles
% Initialization of the position of particles..i.e origin
x = zeros(1,np);
y = zeros(1,np);
z = zeros(1,np);
% Iteration to store positions of particles
for j=1:np
for i=1:N
x(i+1,j)=x(i,j)+h*randn();
y(i+1,j)=y(i,j)+h*randn();
z(i+1,j)=z(i,j)+h*randn();
end
end
cmap = hsv(np); % Creates a np-by-3 set of colors from the HSV colormap
% Plotting of the particles.
for k=1:np
plot3(x(:,k),y(:,k),z(:,k),'Color',cmap(k,:));
hold on;
end
grid on;
another method
% This simulation illustrates a fast implementation of three dimensional
% Brownian motion, the output is the Euclidean distance between initial
% and final positions.
% To calculate the mean value of T runs, run the following code in the
% Command window :
%
% >>T=100;
% >> for n=1:T
% >>Brownianmotion;
% >>close;
% >>D(n)=d;
% >>end
% >>figure; plot(D),title(' Distance ');
% >>dd=mean(D)
% (c) Youssef Khmou, Applied mathematics, may 2015.
N=1000;
x=cumsum(randn(1,N));
y=cumsum(randn(1,N));
z=cumsum(randn(1,N));
h=figure;
view(3);
set(gca,'GridLineStyle','--')
hold on;
plot3(x,y,z,'LineWidth',1.5)
axis([min(x) max(x) min(y) max(y) min(z) max(z)]);
xlabel('x');
ylabel('y');
zlabel('z');
% initial radius
r0=[x(1) y(1) z(1)];
% final radius
rf=[x(end) y(end) z(end)];
plot3(r0(1),r0(2),r0(3),'or','MarkerFaceColor','r');
plot3(rf(1),rf(2),rf(3),'ok','MarkerFaceColor','k');
% Line of Sight between initial and final state
%xx=linspace(r0(1),rf(1),10);
%yy=linspace(r0(2),rf(2),10);
%zz=linspace(r0(3),rf(3),10);
%plot3(xx,yy,zz,'k--','LineWidth',2);
grid on;
% Distance
d=sqrt(sum((rf-r0).^2));
Information=strcat('Three dimensional Brownian Motion, d=',num2str(d),' units');
title(Information ,'FontWeight','bold');
view(-109,58);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.