<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://bradleymonk.com/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Bradley+Monk</id>
	<title>bradwiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://bradleymonk.com/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Bradley+Monk"/>
	<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Special:Contributions/Bradley_Monk"/>
	<updated>2026-04-09T15:05:07Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Computer_Science&amp;diff=4153</id>
		<title>Computer Science</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Computer_Science&amp;diff=4153"/>
		<updated>2026-04-04T08:25:14Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Conceptual &amp;amp; Structural Systems ==&lt;br /&gt;
&lt;br /&gt;
(How software is *organized* at a high level)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Architecture]]&#039;&#039;&#039; – The fundamental structure of a system: components, responsibilities, and relationships.&lt;br /&gt;
* &#039;&#039;&#039;[[Design Pattern]]&#039;&#039;&#039; – Reusable solutions to recurring structural or behavioral problems.&lt;br /&gt;
* &#039;&#039;&#039;[[System Design]]&#039;&#039;&#039; – End-to-end planning of components, data flow, scaling, and failure modes.&lt;br /&gt;
* &#039;&#039;&#039;[[Platform]]&#039;&#039;&#039; – A base system upon which other software or services are built.&lt;br /&gt;
* &#039;&#039;&#039;[[Ecosystem]]&#039;&#039;&#039; – Interacting tools, libraries, services, and users around a platform.&lt;br /&gt;
* &#039;&#039;&#039;[[Stack]]&#039;&#039;&#039; – A layered collection of technologies used together (e.g., LAMP, MERN).&lt;br /&gt;
* &#039;&#039;&#039;[[Subsystem]]&#039;&#039;&#039; – A self-contained functional unit within a larger system.&lt;br /&gt;
* &#039;&#039;&#039;[[Module]]&#039;&#039;&#039; – A discrete, replaceable unit of functionality.&lt;br /&gt;
* &#039;&#039;&#039;[[Component]]&#039;&#039;&#039; – A deployable or reusable unit with a defined interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Code Reuse &amp;amp; Capability Systems ==&lt;br /&gt;
&lt;br /&gt;
(Things you *build with*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Framework]]&#039;&#039;&#039; – Opinionated structure that controls application flow.&lt;br /&gt;
* &#039;&#039;&#039;[[Library]]&#039;&#039;&#039; – Reusable code invoked by the developer.&lt;br /&gt;
* &#039;&#039;&#039;[[SDK (Software Development Kit)]]&#039;&#039;&#039; – Tools, libraries, docs, and examples for a platform.&lt;br /&gt;
* &#039;&#039;&#039;[[API]]&#039;&#039;&#039; – A formal interface for interacting with a system or service.&lt;br /&gt;
* &#039;&#039;&#039;[[Middleware]]&#039;&#039;&#039; – Software that connects or mediates between systems.&lt;br /&gt;
* &#039;&#039;&#039;[[Runtime]]&#039;&#039;&#039; – The environment in which code executes.&lt;br /&gt;
* &#039;&#039;&#039;[[Plugin System]]&#039;&#039;&#039; – Mechanism for extending a host application.&lt;br /&gt;
* &#039;&#039;&#039;[[Extension]]&#039;&#039;&#039; – Add-on module that enhances functionality.&lt;br /&gt;
* &#039;&#039;&#039;[[Package]]&#039;&#039;&#039; – A distributable unit of code.&lt;br /&gt;
* &#039;&#039;&#039;[[Dependency]]&#039;&#039;&#039; – External code required by a system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Execution &amp;amp; Environment Systems ==&lt;br /&gt;
&lt;br /&gt;
(Where software *runs*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Environment]]&#039;&#039;&#039; – A configured context (variables, paths, services).&lt;br /&gt;
* &#039;&#039;&#039;[[Runtime Environment]]&#039;&#039;&#039; – OS + libraries + execution context.&lt;br /&gt;
* &#039;&#039;&#039;[[Virtual Environment]]&#039;&#039;&#039; – Isolated dependency space (e.g., Python venv).&lt;br /&gt;
* &#039;&#039;&#039;[[Container]]&#039;&#039;&#039; – Lightweight, isolated execution unit (e.g., Docker).&lt;br /&gt;
* &#039;&#039;&#039;[[Virtual Machine (VM)]]&#039;&#039;&#039; – Fully virtualized OS instance.&lt;br /&gt;
* &#039;&#039;&#039;[[Sandbox]]&#039;&#039;&#039; – Restricted execution environment for safety.&lt;br /&gt;
* &#039;&#039;&#039;[[Host System]]&#039;&#039;&#039; – The machine or OS running workloads.&lt;br /&gt;
* &#039;&#039;&#039;[[Target Environment]]&#039;&#039;&#039; – Intended deployment context.&lt;br /&gt;
* &#039;&#039;&#039;[[Emulator]]&#039;&#039;&#039; – Software that mimics another system.&lt;br /&gt;
* &#039;&#039;&#039;[[Interpreter]]&#039;&#039;&#039; – Executes code directly.&lt;br /&gt;
* &#039;&#039;&#039;[[Compiler Toolchain]]&#039;&#039;&#039; – System that translates source to binaries.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Development &amp;amp; Tooling Systems ==&lt;br /&gt;
&lt;br /&gt;
(How developers *work*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[IDE]]&#039;&#039;&#039; – (Integrated Development Environment) Unified coding, debugging, and tooling system.&lt;br /&gt;
* &#039;&#039;&#039;[[Editor]]&#039;&#039;&#039; – Code authoring environment.&lt;br /&gt;
* &#039;&#039;&#039;[[Build System]]&#039;&#039;&#039; – Automates compilation, linking, and packaging.&lt;br /&gt;
* &#039;&#039;&#039;[[Toolchain]]&#039;&#039;&#039; – Collection of compilers, linkers, debuggers, etc.&lt;br /&gt;
* &#039;&#039;&#039;[[Package Manager]]&#039;&#039;&#039; – Manages dependencies and versions.&lt;br /&gt;
* &#039;&#039;&#039;[[Task Runner]]&#039;&#039;&#039; – Automates development workflows.&lt;br /&gt;
* &#039;&#039;&#039;[[Debugger]]&#039;&#039;&#039; – Inspection and control of execution.&lt;br /&gt;
* &#039;&#039;&#039;[[Profiler]]&#039;&#039;&#039; – Performance measurement system.&lt;br /&gt;
* &#039;&#039;&#039;[[Linter]]&#039;&#039;&#039; – Static analysis and style enforcement.&lt;br /&gt;
* &#039;&#039;&#039;[[Formatter]]&#039;&#039;&#039; – Code layout normalization system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Data &amp;amp; State Systems ==&lt;br /&gt;
&lt;br /&gt;
(How information is *stored and managed*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Database]]&#039;&#039;&#039; – Persistent data storage system.&lt;br /&gt;
* &#039;&#039;&#039;[[DBMS]]&#039;&#039;&#039; – Software managing databases.&lt;br /&gt;
* &#039;&#039;&#039;[[Data Store]]&#039;&#039;&#039; – Generalized storage system.&lt;br /&gt;
* &#039;&#039;&#039;[[Cache]]&#039;&#039;&#039; – Fast, transient data system.&lt;br /&gt;
* &#039;&#039;&#039;[[Message Queue]]&#039;&#039;&#039; – Asynchronous communication system.&lt;br /&gt;
* &#039;&#039;&#039;[[Event Bus]]&#039;&#039;&#039; – Event-driven messaging system.&lt;br /&gt;
* &#039;&#039;&#039;[[State Management System]]&#039;&#039;&#039; – Controls application state.&lt;br /&gt;
* &#039;&#039;&#039;[[Filesystem]]&#039;&#039;&#039; – Persistent hierarchical storage system.&lt;br /&gt;
* &#039;&#039;&#039;[[Indexing System]]&#039;&#039;&#039; – Accelerated lookup structures.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Distributed &amp;amp; Service Systems ==&lt;br /&gt;
&lt;br /&gt;
(How software *communicates*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Client–Server System]]&#039;&#039;&#039; - A model where a client (user interface or device) sends requests and a server processes those requests and returns responses.&lt;br /&gt;
* &#039;&#039;&#039;[[Distributed System]]&#039;&#039;&#039; - A collection of multiple computers that work together over a network to function as a single system.&lt;br /&gt;
* &#039;&#039;&#039;[[Microservices Architecture]]&#039;&#039;&#039; - An approach where an application is built as a set of small, independent services that communicate over APIs.&lt;br /&gt;
* &#039;&#039;&#039;[[Monolith]]&#039;&#039;&#039; - A single, tightly coupled application where all functionality is contained in one codebase and deployed as one unit.&lt;br /&gt;
* &#039;&#039;&#039;[[Service Mesh]]&#039;&#039;&#039; - An infrastructure layer that manages communication between services, handling routing, security, and observability.&lt;br /&gt;
* &#039;&#039;&#039;[[Backend System]]&#039;&#039;&#039; - The server-side components that handle business logic, data processing, and storage.&lt;br /&gt;
* &#039;&#039;&#039;[[Frontend System]]&#039;&#039;&#039; - The user-facing interface that interacts with the backend, such as web apps or desktop interfaces.&lt;br /&gt;
* &#039;&#039;&#039;[[Control Plane / Data Plane]]&#039;&#039;&#039; - The control plane makes decisions and manages the system, while the data plane executes those decisions and handles real work.&lt;br /&gt;
* &#039;&#039;&#039;[[Load Balancer]]&#039;&#039;&#039; - A system that distributes incoming requests across multiple servers to improve performance and reliability.&lt;br /&gt;
* &#039;&#039;&#039;[[Gateway]]&#039;&#039;&#039; - A centralized entry point that routes, filters, and enforces policies on incoming requests to a system.&lt;br /&gt;
* &#039;&#039;&#039;[[Reverse Proxy]]&#039;&#039;&#039; - A server that sits in front of backend services and forwards client requests while hiding internal system details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Deployment &amp;amp; Operations Systems ==&lt;br /&gt;
&lt;br /&gt;
(How software *lives in the world*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Operating System]]&#039;&#039;&#039; - Software that manages hardware resources and provides a runtime environment for applications.&lt;br /&gt;
* &#039;&#039;&#039;[[Infrastructure]]&#039;&#039;&#039; - The underlying physical or virtual resources such as servers, storage, and networking that support systems.&lt;br /&gt;
* &#039;&#039;&#039;[[Cloud Platform]]&#039;&#039;&#039; - A system that provides on-demand access to computing resources and services through abstraction and automation.&lt;br /&gt;
* &#039;&#039;&#039;[[Orchestration System]]&#039;&#039;&#039; - Software that automates deployment, scaling, scheduling, and management of applications (e.g., Kubernetes).&lt;br /&gt;
* &#039;&#039;&#039;[[CI/CD Pipeline]]&#039;&#039;&#039; - An automated workflow that builds, tests, secures, and deploys software from source code to running systems.&lt;br /&gt;
* &#039;&#039;&#039;[[Monitoring System]]&#039;&#039;&#039; - A system that tracks performance and health metrics of infrastructure and applications.&lt;br /&gt;
* &#039;&#039;&#039;[[Logging System]]&#039;&#039;&#039; - A system that collects and stores event records and system messages for debugging and auditing.&lt;br /&gt;
* &#039;&#039;&#039;[[Telemetry System]]&#039;&#039;&#039; - A system that gathers and analyzes metrics, logs, and traces to understand system behavior.&lt;br /&gt;
* &#039;&#039;&#039;[[Configuration Management System]]&#039;&#039;&#039; - A system that defines, manages, and enforces configuration settings across environments.&lt;br /&gt;
* &#039;&#039;&#039;[[Secrets Management System]]&#039;&#039;&#039; - A system that securely stores and controls access to sensitive data like passwords, keys, and tokens.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Security &amp;amp; Governance Systems ==&lt;br /&gt;
&lt;br /&gt;
(How systems *protect themselves*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Authentication System]]&#039;&#039;&#039; - A system that verifies the identity of a user or service.&lt;br /&gt;
* &#039;&#039;&#039;[[Authorization System]]&#039;&#039;&#039; - A system that determines what actions an authenticated user or service is allowed to perform.&lt;br /&gt;
* &#039;&#039;&#039;[[Identity Management System]]&#039;&#039;&#039; - A system that manages user identities, credentials, and access lifecycle.&lt;br /&gt;
* &#039;&#039;&#039;[[Access Control System]]&#039;&#039;&#039; - A system that enforces rules governing access to resources.&lt;br /&gt;
* &#039;&#039;&#039;[[Key Management System]]&#039;&#039;&#039; - A system that creates, stores, and manages cryptographic keys.&lt;br /&gt;
* &#039;&#039;&#039;[[Policy Engine]]&#039;&#039;&#039; - A system that evaluates rules and policies to make access or operational decisions.&lt;br /&gt;
* &#039;&#039;&#039;[[Permission Model]]&#039;&#039;&#039; - A framework that defines how permissions are structured and assigned (e.g., roles or attributes).&lt;br /&gt;
* &#039;&#039;&#039;[[Trust Boundary]]&#039;&#039;&#039; - A point in a system where trust assumptions change and additional security controls are required.&lt;br /&gt;
* &#039;&#039;&#039;[[Security Sandbox]]&#039;&#039;&#039; - An isolated environment used to safely execute untrusted or restricted code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== AI-Specific System Terms ==&lt;br /&gt;
&lt;br /&gt;
(Modern AI “systems” framing)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Model Architecture]]&#039;&#039;&#039; - The structure and design of a machine learning model, such as a transformer or neural network.&lt;br /&gt;
* &#039;&#039;&#039;[[Inference Engine]]&#039;&#039;&#039; - The system that runs a trained model to generate predictions or outputs.&lt;br /&gt;
* &#039;&#039;&#039;[[Training Pipeline]]&#039;&#039;&#039; - The process and infrastructure used to train machine learning models from data.&lt;br /&gt;
* &#039;&#039;&#039;[[Evaluation Framework]]&#039;&#039;&#039; - A system for measuring and validating model performance and reliability.&lt;br /&gt;
* &#039;&#039;&#039;[[RAG Pipeline]]&#039;&#039;&#039; - A system that retrieves relevant data and feeds it into a model to improve generated outputs.&lt;br /&gt;
* &#039;&#039;&#039;[[Vector Database]]&#039;&#039;&#039; - A database optimized for storing and searching embeddings for semantic similarity.&lt;br /&gt;
* &#039;&#039;&#039;[[Agent System]]&#039;&#039;&#039; - A system where an AI model can take actions, use tools, and execute tasks autonomously.&lt;br /&gt;
* &#039;&#039;&#039;[[Orchestration Graph]]&#039;&#039;&#039; - A structured representation of workflows and dependencies between components or steps.&lt;br /&gt;
* &#039;&#039;&#039;[[Prompting Framework]]&#039;&#039;&#039; - A system for managing and structuring prompts, templates, and context for AI models.&lt;br /&gt;
* &#039;&#039;&#039;[[Multi-Agent System]]&#039;&#039;&#039; - A system where multiple AI agents collaborate or coordinate to complete tasks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Computer Science]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Computer_Science&amp;diff=4152</id>
		<title>Computer Science</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Computer_Science&amp;diff=4152"/>
		<updated>2026-04-04T08:10:30Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Created page with &amp;quot;== Conceptual &amp;amp; Structural Systems ==  (How software is *organized* at a high level)  * &amp;#039;&amp;#039;&amp;#039;Architecture&amp;#039;&amp;#039;&amp;#039; – The fundamental structure of a system: components, responsibilities, and relationships. * &amp;#039;&amp;#039;&amp;#039;Design Pattern&amp;#039;&amp;#039;&amp;#039; – Reusable solutions to recurring structural or behavioral problems. * &amp;#039;&amp;#039;&amp;#039;System Design&amp;#039;&amp;#039;&amp;#039; – End-to-end planning of components, data flow, scaling, and failure modes. * &amp;#039;&amp;#039;&amp;#039;Platform&amp;#039;&amp;#039;&amp;#039; – A base system upon which other software or se...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Conceptual &amp;amp; Structural Systems ==&lt;br /&gt;
