Matlab: Plotting multiple paths (random walk)

In summary, the conversation discusses modifying a code to generate 1000 paths and estimate the variance with n = 100 for specific time intervals. The code uses a scaled random walk and a uniform sample of 1000 steps. The process of generating 1000 paths is explained and the variance is estimated for different time intervals.
  • #1
spenghali
14
0
I have created a code to simulate a scaled random walk, but I need to now modify it to generate 1000 paths and then estimate the variance with n = 100, for t = 0.25, 0.5, 0.75 and 1. Can anyone help out with how to generate all 1000 paths? Sorry if this is painfully easy, I am a complete matlab/coding noob.

clear;
N = 10
t = (0:1:N)';
X = [0; cumsum(2*(rand(N,1)>0.5)-1)];

plot(t/N,X(1:N+1)/sqrt(N));
axis([0 1 -2 2]);
title(['Uniform Sample 1000 steps'])

ylabel('Position')
xlabel('Time')
 
Last edited:
Physics news on Phys.org
  • #2
% Generate 1000 pathsfor i = 1:1000 X_1000(i,:) = [0; cumsum(2*(rand(N,1)>0.5)-1)]/sqrt(N);end% Estimate variance for t = 0.25, 0.5, 0.75, and 1for k = 1:4 var_est(k) = mean((X_1000(:,k*2+1)).^2);end
 

1. What is a random walk in Matlab?

A random walk in Matlab is a simulation of a random process where an object moves randomly in discrete steps. This can be used to model various phenomena such as stock market prices, particle movement, or animal behavior.

2. How do I plot multiple paths of a random walk in Matlab?

To plot multiple paths of a random walk in Matlab, you can use the "plot" function with the coordinates of each path as inputs. You can also use a "for" loop to generate and plot multiple paths with different starting points or parameters.

3. How can I customize the appearance of my random walk plot in Matlab?

There are various ways to customize the appearance of a random walk plot in Matlab. You can use different line styles, colors, and markers to differentiate between paths. You can also add titles, labels, and legends to your plot for better visualization.

4. Can I save my random walk plot as an image or file in Matlab?

Yes, you can save your random walk plot as an image or file in Matlab. You can use the "saveas" function to save your plot in various formats such as PNG, JPEG, or PDF. You can also use the "print" function to save your plot as an image or file.

5. Is there a way to animate a random walk plot in Matlab?

Yes, you can animate a random walk plot in Matlab using the "pause" and "drawnow" functions. These functions will pause the execution of the code and update the plot at each iteration, creating an animation effect. You can also use the "VideoWriter" class to save your animation as a video file.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
697
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top