&lt;br /&gt;
(How software is *organized* at a high level)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Architecture]]&#039;&#039;&#039; – The fundamental structure of a system: components, responsibilities, and relationships.&lt;br /&gt;
* &#039;&#039;&#039;[[Design Pattern]]&#039;&#039;&#039; – Reusable solutions to recurring structural or behavioral problems.&lt;br /&gt;
* &#039;&#039;&#039;[[System Design]]&#039;&#039;&#039; – End-to-end planning of components, data flow, scaling, and failure modes.&lt;br /&gt;
* &#039;&#039;&#039;[[Platform]]&#039;&#039;&#039; – A base system upon which other software or services are built.&lt;br /&gt;
* &#039;&#039;&#039;[[Ecosystem]]&#039;&#039;&#039; – Interacting tools, libraries, services, and users around a platform.&lt;br /&gt;
* &#039;&#039;&#039;[[Stack]]&#039;&#039;&#039; – A layered collection of technologies used together (e.g., LAMP, MERN).&lt;br /&gt;
* &#039;&#039;&#039;[[Subsystem]]&#039;&#039;&#039; – A self-contained functional unit within a larger system.&lt;br /&gt;
* &#039;&#039;&#039;[[Module]]&#039;&#039;&#039; – A discrete, replaceable unit of functionality.&lt;br /&gt;
* &#039;&#039;&#039;[[Component]]&#039;&#039;&#039; – A deployable or reusable unit with a defined interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Code Reuse &amp;amp; Capability Systems ==&lt;br /&gt;
&lt;br /&gt;
(Things you *build with*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Framework]]&#039;&#039;&#039; – Opinionated structure that controls application flow.&lt;br /&gt;
* &#039;&#039;&#039;[[Library]]&#039;&#039;&#039; – Reusable code invoked by the developer.&lt;br /&gt;
* &#039;&#039;&#039;[[SDK (Software Development Kit)]]&#039;&#039;&#039; – Tools, libraries, docs, and examples for a platform.&lt;br /&gt;
* &#039;&#039;&#039;[[API]]&#039;&#039;&#039; – A formal interface for interacting with a system or service.&lt;br /&gt;
* &#039;&#039;&#039;[[Middleware]]&#039;&#039;&#039; – Software that connects or mediates between systems.&lt;br /&gt;
* &#039;&#039;&#039;[[Runtime]]&#039;&#039;&#039; – The environment in which code executes.&lt;br /&gt;
* &#039;&#039;&#039;[[Plugin System]]&#039;&#039;&#039; – Mechanism for extending a host application.&lt;br /&gt;
* &#039;&#039;&#039;[[Extension]]&#039;&#039;&#039; – Add-on module that enhances functionality.&lt;br /&gt;
* &#039;&#039;&#039;[[Package]]&#039;&#039;&#039; – A distributable unit of code.&lt;br /&gt;
* &#039;&#039;&#039;[[Dependency]]&#039;&#039;&#039; – External code required by a system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Execution &amp;amp; Environment Systems ==&lt;br /&gt;
&lt;br /&gt;
(Where software *runs*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Environment]]&#039;&#039;&#039; – A configured context (variables, paths, services).&lt;br /&gt;
* &#039;&#039;&#039;[[Runtime Environment]]&#039;&#039;&#039; – OS + libraries + execution context.&lt;br /&gt;
* &#039;&#039;&#039;[[Virtual Environment]]&#039;&#039;&#039; – Isolated dependency space (e.g., Python venv).&lt;br /&gt;
* &#039;&#039;&#039;[[Container]]&#039;&#039;&#039; – Lightweight, isolated execution unit (e.g., Docker).&lt;br /&gt;
* &#039;&#039;&#039;[[Virtual Machine (VM)]]&#039;&#039;&#039; – Fully virtualized OS instance.&lt;br /&gt;
* &#039;&#039;&#039;[[Sandbox]]&#039;&#039;&#039; – Restricted execution environment for safety.&lt;br /&gt;
* &#039;&#039;&#039;[[Host System]]&#039;&#039;&#039; – The machine or OS running workloads.&lt;br /&gt;
* &#039;&#039;&#039;[[Target Environment]]&#039;&#039;&#039; – Intended deployment context.&lt;br /&gt;
* &#039;&#039;&#039;[[Emulator]]&#039;&#039;&#039; – Software that mimics another system.&lt;br /&gt;
* &#039;&#039;&#039;[[Interpreter]]&#039;&#039;&#039; – Executes code directly.&lt;br /&gt;
* &#039;&#039;&#039;[[Compiler Toolchain]]&#039;&#039;&#039; – System that translates source to binaries.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Development &amp;amp; Tooling Systems ==&lt;br /&gt;
&lt;br /&gt;
(How developers *work*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[IDE]]&#039;&#039;&#039; – (Integrated Development Environment) Unified coding, debugging, and tooling system.&lt;br /&gt;
* &#039;&#039;&#039;[[Editor]]&#039;&#039;&#039; – Code authoring environment.&lt;br /&gt;
* &#039;&#039;&#039;[[Build System]]&#039;&#039;&#039; – Automates compilation, linking, and packaging.&lt;br /&gt;
* &#039;&#039;&#039;[[Toolchain]]&#039;&#039;&#039; – Collection of compilers, linkers, debuggers, etc.&lt;br /&gt;
* &#039;&#039;&#039;[[Package Manager]]&#039;&#039;&#039; – Manages dependencies and versions.&lt;br /&gt;
* &#039;&#039;&#039;[[Task Runner]]&#039;&#039;&#039; – Automates development workflows.&lt;br /&gt;
* &#039;&#039;&#039;[[Debugger]]&#039;&#039;&#039; – Inspection and control of execution.&lt;br /&gt;
* &#039;&#039;&#039;[[Profiler]]&#039;&#039;&#039; – Performance measurement system.&lt;br /&gt;
* &#039;&#039;&#039;[[Linter]]&#039;&#039;&#039; – Static analysis and style enforcement.&lt;br /&gt;
* &#039;&#039;&#039;[[Formatter]]&#039;&#039;&#039; – Code layout normalization system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Data &amp;amp; State Systems ==&lt;br /&gt;
&lt;br /&gt;
(How information is *stored and managed*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Database]]&#039;&#039;&#039; – Persistent data storage system.&lt;br /&gt;
* &#039;&#039;&#039;[[DBMS]]&#039;&#039;&#039; – Software managing databases.&lt;br /&gt;
* &#039;&#039;&#039;[[Data Store]]&#039;&#039;&#039; – Generalized storage system.&lt;br /&gt;
* &#039;&#039;&#039;[[Cache]]&#039;&#039;&#039; – Fast, transient data system.&lt;br /&gt;
* &#039;&#039;&#039;[[Message Queue]]&#039;&#039;&#039; – Asynchronous communication system.&lt;br /&gt;
* &#039;&#039;&#039;[[Event Bus]]&#039;&#039;&#039; – Event-driven messaging system.&lt;br /&gt;
* &#039;&#039;&#039;[[State Management System]]&#039;&#039;&#039; – Controls application state.&lt;br /&gt;
* &#039;&#039;&#039;[[Filesystem]]&#039;&#039;&#039; – Persistent hierarchical storage system.&lt;br /&gt;
* &#039;&#039;&#039;[[Indexing System]]&#039;&#039;&#039; – Accelerated lookup structures.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Distributed &amp;amp; Service Systems ==&lt;br /&gt;
&lt;br /&gt;
(How software *communicates*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Client–Server System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Distributed System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Microservices Architecture]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Monolith]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Service Mesh]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Backend System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Frontend System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Control Plane / Data Plane]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Load Balancer]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Gateway]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Reverse Proxy]]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Deployment &amp;amp; Operations Systems ==&lt;br /&gt;
&lt;br /&gt;
(How software *lives in the world*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Operating System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Infrastructure]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Cloud Platform]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Orchestration System]]&#039;&#039;&#039; (e.g., Kubernetes)&lt;br /&gt;
* &#039;&#039;&#039;[[CI/CD Pipeline]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Monitoring System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Logging System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Telemetry System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Configuration Management System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Secrets Management System]]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Security &amp;amp; Governance Systems ==&lt;br /&gt;
&lt;br /&gt;
(How systems *protect themselves*)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Authentication System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Authorization System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Identity Management System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Access Control System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Key Management System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Policy Engine]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Permission Model]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Trust Boundary]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Security Sandbox]]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== AI-Specific System Terms (given your recent work) ==&lt;br /&gt;
&lt;br /&gt;
(Modern “systems” framing)&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;[[Model Architecture]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Inference Engine]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Training Pipeline]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Evaluation Framework]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[RAG Pipeline]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Vector Database]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Agent System]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Orchestration Graph]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Prompting Framework]]&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;[[Multi-Agent System]]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Computer Science]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4151</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4151"/>
		<updated>2025-11-06T06:52:03Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* A Correlated Trajectory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[File:Displacement Cross Correlation.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[File:Xcorr Acorr.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[File:Brownian Motion Cross Correlation.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Additional Resources=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html Wolfram Alpha - Random Walk]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus - MSD Theory]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories MSD MATLAB Toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active Diffusion Positions in the Nucleus]&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:Brownian_Motion_Cross_Correlation.png&amp;diff=4150</id>
		<title>File:Brownian Motion Cross Correlation.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:Brownian_Motion_Cross_Correlation.png&amp;diff=4150"/>
		<updated>2025-11-06T06:51:11Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4149</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4149"/>
		<updated>2025-11-06T06:47:09Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* MATLAB Tricks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[File:Displacement Cross Correlation.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[File:Xcorr Acorr.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Additional Resources=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html Wolfram Alpha - Random Walk]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus - MSD Theory]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories MSD MATLAB Toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active Diffusion Positions in the Nucleus]&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:Xcorr_Acorr.png&amp;diff=4148</id>
		<title>File:Xcorr Acorr.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:Xcorr_Acorr.png&amp;diff=4148"/>
		<updated>2025-11-06T06:46:32Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4147</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4147"/>
		<updated>2025-11-06T06:39:00Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Cross Correlation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[File:Displacement Cross Correlation.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Additional Resources=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html Wolfram Alpha - Random Walk]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus - MSD Theory]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories MSD MATLAB Toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active Diffusion Positions in the Nucleus]&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:Displacement_Cross_Correlation.png&amp;diff=4146</id>
		<title>File:Displacement Cross Correlation.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:Displacement_Cross_Correlation.png&amp;diff=4146"/>
		<updated>2025-11-06T06:38:10Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_06.png&amp;diff=4145</id>
		<title>File:FancyStatistics 06.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_06.png&amp;diff=4145"/>
		<updated>2025-11-06T06:35:35Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:FancyStatistics 06.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_04.png&amp;diff=4144</id>
		<title>File:FancyStatistics 04.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_04.png&amp;diff=4144"/>
		<updated>2025-11-06T06:33:08Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:FancyStatistics 04.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_03.png&amp;diff=4143</id>
		<title>File:FancyStatistics 03.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_03.png&amp;diff=4143"/>
		<updated>2025-11-06T06:32:05Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:FancyStatistics 03.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_02.png&amp;diff=4142</id>
		<title>File:FancyStatistics 02.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_02.png&amp;diff=4142"/>
		<updated>2025-11-06T06:31:17Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:FancyStatistics 02.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_01.png&amp;diff=4141</id>
		<title>File:FancyStatistics 01.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:FancyStatistics_01.png&amp;diff=4141"/>
		<updated>2025-11-06T06:29:58Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:FancyStatistics 01.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4140</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4140"/>
		<updated>2024-06-23T10:12:30Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Additional Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Additional Resources=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html Wolfram Alpha - Random Walk]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus - MSD Theory]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories MSD MATLAB Toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active Diffusion Positions in the Nucleus]&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4139</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4139"/>
		<updated>2024-06-23T10:11:45Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Additional Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Additional Resources=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html Wolfram Alpha - Random Walk]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus - MSD Theory]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories MSD MATLAB Toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active Diffusion Positions in the Nucleus]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4138</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4138"/>
		<updated>2024-06-23T10:11:23Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Additional Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Additional Resources=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html Wolfram Alpha - Random Walk]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus - MSD Theory]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories MSD MATLAB Toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active Diffusion Positions in the Nucleus]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4137</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4137"/>
		<updated>2024-06-23T10:09:36Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* notes &amp;amp; articles */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4136</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4136"/>
		<updated>2024-06-23T10:08:46Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* 100 Years of BIO Lab Data in 1 Second */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4135</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4135"/>
		<updated>2024-06-23T10:07:24Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Mean Squared Displacement (MSD) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4134</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4134"/>
		<updated>2024-06-23T10:06:16Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Fancy Statistics and Plots */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Motion Statistics and Plots=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4133</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4133"/>
		<updated>2024-06-23T10:05:04Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Simulating Multiple Particles */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4132</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4132"/>
		<updated>2024-06-23T10:03:30Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Simulating Multiple Particles */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_08.png&amp;diff=4131</id>
		<title>File:SingleParticleSimulations 08.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_08.png&amp;diff=4131"/>
		<updated>2024-06-23T10:00:43Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 08.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:MultipleParticleSimulation_03.png&amp;diff=4130</id>
		<title>File:MultipleParticleSimulation 03.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:MultipleParticleSimulation_03.png&amp;diff=4130"/>
		<updated>2024-06-23T09:59:56Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:MultipleParticleSimulation 03.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:MultipleParticleSimulation_02.png&amp;diff=4129</id>
		<title>File:MultipleParticleSimulation 02.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:MultipleParticleSimulation_02.png&amp;diff=4129"/>
		<updated>2024-06-23T09:59:44Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:MultipleParticleSimulation 02.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:MultipleParticleSimulation_01.png&amp;diff=4128</id>
		<title>File:MultipleParticleSimulation 01.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:MultipleParticleSimulation_01.png&amp;diff=4128"/>
		<updated>2024-06-23T09:59:27Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:MultipleParticleSimulation 01.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_09.png&amp;diff=4127</id>
		<title>File:SingleParticleSimulations 09.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_09.png&amp;diff=4127"/>
		<updated>2024-06-23T09:59:16Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 09.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_07.png&amp;diff=4126</id>
		<title>File:SingleParticleSimulations 07.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_07.png&amp;diff=4126"/>
		<updated>2024-06-23T09:59:04Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 07.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4125</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4125"/>
		<updated>2024-06-23T09:43:15Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Estimating D from the Simulated Data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.22 E-13&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_07.png&amp;diff=4124</id>
		<title>File:SingleParticleSimulations 07.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_07.png&amp;diff=4124"/>
		<updated>2024-06-23T09:41:45Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 07.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_06.png&amp;diff=4123</id>
		<title>File:SingleParticleSimulations 06.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_06.png&amp;diff=4123"/>
		<updated>2024-06-23T09:41:31Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 06.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4122</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4122"/>
		<updated>2024-06-23T09:39:32Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Simulating Particle Trajectories */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% continuing from above...&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.2192e-013&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4121</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4121"/>
		<updated>2024-06-23T09:37:17Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Theoretical Value of D */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.2192e-013&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=MediaWiki:Common.css&amp;diff=4120</id>
		<title>MediaWiki:Common.css</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=MediaWiki:Common.css&amp;diff=4120"/>
		<updated>2024-06-23T09:35:56Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;/* CSS placed here will be applied to all skins */&lt;br /&gt;
&lt;br /&gt;
/* Applies to Syntax Highlight Extension */&lt;br /&gt;
.mw-highlight pre {&lt;br /&gt;
  font-family: &amp;quot;Courier&amp;quot;, monospace;&lt;br /&gt;
  font-size: 90%;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@import url( &#039;https://fonts.googleapis.com/css?family=Noto+Sans|Noto+Serif &#039;)&lt;br /&gt;
&lt;br /&gt;
/* Changes the default font used for MediaWiki to Noto Sans (does not include headings or monospaced text): */&lt;br /&gt;
body {&lt;br /&gt;
  font-family: &amp;quot;Noto Sans&amp;quot;, sans-serif;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* Changes the default font used for MediaWiki headings to Noto Serif: */&lt;br /&gt;
#content h1, &lt;br /&gt;
#content h2 {&lt;br /&gt;
  font-family: &amp;quot;Noto Serif&amp;quot;, serif;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Removes external link icon */&lt;br /&gt;
#content a[href ^=&amp;quot;http://&amp;quot;].external {background: center right no-repeat; padding-right: 1px;}&lt;br /&gt;
#content a[href ^=&amp;quot;https://&amp;quot;].external {background: center right no-repeat; padding-right: 1px;}&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=MediaWiki:Common.css&amp;diff=4119</id>
		<title>MediaWiki:Common.css</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=MediaWiki:Common.css&amp;diff=4119"/>
		<updated>2024-06-23T09:35:16Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;/* CSS placed here will be applied to all skins */&lt;br /&gt;
&lt;br /&gt;
/* Applies to Syntax Highlight Extension */&lt;br /&gt;
.mw-highlight pre {&lt;br /&gt;
  font-family: &amp;quot;Courier New&amp;quot;, monospace;&lt;br /&gt;
  font-size: 90%;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@import url( &#039;https://fonts.googleapis.com/css?family=Noto+Sans|Noto+Serif &#039;)&lt;br /&gt;
&lt;br /&gt;
/* Changes the default font used for MediaWiki to Noto Sans (does not include headings or monospaced text): */&lt;br /&gt;
body {&lt;br /&gt;
  font-family: &amp;quot;Noto Sans&amp;quot;, sans-serif;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* Changes the default font used for MediaWiki headings to Noto Serif: */&lt;br /&gt;
#content h1, &lt;br /&gt;
#content h2 {&lt;br /&gt;
  font-family: &amp;quot;Noto Serif&amp;quot;, serif;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Removes external link icon */&lt;br /&gt;
#content a[href ^=&amp;quot;http://&amp;quot;].external {background: center right no-repeat; padding-right: 1px;}&lt;br /&gt;
#content a[href ^=&amp;quot;https://&amp;quot;].external {background: center right no-repeat; padding-right: 1px;}&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=MediaWiki:Common.css&amp;diff=4118</id>
		<title>MediaWiki:Common.css</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=MediaWiki:Common.css&amp;diff=4118"/>
		<updated>2024-06-23T09:34:41Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;/* CSS placed here will be applied to all skins */&lt;br /&gt;
&lt;br /&gt;
/* Applies to SyntaxHighlight Extension */&lt;br /&gt;
.mw-highlight pre {&lt;br /&gt;
  font-family: &amp;quot;Courier New&amp;quot;, monospace;&lt;br /&gt;
  font-size: 90%;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@import url( &#039;https://fonts.googleapis.com/css?family=Noto+Sans|Noto+Serif &#039;)&lt;br /&gt;
&lt;br /&gt;
/* Changes the default font used for MediaWiki to Noto Sans (does not include headings or monospaced text): */&lt;br /&gt;
body {&lt;br /&gt;
  font-family: &amp;quot;Noto Sans&amp;quot;, sans-serif;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* Changes the default font used for MediaWiki headings to Noto Serif: */&lt;br /&gt;
#content h1, &lt;br /&gt;
#content h2 {&lt;br /&gt;
  font-family: &amp;quot;Noto Serif&amp;quot;, serif;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Removes external link icon */&lt;br /&gt;
#content a[href ^=&amp;quot;http://&amp;quot;].external {background: center right no-repeat; padding-right: 1px;}&lt;br /&gt;
#content a[href ^=&amp;quot;https://&amp;quot;].external {background: center right no-repeat; padding-right: 1px;}&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=MediaWiki:Common.css&amp;diff=4117</id>
		<title>MediaWiki:Common.css</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=MediaWiki:Common.css&amp;diff=4117"/>
		<updated>2024-06-23T09:31:55Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;/* CSS placed here will be applied to all skins */&lt;br /&gt;
&lt;br /&gt;
@import url( &#039;https://fonts.googleapis.com/css?family=Noto+Sans|Noto+Serif &#039;)&lt;br /&gt;
&lt;br /&gt;
/* Changes the default font used for MediaWiki to Noto Sans (does not include headings or monospaced text): */&lt;br /&gt;
body {&lt;br /&gt;
  font-family: &amp;quot;Noto Sans&amp;quot;, sans-serif;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* Changes the default font used for MediaWiki headings to Noto Serif: */&lt;br /&gt;
#content h1, &lt;br /&gt;
#content h2 {&lt;br /&gt;
  font-family: &amp;quot;Noto Serif&amp;quot;, serif;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Removes external link icon */&lt;br /&gt;
#content a[href ^=&amp;quot;http://&amp;quot;].external {background: center right no-repeat; padding-right: 1px;}&lt;br /&gt;
#content a[href ^=&amp;quot;https://&amp;quot;].external {background: center right no-repeat; padding-right: 1px;}&lt;br /&gt;
&lt;br /&gt;
/* CSS placed here will be applied to all skins */&lt;br /&gt;
.mw-highlight pre {&lt;br /&gt;
  font-size: 90%;&lt;br /&gt;
  font-family: monospace;&lt;br /&gt;
}&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4116</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4116"/>
		<updated>2024-06-23T09:25:09Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* A more realistic particle -- Getting the Units Right */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r      = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T     = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
N = 1000;&lt;br /&gt;
&lt;br /&gt;
r    = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T    = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.2192e-013&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4115</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4115"/>
		<updated>2024-06-23T09:20:01Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Simulating Particle Trajectories */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r      = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T     = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
%             Two Dimensional Realistic Brownian Motion&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&lt;br /&gt;
%-------------############################------------------%&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.2192e-013&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_05.png&amp;diff=4114</id>
		<title>File:SingleParticleSimulations 05.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_05.png&amp;diff=4114"/>
		<updated>2024-06-23T09:18:56Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 05.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_04.png&amp;diff=4113</id>
		<title>File:SingleParticleSimulations 04.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_04.png&amp;diff=4113"/>
		<updated>2024-06-23T09:14:05Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 04.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_03.png&amp;diff=4112</id>
		<title>File:SingleParticleSimulations 03.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_03.png&amp;diff=4112"/>
		<updated>2024-06-23T09:13:52Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 03.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_02.png&amp;diff=4111</id>
		<title>File:SingleParticleSimulations 02.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_02.png&amp;diff=4111"/>
		<updated>2024-06-23T09:13:39Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 02.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_01.png&amp;diff=4110</id>
		<title>File:SingleParticleSimulations 01.png</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=File:SingleParticleSimulations_01.png&amp;diff=4110"/>
		<updated>2024-06-23T09:09:12Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Bradley Monk uploaded a new version of File:SingleParticleSimulations 01.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4109</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4109"/>
		<updated>2024-06-23T09:03:48Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
=Brownian Motion MATLAB Code=&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r      = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T     = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
%             Two Dimensional Realistic Brownian Motion&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&lt;br /&gt;
%-------------############################------------------%&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.2192e-013&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4108</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4108"/>
		<updated>2024-06-23T09:03:01Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Brownian Motion MATLAB Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
==Brownian Motion MATLAB Code==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r      = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T     = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
%             Two Dimensional Realistic Brownian Motion&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&lt;br /&gt;
%-------------############################------------------%&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.2192e-013&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4107</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4107"/>
		<updated>2024-06-23T09:02:22Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
==Brownian Motion MATLAB Code==&lt;br /&gt;
&lt;br /&gt;
Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r      = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T     = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
%             Two Dimensional Realistic Brownian Motion&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&lt;br /&gt;
%-------------############################------------------%&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.2192e-013&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion_MATLAB_Code&amp;diff=4106</id>
		<title>Brownian Motion MATLAB Code</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion_MATLAB_Code&amp;diff=4106"/>
		<updated>2024-06-23T09:00:12Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
function [tracks] = BrownianMotion()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% STARTING PARAMETERS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
D = .3;                     % Diffusion Rate&lt;br /&gt;
Ds = .1;                    % Diffusion Scalar (Ds = Dn)&lt;br /&gt;
Dn = D/(D/Ds);              % new D after scaling L&lt;br /&gt;
d = 2;                      % dimensions&lt;br /&gt;
dT = 1;                     % time step&lt;br /&gt;
k = sqrt(d*D);	            % stdev of D&#039;s step size distribution&lt;br /&gt;
MSD = 2*d*D;                % mean squared displacement&lt;br /&gt;
L = sqrt(2*d*D);            % average diagonal (2D) step size&lt;br /&gt;
Lx = L/sqrt(2);             % average linear (1D) step size&lt;br /&gt;
Ls = 1/sqrt(D/Ds);          % scales Lx values for Dn&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MSDtest = [1 0 0];			% test: D, Dn, or L&lt;br /&gt;
Scale = 1/10;				% scale of model&lt;br /&gt;
Ndots = 100;&lt;br /&gt;
Nsteps = Ndots;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
xyl = ones(2,Ndots);&lt;br /&gt;
xyds = ones(2,Ndots);&lt;br /&gt;
lims = ((D+1)^2)*10;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% LIVE PARTICLE DIFFUSION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
for t = 1:Nsteps&lt;br /&gt;
&lt;br /&gt;
    xyds = STEPxyds(Ndots, k);&lt;br /&gt;
	[xyl] = AMPARSTEP(Ndots, xyds, xyl);&lt;br /&gt;
	MAINPLOT(xyl, lims);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD RANDOM STEPS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
tracks = cell(Ndots, 1);&lt;br /&gt;
&lt;br /&gt;
stepN = 1;&lt;br /&gt;
for t = 1:Nsteps &lt;br /&gt;
&lt;br /&gt;
	xyds = STEPxyds(Ndots, k);&lt;br /&gt;
	[xyl] = AMPARSTEP(Ndots, xyds, xyl);&lt;br /&gt;
    [tracks] = MSDfun(stepN, Nsteps, tracks, xyds);&lt;br /&gt;
&lt;br /&gt;
stepN = stepN+1;&lt;br /&gt;
end&lt;br /&gt;
MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD UNIFORM STEPS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
stepN = 1;&lt;br /&gt;
for t = 1:Nsteps &lt;br /&gt;
&lt;br /&gt;
xyds = stepsize(Ndots, Lx);&lt;br /&gt;
		&lt;br /&gt;
	[xyl xyds] = MSDAMPARSTEP(Ndots, xyds, xyl, Ls);&lt;br /&gt;
    [tracks] = MSDfun(stepN, Nsteps, tracks, xyds);&lt;br /&gt;
&lt;br /&gt;
stepN = stepN+1;&lt;br /&gt;
end&lt;br /&gt;
MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
%%&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% STEP SIZE GENERATOR&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function xyds = STEPxyds(Ndots, k)&lt;br /&gt;
&lt;br /&gt;
    xyds = (k * randn(2,Ndots));&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MOVE PARTICLES MAIN FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [xyl] = AMPARSTEP(Ndots, xyds, xyl)&lt;br /&gt;
	&lt;br /&gt;
	for j = 1:Ndots&lt;br /&gt;
        xyl(:,j) = xyl(:,j)+xyds(:,j);&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% LIVE DIFFUSION PLOT&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [] = MAINPLOT(xyl, lims)&lt;br /&gt;
&lt;br /&gt;
xlim = [-lims lims];&lt;br /&gt;
ylim = [-lims lims];&lt;br /&gt;
zlim = [-5 5];&lt;br /&gt;
&lt;br /&gt;
figure(1)&lt;br /&gt;
subplot(2,1,1), &lt;br /&gt;
AMPARPlot = gscatter(xyl(1,:),xyl(2,:));&lt;br /&gt;
axis([xlim, ylim]);&lt;br /&gt;
set(AMPARPlot,&#039;marker&#039;,&#039;.&#039;,&#039;markersize&#039;,[6],&#039;color&#039;,[1 0 0])&lt;br /&gt;
&lt;br /&gt;
figure(1);&lt;br /&gt;
subplot(2,1,2), &lt;br /&gt;
gscatter(xyl(1,:),xyl(2,:)); view(20, 30);&lt;br /&gt;
axis normal;&lt;br /&gt;
grid off&lt;br /&gt;
axis([xlim, ylim, zlim]);&lt;br /&gt;
set(gca, &#039;Box&#039;, &#039;on&#039;);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MANUAL STEP SIZE FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function xyds = stepsize(Ndots, Lx)&lt;br /&gt;
&lt;br /&gt;
   Lx(1:2,1:Ndots) = Lx;&lt;br /&gt;
   xyd = randi([0 1],Ndots,2)&#039;;&lt;br /&gt;
   xyd(xyd == 0) = -1;&lt;br /&gt;
   xyds = (Lx.*xyd);&lt;br /&gt;
   &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD SCALED STEPS FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [xyl xyds] = MSDAMPARSTEP(Ndots, xyds, xyl, Ls)&lt;br /&gt;
	&lt;br /&gt;
	for j = 1:Ndots&lt;br /&gt;
        xyds(:,j) = xyds(:,j)*Ls;&lt;br /&gt;
        xyl(:,j) = xyl(:,j)+xyds(:,j);&lt;br /&gt;
	end	&lt;br /&gt;
	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD TRACKS GENERATOR&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [tracks] = MSDfun(stepN, Nsteps, tracks, xyds)&lt;br /&gt;
&lt;br /&gt;
    time = (0:Nsteps-1)&#039;;&lt;br /&gt;
    xymsd = xyds&#039;;&lt;br /&gt;
    xymsd = cumsum(xymsd,1);&lt;br /&gt;
    tracks{stepN} = [time xymsd];&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD TRACKS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [] = MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest)&lt;br /&gt;
&lt;br /&gt;
SPACE_UNITS = &#039;µm&#039;;&lt;br /&gt;
TIME_UNITS = &#039;s&#039;;&lt;br /&gt;
N_PARTICLES = Ndots;&lt;br /&gt;
N_TIME_STEPS = Nsteps;&lt;br /&gt;
N_DIM = 2;&lt;br /&gt;
&lt;br /&gt;
oD = D;				% raw		µm^2/s&lt;br /&gt;
D  = D*Scale;       % to-scale	µm^2/s&lt;br /&gt;
&lt;br /&gt;
oDn = Dn;			% raw		µm^2/s&lt;br /&gt;
Dn = Dn*Scale;		% to-scale	µm^2/s&lt;br /&gt;
&lt;br /&gt;
oL = L;				% raw		µm&lt;br /&gt;
L = L*Scale;		% to-scale	µm&lt;br /&gt;
&lt;br /&gt;
dTbase = dT;		% raw time-step &lt;br /&gt;
dT = dT*Scale;		% to-scale time-step&lt;br /&gt;
k = k;				% stdv of step distribution&lt;br /&gt;
&lt;br /&gt;
ma = msdanalyzer(2, SPACE_UNITS, TIME_UNITS);&lt;br /&gt;
ma = ma.addAll(tracks);&lt;br /&gt;
disp(ma)&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
ma.plotTracks;&lt;br /&gt;
ma.labelPlotTracks;&lt;br /&gt;
&lt;br /&gt;
ma = ma.computeMSD;&lt;br /&gt;
ma.msd;&lt;br /&gt;
&lt;br /&gt;
t = (0 : N_TIME_STEPS)&#039; * dT;&lt;br /&gt;
[T1, T2] = meshgrid(t, t);&lt;br /&gt;
all_delays = unique( abs(T1 - T2) );&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
ma.plotMSD;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cla&lt;br /&gt;
ma.plotMeanMSD(gca, true)&lt;br /&gt;
&lt;br /&gt;
mmsd = ma.getMeanMSD;&lt;br /&gt;
t = mmsd(:,1);&lt;br /&gt;
x = mmsd(:,2);&lt;br /&gt;
dx = mmsd(:,3) ./ sqrt(mmsd(:,4));&lt;br /&gt;
errorbar(t, x, dx, &#039;k&#039;)&lt;br /&gt;
&lt;br /&gt;
[fo, gof] = ma.fitMeanMSD;&lt;br /&gt;
plot(fo)&lt;br /&gt;
ma.labelPlotMSD;&lt;br /&gt;
legend off&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ma = ma.fitMSD;&lt;br /&gt;
&lt;br /&gt;
good_enough_fit = ma.lfit.r2fit &amp;gt; 0.8;&lt;br /&gt;
Dmean = mean( ma.lfit.a(good_enough_fit) ) / 2 / ma.n_dim;&lt;br /&gt;
Dstd  =  std( ma.lfit.a(good_enough_fit) ) / 2 / ma.n_dim;&lt;br /&gt;
&lt;br /&gt;
Dheader1 = [&#039;Raw Unscaled Values&#039;];&lt;br /&gt;
Dhead1 = [&#039;    D        Dn        L&#039;];&lt;br /&gt;
Ddat1 = [oD oDn oL];&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
disp(Dheader1)&lt;br /&gt;
disp(Dhead1)&lt;br /&gt;
disp(Ddat1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
yourtesthead = [&#039;YOU ARE TESTING DIFFUSION FOR:&#039;];&lt;br /&gt;
if MSDtest(1)&lt;br /&gt;
	yourtest = [&#039;   D:   original diffusion rate&#039;];&lt;br /&gt;
elseif MSDtest(2)&lt;br /&gt;
	yourtest = [&#039;   Dn:  new diffusion rate&#039;];&lt;br /&gt;
elseif MSDtest(3)&lt;br /&gt;
	yourtest = [&#039;   L:  step length&#039;];&lt;br /&gt;
else&lt;br /&gt;
	yourtest = [&#039;   generic diffusion rate&#039;];&lt;br /&gt;
end&lt;br /&gt;
disp(yourtesthead)&lt;br /&gt;
disp(yourtest)&lt;br /&gt;
&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
fprintf(&#039;Estimation of raw D coefficient from MSD:\n&#039;)&lt;br /&gt;
fprintf(&#039;D = %.3g ± %.3g (mean ± std, N = %d)\n&#039;, ...&lt;br /&gt;
    Dmean, Dstd, sum(good_enough_fit));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% Retrieve instantaneous velocities, per track&lt;br /&gt;
 trackV = ma.getVelocities;&lt;br /&gt;
&lt;br /&gt;
 % Pool track data together&lt;br /&gt;
 TV = vertcat( trackV{:} );&lt;br /&gt;
&lt;br /&gt;
 % Velocities are returned in a N x (nDim+1) array: [ T Vx Vy ...]. So the&lt;br /&gt;
 % velocity vector in 2D is:&lt;br /&gt;
 V = TV(:, 2:3);&lt;br /&gt;
&lt;br /&gt;
 % Compute diffusion coefficient&lt;br /&gt;
varV = var(V);&lt;br /&gt;
mVarV = mean(varV); % Take the mean of the two estimates&lt;br /&gt;
Dest = mVarV / 2 * dT;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Dheader2 = [&#039;Scaling to model...&#039;];&lt;br /&gt;
Dhead2 = [&#039;    D        Dn        L&#039;];&lt;br /&gt;
Ddat2 = [D Dn L];&lt;br /&gt;
&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
disp(Dheader2)&lt;br /&gt;
disp(Dhead2)&lt;br /&gt;
disp(Ddat2)&lt;br /&gt;
fprintf(&#039;Estimation from velocities histogram:\n&#039;)&lt;br /&gt;
fprintf(&#039;Tested D = %.3g %s, compare to scaled Des value of %.3g %s\n&#039;, ...&lt;br /&gt;
    Dest, [SPACE_UNITS &#039;²/&#039; TIME_UNITS], D, [SPACE_UNITS &#039;²/&#039; TIME_UNITS]);&lt;br /&gt;
&lt;br /&gt;
% printf(&#039;D.psd target value was %.3g %s\n&#039;, ...&lt;br /&gt;
%     Dest, msdDpsd, [SPACE_UNITS &#039;²/&#039; TIME_UNITS]);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
{{Clear}}&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion_MATLAB_Code&amp;diff=4105</id>
		<title>Brownian Motion MATLAB Code</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion_MATLAB_Code&amp;diff=4105"/>
		<updated>2024-06-23T08:59:58Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: Created page with &amp;quot;==Brownian Motion MATLAB Code==  &amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt; function [tracks] = BrownianMotion()   % STARTING PARAMETERS %----------------------------------------------------------- D = .3;                     % Diffusion Rate Ds = .1;                    % Diffusion Scalar (Ds = Dn) Dn = D/(D/Ds);              % new D after scaling L d = 2;                      % dimensions dT = 1;                     % time step k = sqrt(d*D);	            % stdev of...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Brownian Motion MATLAB Code==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
function [tracks] = BrownianMotion()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% STARTING PARAMETERS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
D = .3;                     % Diffusion Rate&lt;br /&gt;
Ds = .1;                    % Diffusion Scalar (Ds = Dn)&lt;br /&gt;
Dn = D/(D/Ds);              % new D after scaling L&lt;br /&gt;
d = 2;                      % dimensions&lt;br /&gt;
dT = 1;                     % time step&lt;br /&gt;
k = sqrt(d*D);	            % stdev of D&#039;s step size distribution&lt;br /&gt;
MSD = 2*d*D;                % mean squared displacement&lt;br /&gt;
L = sqrt(2*d*D);            % average diagonal (2D) step size&lt;br /&gt;
Lx = L/sqrt(2);             % average linear (1D) step size&lt;br /&gt;
Ls = 1/sqrt(D/Ds);          % scales Lx values for Dn&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MSDtest = [1 0 0];			% test: D, Dn, or L&lt;br /&gt;
Scale = 1/10;				% scale of model&lt;br /&gt;
Ndots = 100;&lt;br /&gt;
Nsteps = Ndots;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
xyl = ones(2,Ndots);&lt;br /&gt;
xyds = ones(2,Ndots);&lt;br /&gt;
lims = ((D+1)^2)*10;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% LIVE PARTICLE DIFFUSION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
for t = 1:Nsteps&lt;br /&gt;
&lt;br /&gt;
    xyds = STEPxyds(Ndots, k);&lt;br /&gt;
	[xyl] = AMPARSTEP(Ndots, xyds, xyl);&lt;br /&gt;
	MAINPLOT(xyl, lims);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD RANDOM STEPS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
tracks = cell(Ndots, 1);&lt;br /&gt;
&lt;br /&gt;
stepN = 1;&lt;br /&gt;
for t = 1:Nsteps &lt;br /&gt;
&lt;br /&gt;
	xyds = STEPxyds(Ndots, k);&lt;br /&gt;
	[xyl] = AMPARSTEP(Ndots, xyds, xyl);&lt;br /&gt;
    [tracks] = MSDfun(stepN, Nsteps, tracks, xyds);&lt;br /&gt;
&lt;br /&gt;
stepN = stepN+1;&lt;br /&gt;
end&lt;br /&gt;
MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD UNIFORM STEPS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
stepN = 1;&lt;br /&gt;
for t = 1:Nsteps &lt;br /&gt;
&lt;br /&gt;
xyds = stepsize(Ndots, Lx);&lt;br /&gt;
		&lt;br /&gt;
	[xyl xyds] = MSDAMPARSTEP(Ndots, xyds, xyl, Ls);&lt;br /&gt;
    [tracks] = MSDfun(stepN, Nsteps, tracks, xyds);&lt;br /&gt;
&lt;br /&gt;
stepN = stepN+1;&lt;br /&gt;
end&lt;br /&gt;
MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
%%&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% STEP SIZE GENERATOR&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function xyds = STEPxyds(Ndots, k)&lt;br /&gt;
&lt;br /&gt;
    xyds = (k * randn(2,Ndots));&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MOVE PARTICLES MAIN FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [xyl] = AMPARSTEP(Ndots, xyds, xyl)&lt;br /&gt;
	&lt;br /&gt;
	for j = 1:Ndots&lt;br /&gt;
        xyl(:,j) = xyl(:,j)+xyds(:,j);&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% LIVE DIFFUSION PLOT&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [] = MAINPLOT(xyl, lims)&lt;br /&gt;
&lt;br /&gt;
xlim = [-lims lims];&lt;br /&gt;
ylim = [-lims lims];&lt;br /&gt;
zlim = [-5 5];&lt;br /&gt;
&lt;br /&gt;
figure(1)&lt;br /&gt;
subplot(2,1,1), &lt;br /&gt;
AMPARPlot = gscatter(xyl(1,:),xyl(2,:));&lt;br /&gt;
axis([xlim, ylim]);&lt;br /&gt;
set(AMPARPlot,&#039;marker&#039;,&#039;.&#039;,&#039;markersize&#039;,[6],&#039;color&#039;,[1 0 0])&lt;br /&gt;
&lt;br /&gt;
figure(1);&lt;br /&gt;
subplot(2,1,2), &lt;br /&gt;
gscatter(xyl(1,:),xyl(2,:)); view(20, 30);&lt;br /&gt;
axis normal;&lt;br /&gt;
grid off&lt;br /&gt;
axis([xlim, ylim, zlim]);&lt;br /&gt;
set(gca, &#039;Box&#039;, &#039;on&#039;);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MANUAL STEP SIZE FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function xyds = stepsize(Ndots, Lx)&lt;br /&gt;
&lt;br /&gt;
   Lx(1:2,1:Ndots) = Lx;&lt;br /&gt;
   xyd = randi([0 1],Ndots,2)&#039;;&lt;br /&gt;
   xyd(xyd == 0) = -1;&lt;br /&gt;
   xyds = (Lx.*xyd);&lt;br /&gt;
   &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD SCALED STEPS FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [xyl xyds] = MSDAMPARSTEP(Ndots, xyds, xyl, Ls)&lt;br /&gt;
	&lt;br /&gt;
	for j = 1:Ndots&lt;br /&gt;
        xyds(:,j) = xyds(:,j)*Ls;&lt;br /&gt;
        xyl(:,j) = xyl(:,j)+xyds(:,j);&lt;br /&gt;
	end	&lt;br /&gt;
	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD TRACKS GENERATOR&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [tracks] = MSDfun(stepN, Nsteps, tracks, xyds)&lt;br /&gt;
&lt;br /&gt;
    time = (0:Nsteps-1)&#039;;&lt;br /&gt;
    xymsd = xyds&#039;;&lt;br /&gt;
    xymsd = cumsum(xymsd,1);&lt;br /&gt;
    tracks{stepN} = [time xymsd];&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD TRACKS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [] = MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest)&lt;br /&gt;
&lt;br /&gt;
SPACE_UNITS = &#039;µm&#039;;&lt;br /&gt;
TIME_UNITS = &#039;s&#039;;&lt;br /&gt;
N_PARTICLES = Ndots;&lt;br /&gt;
N_TIME_STEPS = Nsteps;&lt;br /&gt;
N_DIM = 2;&lt;br /&gt;
&lt;br /&gt;
oD = D;				% raw		µm^2/s&lt;br /&gt;
D  = D*Scale;       % to-scale	µm^2/s&lt;br /&gt;
&lt;br /&gt;
oDn = Dn;			% raw		µm^2/s&lt;br /&gt;
Dn = Dn*Scale;		% to-scale	µm^2/s&lt;br /&gt;
&lt;br /&gt;
oL = L;				% raw		µm&lt;br /&gt;
L = L*Scale;		% to-scale	µm&lt;br /&gt;
&lt;br /&gt;
dTbase = dT;		% raw time-step &lt;br /&gt;
dT = dT*Scale;		% to-scale time-step&lt;br /&gt;
k = k;				% stdv of step distribution&lt;br /&gt;
&lt;br /&gt;
ma = msdanalyzer(2, SPACE_UNITS, TIME_UNITS);&lt;br /&gt;
ma = ma.addAll(tracks);&lt;br /&gt;
disp(ma)&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
ma.plotTracks;&lt;br /&gt;
ma.labelPlotTracks;&lt;br /&gt;
&lt;br /&gt;
ma = ma.computeMSD;&lt;br /&gt;
ma.msd;&lt;br /&gt;
&lt;br /&gt;
t = (0 : N_TIME_STEPS)&#039; * dT;&lt;br /&gt;
[T1, T2] = meshgrid(t, t);&lt;br /&gt;
all_delays = unique( abs(T1 - T2) );&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
ma.plotMSD;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cla&lt;br /&gt;
ma.plotMeanMSD(gca, true)&lt;br /&gt;
&lt;br /&gt;
mmsd = ma.getMeanMSD;&lt;br /&gt;
t = mmsd(:,1);&lt;br /&gt;
x = mmsd(:,2);&lt;br /&gt;
dx = mmsd(:,3) ./ sqrt(mmsd(:,4));&lt;br /&gt;
errorbar(t, x, dx, &#039;k&#039;)&lt;br /&gt;
&lt;br /&gt;
[fo, gof] = ma.fitMeanMSD;&lt;br /&gt;
plot(fo)&lt;br /&gt;
ma.labelPlotMSD;&lt;br /&gt;
legend off&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ma = ma.fitMSD;&lt;br /&gt;
&lt;br /&gt;
good_enough_fit = ma.lfit.r2fit &amp;gt; 0.8;&lt;br /&gt;
Dmean = mean( ma.lfit.a(good_enough_fit) ) / 2 / ma.n_dim;&lt;br /&gt;
Dstd  =  std( ma.lfit.a(good_enough_fit) ) / 2 / ma.n_dim;&lt;br /&gt;
&lt;br /&gt;
Dheader1 = [&#039;Raw Unscaled Values&#039;];&lt;br /&gt;
Dhead1 = [&#039;    D        Dn        L&#039;];&lt;br /&gt;
Ddat1 = [oD oDn oL];&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
disp(Dheader1)&lt;br /&gt;
disp(Dhead1)&lt;br /&gt;
disp(Ddat1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
yourtesthead = [&#039;YOU ARE TESTING DIFFUSION FOR:&#039;];&lt;br /&gt;
if MSDtest(1)&lt;br /&gt;
	yourtest = [&#039;   D:   original diffusion rate&#039;];&lt;br /&gt;
elseif MSDtest(2)&lt;br /&gt;
	yourtest = [&#039;   Dn:  new diffusion rate&#039;];&lt;br /&gt;
elseif MSDtest(3)&lt;br /&gt;
	yourtest = [&#039;   L:  step length&#039;];&lt;br /&gt;
else&lt;br /&gt;
	yourtest = [&#039;   generic diffusion rate&#039;];&lt;br /&gt;
end&lt;br /&gt;
disp(yourtesthead)&lt;br /&gt;
disp(yourtest)&lt;br /&gt;
&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
fprintf(&#039;Estimation of raw D coefficient from MSD:\n&#039;)&lt;br /&gt;
fprintf(&#039;D = %.3g ± %.3g (mean ± std, N = %d)\n&#039;, ...&lt;br /&gt;
    Dmean, Dstd, sum(good_enough_fit));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% Retrieve instantaneous velocities, per track&lt;br /&gt;
 trackV = ma.getVelocities;&lt;br /&gt;
&lt;br /&gt;
 % Pool track data together&lt;br /&gt;
 TV = vertcat( trackV{:} );&lt;br /&gt;
&lt;br /&gt;
 % Velocities are returned in a N x (nDim+1) array: [ T Vx Vy ...]. So the&lt;br /&gt;
 % velocity vector in 2D is:&lt;br /&gt;
 V = TV(:, 2:3);&lt;br /&gt;
&lt;br /&gt;
 % Compute diffusion coefficient&lt;br /&gt;
varV = var(V);&lt;br /&gt;
mVarV = mean(varV); % Take the mean of the two estimates&lt;br /&gt;
Dest = mVarV / 2 * dT;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Dheader2 = [&#039;Scaling to model...&#039;];&lt;br /&gt;
Dhead2 = [&#039;    D        Dn        L&#039;];&lt;br /&gt;
Ddat2 = [D Dn L];&lt;br /&gt;
&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
disp(Dheader2)&lt;br /&gt;
disp(Dhead2)&lt;br /&gt;
disp(Ddat2)&lt;br /&gt;
fprintf(&#039;Estimation from velocities histogram:\n&#039;)&lt;br /&gt;
fprintf(&#039;Tested D = %.3g %s, compare to scaled Des value of %.3g %s\n&#039;, ...&lt;br /&gt;
    Dest, [SPACE_UNITS &#039;²/&#039; TIME_UNITS], D, [SPACE_UNITS &#039;²/&#039; TIME_UNITS]);&lt;br /&gt;
&lt;br /&gt;
% printf(&#039;D.psd target value was %.3g %s\n&#039;, ...&lt;br /&gt;
%     Dest, msdDpsd, [SPACE_UNITS &#039;²/&#039; TIME_UNITS]);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
{{Clear}}&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
	<entry>
		<id>https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4104</id>
		<title>Brownian Motion</title>
		<link rel="alternate" type="text/html" href="https://bradleymonk.com/wiki/index.php?title=Brownian_Motion&amp;diff=4104"/>
		<updated>2024-06-23T05:06:38Z</updated>

		<summary type="html">&lt;p&gt;Bradley Monk: /* Theoretical Value of D */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Brownian-Diffusion.gif|right]]&lt;br /&gt;
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through &#039;&#039;n&#039;&#039;-dimensional spaces filled with other particles and physical barriers. Here the term &#039;&#039;particle&#039;&#039; is a generic term that can be generalized to describe the motion of molecule (e.g. H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements. &lt;br /&gt;
{{TOC right}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Properties of Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
Physical experiments characterize the following properties of Brownian motion...&lt;br /&gt;
* continuous increments&lt;br /&gt;
* increments of a particle over disjoint time intervals are independent events&lt;br /&gt;
* each increment is assumed to result from collisions with many molecules&lt;br /&gt;
* each increment is assumed to have a normal probability distribution &lt;br /&gt;
* the mean increment is zero as there is no preferred direction&lt;br /&gt;
* the position of a particle spreads out with time&lt;br /&gt;
* the variance of the increment is proportional to the length of time that Brownian motion has been observed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Common Diffusion Terms &amp;amp; Parameters==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
I&#039;ve found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;big&amp;gt;D&amp;lt;/big&amp;gt; &amp;amp;nbsp;: diffusion rate coefficient ===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
D = {L^{2} \over 2d \cdot t}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : step length&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D (in units: &#039;&#039;&#039;µm²/s&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once &#039;&#039;&#039;D&#039;&#039;&#039; is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle&#039;s movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: &lt;br /&gt;
&lt;br /&gt;
Instead, &#039;&#039;&#039;D&#039;&#039;&#039; is often calculated from the &#039;&#039;mean squared diffusion&#039;&#039; (MSD) path of the particle, defined below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean Squared Displacement (MSD)===&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
MSD = { 2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;MSD (in units: &#039;&#039;&#039;µm²&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the mean square displacement of a particle over some time period.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Standard deviation (k) of the normal distribution of step lengths ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
k = \sqrt{d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;k (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate &#039;&#039;&#039;D&#039;&#039;&#039;. This value is useful for simulating Brownian motion for a particular diffusion rate. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mean Step Length (L)===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
L = \sqrt{2d \cdot D}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : dimensions&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : diffusion coefficient&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L or Λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;L = (2d * D)&amp;lt;sup&amp;gt;.5&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mean 1D step length component (λ) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\lambda = {L \over{\sqrt{2}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;λ (in units: &#039;&#039;&#039;µm&#039;&#039;&#039;)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is the average 1-dimensional step length for each component (X,Y,Z) dimension of &#039;&#039;&#039;&#039;&#039;L&#039;&#039;&#039;&#039;&#039;. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Step length scalar coefficient (ε) ===&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\epsilon = {\sqrt{\delta \over{D}}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;big&amp;gt;&amp;lt;code&amp;gt;δ&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; : new desired diffusion rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;big&amp;gt;&amp;lt;code&amp;gt;ε (in units: au)&amp;lt;/code&amp;gt;&amp;lt;/big&amp;gt; is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate &#039;&#039;&#039;δ&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
==Brownian Motion MATLAB Code==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
function [tracks] = BrownianMotion()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% STARTING PARAMETERS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
D = .3;                     % Diffusion Rate&lt;br /&gt;
Ds = .1;                    % Diffusion Scalar (Ds = Dn)&lt;br /&gt;
Dn = D/(D/Ds);              % new D after scaling L&lt;br /&gt;
d = 2;                      % dimensions&lt;br /&gt;
dT = 1;                     % time step&lt;br /&gt;
k = sqrt(d*D);	            % stdev of D&#039;s step size distribution&lt;br /&gt;
MSD = 2*d*D;                % mean squared displacement&lt;br /&gt;
L = sqrt(2*d*D);            % average diagonal (2D) step size&lt;br /&gt;
Lx = L/sqrt(2);             % average linear (1D) step size&lt;br /&gt;
Ls = 1/sqrt(D/Ds);          % scales Lx values for Dn&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MSDtest = [1 0 0];			% test: D, Dn, or L&lt;br /&gt;
Scale = 1/10;				% scale of model&lt;br /&gt;
Ndots = 100;&lt;br /&gt;
Nsteps = Ndots;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
xyl = ones(2,Ndots);&lt;br /&gt;
xyds = ones(2,Ndots);&lt;br /&gt;
lims = ((D+1)^2)*10;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% LIVE PARTICLE DIFFUSION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
for t = 1:Nsteps&lt;br /&gt;
&lt;br /&gt;
    xyds = STEPxyds(Ndots, k);&lt;br /&gt;
	[xyl] = AMPARSTEP(Ndots, xyds, xyl);&lt;br /&gt;
	MAINPLOT(xyl, lims);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD RANDOM STEPS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
tracks = cell(Ndots, 1);&lt;br /&gt;
&lt;br /&gt;
stepN = 1;&lt;br /&gt;
for t = 1:Nsteps &lt;br /&gt;
&lt;br /&gt;
	xyds = STEPxyds(Ndots, k);&lt;br /&gt;
	[xyl] = AMPARSTEP(Ndots, xyds, xyl);&lt;br /&gt;
    [tracks] = MSDfun(stepN, Nsteps, tracks, xyds);&lt;br /&gt;
&lt;br /&gt;
stepN = stepN+1;&lt;br /&gt;
end&lt;br /&gt;
MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD UNIFORM STEPS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
stepN = 1;&lt;br /&gt;
for t = 1:Nsteps &lt;br /&gt;
&lt;br /&gt;
xyds = stepsize(Ndots, Lx);&lt;br /&gt;
		&lt;br /&gt;
	[xyl xyds] = MSDAMPARSTEP(Ndots, xyds, xyl, Ls);&lt;br /&gt;
    [tracks] = MSDfun(stepN, Nsteps, tracks, xyds);&lt;br /&gt;
&lt;br /&gt;
stepN = stepN+1;&lt;br /&gt;
end&lt;br /&gt;
MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
%%&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% STEP SIZE GENERATOR&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function xyds = STEPxyds(Ndots, k)&lt;br /&gt;
&lt;br /&gt;
    xyds = (k * randn(2,Ndots));&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MOVE PARTICLES MAIN FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [xyl] = AMPARSTEP(Ndots, xyds, xyl)&lt;br /&gt;
	&lt;br /&gt;
	for j = 1:Ndots&lt;br /&gt;
        xyl(:,j) = xyl(:,j)+xyds(:,j);&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% LIVE DIFFUSION PLOT&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [] = MAINPLOT(xyl, lims)&lt;br /&gt;
&lt;br /&gt;
xlim = [-lims lims];&lt;br /&gt;
ylim = [-lims lims];&lt;br /&gt;
zlim = [-5 5];&lt;br /&gt;
&lt;br /&gt;
figure(1)&lt;br /&gt;
subplot(2,1,1), &lt;br /&gt;
AMPARPlot = gscatter(xyl(1,:),xyl(2,:));&lt;br /&gt;
axis([xlim, ylim]);&lt;br /&gt;
set(AMPARPlot,&#039;marker&#039;,&#039;.&#039;,&#039;markersize&#039;,[6],&#039;color&#039;,[1 0 0])&lt;br /&gt;
&lt;br /&gt;
figure(1);&lt;br /&gt;
subplot(2,1,2), &lt;br /&gt;
gscatter(xyl(1,:),xyl(2,:)); view(20, 30);&lt;br /&gt;
axis normal;&lt;br /&gt;
grid off&lt;br /&gt;
axis([xlim, ylim, zlim]);&lt;br /&gt;
set(gca, &#039;Box&#039;, &#039;on&#039;);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MANUAL STEP SIZE FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function xyds = stepsize(Ndots, Lx)&lt;br /&gt;
&lt;br /&gt;
   Lx(1:2,1:Ndots) = Lx;&lt;br /&gt;
   xyd = randi([0 1],Ndots,2)&#039;;&lt;br /&gt;
   xyd(xyd == 0) = -1;&lt;br /&gt;
   xyds = (Lx.*xyd);&lt;br /&gt;
   &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD SCALED STEPS FUNCTION&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [xyl xyds] = MSDAMPARSTEP(Ndots, xyds, xyl, Ls)&lt;br /&gt;
	&lt;br /&gt;
	for j = 1:Ndots&lt;br /&gt;
        xyds(:,j) = xyds(:,j)*Ls;&lt;br /&gt;
        xyl(:,j) = xyl(:,j)+xyds(:,j);&lt;br /&gt;
	end	&lt;br /&gt;
	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD TRACKS GENERATOR&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [tracks] = MSDfun(stepN, Nsteps, tracks, xyds)&lt;br /&gt;
&lt;br /&gt;
    time = (0:Nsteps-1)&#039;;&lt;br /&gt;
    xymsd = xyds&#039;;&lt;br /&gt;
    xymsd = cumsum(xymsd,1);&lt;br /&gt;
    tracks{stepN} = [time xymsd];&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% MSD TRACKS ANALYSIS&lt;br /&gt;
%-----------------------------------------------------------&lt;br /&gt;
function [] = MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest)&lt;br /&gt;
&lt;br /&gt;
SPACE_UNITS = &#039;µm&#039;;&lt;br /&gt;
TIME_UNITS = &#039;s&#039;;&lt;br /&gt;
N_PARTICLES = Ndots;&lt;br /&gt;
N_TIME_STEPS = Nsteps;&lt;br /&gt;
N_DIM = 2;&lt;br /&gt;
&lt;br /&gt;
oD = D;				% raw		µm^2/s&lt;br /&gt;
D  = D*Scale;       % to-scale	µm^2/s&lt;br /&gt;
&lt;br /&gt;
oDn = Dn;			% raw		µm^2/s&lt;br /&gt;
Dn = Dn*Scale;		% to-scale	µm^2/s&lt;br /&gt;
&lt;br /&gt;
oL = L;				% raw		µm&lt;br /&gt;
L = L*Scale;		% to-scale	µm&lt;br /&gt;
&lt;br /&gt;
dTbase = dT;		% raw time-step &lt;br /&gt;
dT = dT*Scale;		% to-scale time-step&lt;br /&gt;
k = k;				% stdv of step distribution&lt;br /&gt;
&lt;br /&gt;
ma = msdanalyzer(2, SPACE_UNITS, TIME_UNITS);&lt;br /&gt;
ma = ma.addAll(tracks);&lt;br /&gt;
disp(ma)&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
ma.plotTracks;&lt;br /&gt;
ma.labelPlotTracks;&lt;br /&gt;
&lt;br /&gt;
ma = ma.computeMSD;&lt;br /&gt;
ma.msd;&lt;br /&gt;
&lt;br /&gt;
t = (0 : N_TIME_STEPS)&#039; * dT;&lt;br /&gt;
[T1, T2] = meshgrid(t, t);&lt;br /&gt;
all_delays = unique( abs(T1 - T2) );&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
ma.plotMSD;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cla&lt;br /&gt;
ma.plotMeanMSD(gca, true)&lt;br /&gt;
&lt;br /&gt;
mmsd = ma.getMeanMSD;&lt;br /&gt;
t = mmsd(:,1);&lt;br /&gt;
x = mmsd(:,2);&lt;br /&gt;
dx = mmsd(:,3) ./ sqrt(mmsd(:,4));&lt;br /&gt;
errorbar(t, x, dx, &#039;k&#039;)&lt;br /&gt;
&lt;br /&gt;
[fo, gof] = ma.fitMeanMSD;&lt;br /&gt;
plot(fo)&lt;br /&gt;
ma.labelPlotMSD;&lt;br /&gt;
legend off&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ma = ma.fitMSD;&lt;br /&gt;
&lt;br /&gt;
good_enough_fit = ma.lfit.r2fit &amp;gt; 0.8;&lt;br /&gt;
Dmean = mean( ma.lfit.a(good_enough_fit) ) / 2 / ma.n_dim;&lt;br /&gt;
Dstd  =  std( ma.lfit.a(good_enough_fit) ) / 2 / ma.n_dim;&lt;br /&gt;
&lt;br /&gt;
Dheader1 = [&#039;Raw Unscaled Values&#039;];&lt;br /&gt;
Dhead1 = [&#039;    D        Dn        L&#039;];&lt;br /&gt;
Ddat1 = [oD oDn oL];&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
disp(Dheader1)&lt;br /&gt;
disp(Dhead1)&lt;br /&gt;
disp(Ddat1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
yourtesthead = [&#039;YOU ARE TESTING DIFFUSION FOR:&#039;];&lt;br /&gt;
if MSDtest(1)&lt;br /&gt;
	yourtest = [&#039;   D:   original diffusion rate&#039;];&lt;br /&gt;
elseif MSDtest(2)&lt;br /&gt;
	yourtest = [&#039;   Dn:  new diffusion rate&#039;];&lt;br /&gt;
elseif MSDtest(3)&lt;br /&gt;
	yourtest = [&#039;   L:  step length&#039;];&lt;br /&gt;
else&lt;br /&gt;
	yourtest = [&#039;   generic diffusion rate&#039;];&lt;br /&gt;
end&lt;br /&gt;
disp(yourtesthead)&lt;br /&gt;
disp(yourtest)&lt;br /&gt;
&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
fprintf(&#039;Estimation of raw D coefficient from MSD:\n&#039;)&lt;br /&gt;
fprintf(&#039;D = %.3g ± %.3g (mean ± std, N = %d)\n&#039;, ...&lt;br /&gt;
    Dmean, Dstd, sum(good_enough_fit));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
% Retrieve instantaneous velocities, per track&lt;br /&gt;
 trackV = ma.getVelocities;&lt;br /&gt;
&lt;br /&gt;
 % Pool track data together&lt;br /&gt;
 TV = vertcat( trackV{:} );&lt;br /&gt;
&lt;br /&gt;
 % Velocities are returned in a N x (nDim+1) array: [ T Vx Vy ...]. So the&lt;br /&gt;
 % velocity vector in 2D is:&lt;br /&gt;
 V = TV(:, 2:3);&lt;br /&gt;
&lt;br /&gt;
 % Compute diffusion coefficient&lt;br /&gt;
varV = var(V);&lt;br /&gt;
mVarV = mean(varV); % Take the mean of the two estimates&lt;br /&gt;
Dest = mVarV / 2 * dT;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Dheader2 = [&#039;Scaling to model...&#039;];&lt;br /&gt;
Dhead2 = [&#039;    D        Dn        L&#039;];&lt;br /&gt;
Ddat2 = [D Dn L];&lt;br /&gt;
&lt;br /&gt;
disp(&#039; &#039;)&lt;br /&gt;
disp(Dheader2)&lt;br /&gt;
disp(Dhead2)&lt;br /&gt;
disp(Ddat2)&lt;br /&gt;
fprintf(&#039;Estimation from velocities histogram:\n&#039;)&lt;br /&gt;
fprintf(&#039;Tested D = %.3g %s, compare to scaled Des value of %.3g %s\n&#039;, ...&lt;br /&gt;
    Dest, [SPACE_UNITS &#039;²/&#039; TIME_UNITS], D, [SPACE_UNITS &#039;²/&#039; TIME_UNITS]);&lt;br /&gt;
&lt;br /&gt;
% printf(&#039;D.psd target value was %.3g %s\n&#039;, ...&lt;br /&gt;
%     Dest, msdDpsd, [SPACE_UNITS &#039;²/&#039; TIME_UNITS]);&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
{{Clear}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Simulating Particle Trajectories=&lt;br /&gt;
&lt;br /&gt;
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==One Dimensional Brownian Motion==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below. &lt;br /&gt;
&lt;br /&gt;
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% One Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
N = 1000;&lt;br /&gt;
displacement = randn(1,N);&lt;br /&gt;
plot(displacement);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Distribution of Displacements==     &lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Have a look at the distribution of the randomly generated displacements. The &amp;lt;tt&amp;gt;hist&amp;lt;/tt&amp;gt; command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(displacement, 25);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Convert displacements to position==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now we have some appropriate random displacements. Their sum represents a particle  trajectory in 1 dimension. The Matlab function &amp;lt;tt&amp;gt;cumsum&amp;lt;/tt&amp;gt; returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
x = cumsum(displacement);&lt;br /&gt;
plot(x);&lt;br /&gt;
ylabel(&#039;position&#039;);&lt;br /&gt;
xlabel(&#039;time step&#039;);&lt;br /&gt;
title(&#039;Position of 1D Particle versus Time&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Two dimensional particle simulation==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Two Dimensional Brownian Motion&lt;br /&gt;
&lt;br /&gt;
particle = struct();&lt;br /&gt;
particle.x = cumsum( randn(N, 1) );&lt;br /&gt;
particle.y = cumsum( randn(N, 1) );&lt;br /&gt;
&lt;br /&gt;
plot(particle.x, particle.y);&lt;br /&gt;
ylabel(&#039;Y Position&#039;);&lt;br /&gt;
xlabel(&#039;X Position&#039;);&lt;br /&gt;
title(&#039;position versus time in 2D&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compute the Displacement Squared==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).&lt;br /&gt;
&lt;br /&gt;
The dot caret (.^) operator raises each element of a matrix to a power.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% Displacement Squared&lt;br /&gt;
&lt;br /&gt;
dsquared = particle.x .^ 2 + particle.y .^ 2;&lt;br /&gt;
plot(dsquared);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Theoretical Value of D==&lt;br /&gt;
&lt;br /&gt;
The theoretical value of the diffusion coefficient, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;, is given by &amp;lt;code&amp;gt;D    = k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; * T / (3πrη)&amp;lt;/code&amp;gt; where &amp;lt;i&amp;gt;T&amp;lt;/i&amp;gt; = temperature (Kelvin), &amp;lt;i&amp;gt;k&amp;lt;/i&amp;gt;&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt; = Boltzmann&#039;s constant, eta = viscosity, and &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt; = diffusion_radius. Note that the units of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; are length squared divided by time. See the lab writeup for more information. Let&#039;s compute &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for a 1 micron particle in water at 293 degrees Kelvin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
r      = 1.0e-6;              % radius in meters&lt;br /&gt;
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K&lt;br /&gt;
kB   = 1.38e-23;            % Boltzmann constant&lt;br /&gt;
T     = 293;                 % Temperature in degrees Kelvin&lt;br /&gt;
&lt;br /&gt;
D = kB * T / (3 * pi * r * eta)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = 2D / dT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A more realistic particle -- Getting the Units Right==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;According to theory, the mean squared displacement of the particle is proportional to the time interval, where &amp;lt;i&amp;gt;r&amp;lt;/i&amp;gt;(&amp;lt;i&amp;gt;t&amp;lt;/i&amp;gt;) = position, &amp;lt;i&amp;gt;d&amp;lt;/i&amp;gt; = number of dimensions, &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; = diffusion coefficient, and tau = time interval.&amp;lt;/p&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To generate the correct distribution, the output from &amp;lt;tt&amp;gt;randn&amp;lt;/tt&amp;gt; (which has a standard normal distribution) must be scaled by the factor &amp;lt;tt&amp;gt;k&amp;lt;/tt&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
%             Two Dimensional Realistic Brownian Motion&lt;br /&gt;
%-------------###############################----------------%&lt;br /&gt;
&lt;br /&gt;
dimensions = 2;         % two dimensional simulation&lt;br /&gt;
tau = .1;               % time interval in seconds&lt;br /&gt;
time = tau * 1:N;       % create a time vector for plotting&lt;br /&gt;
&lt;br /&gt;
k = sqrt(D * dimensions * tau);&lt;br /&gt;
dx = k * randn(N,1);&lt;br /&gt;
dy = k * randn(N,1);&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle&#039;);&lt;br /&gt;
&lt;br /&gt;
%-------------############################------------------%&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared Plot==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_07.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time for 1 Particle in 2 Dimensions&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Estimating &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the Simulated Data==&lt;br /&gt;
&lt;br /&gt;
The best estimate of the value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; from the simulated data is:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
     4.2192e-013&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uncertainty in the Estimate==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
standardError = &lt;br /&gt;
               1.3162e-014&lt;br /&gt;
&lt;br /&gt;
actualError   = &lt;br /&gt;
               7.1019e-015&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Systematic Error -- Bulk Flow in the Solvent==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
dx = dx + 0.2 * k;&lt;br /&gt;
dy = dy + 0.05 * k;&lt;br /&gt;
&lt;br /&gt;
x = cumsum(dx);&lt;br /&gt;
y = cumsum(dy);&lt;br /&gt;
&lt;br /&gt;
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);&lt;br /&gt;
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);&lt;br /&gt;
&lt;br /&gt;
simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )&lt;br /&gt;
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )&lt;br /&gt;
actualError = D - simulatedD&lt;br /&gt;
&lt;br /&gt;
plot(x,y);&lt;br /&gt;
title(&#039;Particle Track of a Single Simulated Particle with Bulk Flow&#039;);&lt;br /&gt;
&lt;br /&gt;
simulatedD    =  &lt;br /&gt;
               4.2926e-013&lt;br /&gt;
standardError =  &lt;br /&gt;
               1.3694e-014&lt;br /&gt;
actualError   = &lt;br /&gt;
              -2.3859e-016&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Displacement Squared in the Presence of Bulk Flow==&lt;br /&gt;
&lt;br /&gt;
[[Image:SingleParticleSimulations_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there&#039;s a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;k&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
plot(time, squaredDisplacement);&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared&#039;);&lt;br /&gt;
title(&#039;Displacement Squared versus Time with Bulk Flow&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Particles==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let&#039;s simulate this.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a For Loop to Generate Multiple Data Sets===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called &amp;lt;tt&amp;gt;particle&amp;lt;/tt&amp;gt;. For example, &amp;lt;tt&amp;gt;particle{3}&amp;lt;/tt&amp;gt; refers to a structure containing the results of simulation number 3. &amp;lt;tt&amp;gt;particle{3}.D&amp;lt;/tt&amp;gt; is the estimated value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
particleCount = 10;&lt;br /&gt;
N = 50;&lt;br /&gt;
tau = .1;&lt;br /&gt;
time = 0:tau:(N-1) * tau;&lt;br /&gt;
particle = { };             % create an empty cell array to hold the results&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    particle{i} = struct();&lt;br /&gt;
    particle{i}.dx = k * randn(1,N);&lt;br /&gt;
    particle{i}.x = cumsum(particle{i}.dx);&lt;br /&gt;
    particle{i}.dy = k * randn(1,N);&lt;br /&gt;
    particle{i}.y = cumsum(particle{i}.dy);&lt;br /&gt;
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;&lt;br /&gt;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;&lt;br /&gt;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );&lt;br /&gt;
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===SimulateParticle Function===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;That&#039;s a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called &amp;lt;tt&amp;gt;SimulateParticles&amp;lt;/tt&amp;gt;. If you are interested in how the function is implemented, type &amp;lt;tt&amp;gt;edit SimulateParticles.m&amp;lt;/tt&amp;gt; to have a look at the m-file for this function.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
help SimulateParticles&lt;br /&gt;
&lt;br /&gt;
particle = SimulateParticles(N, particleCount, tau, k);&lt;br /&gt;
 &lt;br /&gt;
usage: out = &lt;br /&gt;
SimulateParticles( N, particleCount, tau, k )&lt;br /&gt;
&lt;br /&gt;
N is the number of samples&lt;br /&gt;
particleCount is the number of particles&lt;br /&gt;
tau is the sample period&lt;br /&gt;
k is the standard deviation of dx and dy&lt;br /&gt;
 &lt;br /&gt;
returns a cellular array of length particleCount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Look at the Results===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following code plots all of the generated particle tracks on a single set of axes, each in a random color.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(particle{i}.x, particle{i}.y, &#039;color&#039;, rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;X position (m)&#039;);&lt;br /&gt;
ylabel(&#039;Y position (m)&#039;);&lt;br /&gt;
title(&#039;Combined Particle Tracks&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Displacement Squared===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
% compute the ensemble average&lt;br /&gt;
rsquaredSum = zeros(1,N);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    rsquaredSum = rsquaredSum + particle{i}.rsquared;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ensembleAverage = rsquaredSum / particleCount;&lt;br /&gt;
&lt;br /&gt;
% create the plot&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
plot(time, (0:1:(N-1)) * 2*k^2 , &#039;b&#039;, &#039;LineWidth&#039;, 3);      % plot theoretical line&lt;br /&gt;
&lt;br /&gt;
plot(time, ensembleAverage , &#039;k&#039;, &#039;LineWidth&#039;, 3);          % plot ensemble average&lt;br /&gt;
legend(&#039;Theoretical&#039;,&#039;Average&#039;,&#039;location&#039;,&#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    plot(time, particle{i}.rsquared, &#039;color&#039;, rand(1,3));   % plot each particle track&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Time (seconds)&#039;);&lt;br /&gt;
ylabel(&#039;Displacement Squared (m^2)&#039;);&lt;br /&gt;
title(&#039;Displacement Squared vs Time&#039;);&lt;br /&gt;
hold off;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Estimated Value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This plot shows the computed value of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; for each simulation with error bars. A thick blue line indicates the best overall estimate of &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; (the average of the &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; value from each simulation) along with error bars in green &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt; WHICH HAPPEN TO BE WRONG -- NEED TO FIX &amp;lt;b&amp;gt;*&amp;lt;/b&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clear D e dx;&lt;br /&gt;
&lt;br /&gt;
% extract the D value from each simulation and place them all into a single&lt;br /&gt;
% matrix called &#039;D&#039;&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    D(i) = particle{i}.D;&lt;br /&gt;
    dx(i,:) = particle{i}.dx;&lt;br /&gt;
    e(i) = particle{i}.standardError;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute the estimate of D and the uncertainty&lt;br /&gt;
averageD = mean(D)&lt;br /&gt;
uncertainty = std(D)/sqrt(particleCount)&lt;br /&gt;
&lt;br /&gt;
% plot everything&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
plot(averageD * ones(1,particleCount), &#039;b&#039;, &#039;linewidth&#039;, 3);                    % plot estimated D&lt;br /&gt;
plot((averageD + uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot upper error bar&lt;br /&gt;
plot((averageD - uncertainty) * ones(1,particleCount), &#039;g-&#039;, &#039;linewidth&#039;, 1);   % plot lower error bar&lt;br /&gt;
errorbar(D,e,&#039;ro&#039;);                                                             % plot D values with error bars&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Simulation Number&#039;);&lt;br /&gt;
ylabel(&#039;Estimated Diffusion Coefficient&#039;);&lt;br /&gt;
title(&#039;Estimated Diffusion Coefficient with Error Bars&#039;)&lt;br /&gt;
legend(&#039;Average Value of D&#039;, &#039;location&#039;, &#039;NorthWest&#039;);&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
averageD    = &lt;br /&gt;
             4.2886e-013&lt;br /&gt;
&lt;br /&gt;
uncertainty = &lt;br /&gt;
             2.3294e-014&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fancy Statistics and Plots===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Uncertainty in the estimate of the mean value of a random variable from a population of samples&lt;br /&gt;
* The effect of squaring a normally distributed random variable&lt;br /&gt;
* The assumption of statistical independence of samples&lt;br /&gt;
&lt;br /&gt;
==A More Perfect Distribution==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_01.png|right|thumb|300px]]&lt;br /&gt;
[[Image:FancyStatistics_02.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hist(randn(1,1e3),25)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;hist(randn(1,1e6),100)&lt;br /&gt;
xlabel(&#039;Value&#039;);&lt;br /&gt;
ylabel(&#039;Frequency&#039;);&lt;br /&gt;
title(&#039;Histogram of Values for 1000000 Samples of randn&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sampling Uncertainty==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_03.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an &#039;x&#039; versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab note: check out the nested &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loops used to create the plot.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot; line start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
clf; hold on;&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        y = randn(1,i);&lt;br /&gt;
        m = mean(y);&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);   % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, -1./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Normally Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What Happens when you Square a Random Variable?==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_04.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;We did not estimate &amp;lt;i&amp;gt;D&amp;lt;/i&amp;gt; in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dx = randn(1,1e6);&lt;br /&gt;
dy = randn(1,1e6);&lt;br /&gt;
drSquared = dx .^ 2 + dy .^ 2;&lt;br /&gt;
&lt;br /&gt;
mean(drSquared)&lt;br /&gt;
var(dx) + var(dy)&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
hist(drSquared,100);&lt;br /&gt;
title(&#039;Chi Squared Distribution (2 DOF), 1000000 Samples&#039;);&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
    1.9982&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==100 Years of BIO Lab Data in 1 Second==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_05.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
&lt;br /&gt;
for i= 1:100&lt;br /&gt;
    for j = 1:50&lt;br /&gt;
        dx = randn(1,i);&lt;br /&gt;
        dy = randn(1,i);&lt;br /&gt;
        m = mean( dx .^ 2 + dy .^ 2 );&lt;br /&gt;
        plot(i,m,&#039;x&#039;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plot(1:100, 2 + 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot upper error bar in dark black&lt;br /&gt;
plot(1:100, 2 - 2./sqrt(1:100), &#039;k&#039;, &#039;LineWidth&#039;, 2);  % plot lower error bar in dark black&lt;br /&gt;
&lt;br /&gt;
hold off;&lt;br /&gt;
xlabel(&#039;Population Size (N)&#039;);&lt;br /&gt;
ylabel(&#039;Population Mean&#039;);&lt;br /&gt;
title(&#039;Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Planning Your Time in the Lab===&lt;br /&gt;
&lt;br /&gt;
Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Auto Correlation - A Closer Look at the Data==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_06.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn&#039;t mean it&#039;s true.&lt;br /&gt;
&lt;br /&gt;
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt;. If x is a vector, &amp;lt;tt&amp;gt;xcorr(x)&amp;lt;/tt&amp;gt; returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option &#039;coeff&#039; causes the xcorr function to normalize this value to exactly 1 --perfect correlation.&lt;br /&gt;
&lt;br /&gt;
Also notice that the autocorrelation sequence is symmetric about the origin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
title(&#039;Particle 1 x-axis Displacement Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Cross Correlation==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_07.png|right|thumb|300px]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;With two vector arguments, &amp;lt;tt&amp;gt;xcorr(x,y)&amp;lt;/tt&amp;gt; returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle&#039;s trajectory and another&#039;s. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
clf;&lt;br /&gt;
c = xcorr(particle{1}.dx, particle{2}.dx, &#039;coeff&#039;);&lt;br /&gt;
xaxis = (1-length(c))/2:1:(length(c)-1)/2;&lt;br /&gt;
plot(xaxis, c);&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation&#039;);&lt;br /&gt;
Title(&#039;Particle 1, 2 x-axis Displacement Cross Correlation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==MATLAB Tricks==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_08.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Matlab can generate (and plot) an amazing amount of data. For example, the &amp;lt;tt&amp;gt;xcorr&amp;lt;/tt&amp;gt; function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, &amp;lt;tt&amp;gt;xcorr(A)&amp;lt;/tt&amp;gt; returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create an array whose columns contain the dx values for each particle&lt;br /&gt;
&lt;br /&gt;
for i = 1:particleCount&lt;br /&gt;
    allDx(:,i) = particle{i}.dx&#039;;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% compute all possible auto and cross correlations&lt;br /&gt;
&lt;br /&gt;
c = xcorr(allDx, &#039;coeff&#039;);&lt;br /&gt;
&lt;br /&gt;
% plot the results&lt;br /&gt;
clf;&lt;br /&gt;
hold on;&lt;br /&gt;
for i=1:size(c,1)&lt;br /&gt;
    plot(xaxis, c(:,i),&#039;color&#039;,rand(1,3));&lt;br /&gt;
end&lt;br /&gt;
hold off;&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;All Possible Auto and Cross Correlations in the x Dimension&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Correlated Trajectory==&lt;br /&gt;
&lt;br /&gt;
[[Image:FancyStatistics_09.png|right|thumb|300px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
x     = zeros(1,N);&lt;br /&gt;
c     = 0.80;           % degree of correlation; 0 to 1&lt;br /&gt;
step  = randn(1,N);&lt;br /&gt;
x(2) = randn();&lt;br /&gt;
&lt;br /&gt;
for t=2:N&lt;br /&gt;
     x(t) = (c * x(t-1)) + ((1-c)*step(t));&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
clf;&lt;br /&gt;
plot(xaxis, xcorr(x, &#039;coeff&#039;));&lt;br /&gt;
&lt;br /&gt;
xlabel(&#039;Lag&#039;);&lt;br /&gt;
ylabel(&#039;Correlation Coefficient&#039;);&lt;br /&gt;
title(&#039;Autocorrelation&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==notes &amp;amp; articles==&lt;br /&gt;
&lt;br /&gt;
Here are several good resources for explaining and calculating MSD:&lt;br /&gt;
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]&lt;br /&gt;
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]&lt;br /&gt;
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]&lt;br /&gt;
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]&lt;br /&gt;
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active diffusion positions the nucleus in mouse oocytes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Stochastic Diffusion]]&lt;br /&gt;
*[[Category:Diffusion]]&lt;br /&gt;
*[[Category:Neurobiology]]&lt;br /&gt;
*[[Category:Math]]&lt;br /&gt;
*[[Category:Statistics]]&lt;br /&gt;
*[[Category:Synaptic Plasticity]]&lt;br /&gt;
*[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Bradley Monk</name></author>
	</entry>
</feed>