Question

Problem 1 (2 Points) 1. Write a function randomWalk(.. .) which simulates one path (or trajectory) of a simple symmetric randCode in Python

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import numpy as np
import random
import matplotlib.pyplot as plt


def randomWalk(L):
  
# S is a 2d array where x = row1= S[0][:], y = row2 = S[1][:]
S = np.zeros(shape=(2,L+1))
directions = [1,2,3,4]
for i in range(1, L+1):
direction = np.random.choice(directions)
if direction == 1:
S[0][i] = S[0][i-1] + 1; S[1][i] = S[1][i-1] # go right
elif direction == 2:
S[0][i] = S[0][i-1] - 1; S[1][i] = S[1][i-1] # go left
elif direction == 3:
S[0][i] = S[0][i-1]; S[1][i] = S[1][i-1] + 1 # go up
else:
S[0][i] = S[0][i-1]; S[1][i] = S[1][i-1] - 1; # go down
samplePath = S
return samplePath


n = 100;
path = randomWalk(2*n)

print(path)

x = path[0][:]; y = path[1][:];
plt.figure(1)
plt.plot(x,y,'-')
plt.ylabel('y')
plt.xlabel('x')
plt.show()



import numpy as np import random import matplotlib.pyplot as plt def randomWalk (L) # S is a 2d array where x rowi- S [O] [:]

65561220331 10212111020 64661311421 00201111021 54752211430 01201120121 55663311321 11201131121 44574321221 1201230130 1 4366

8 6 4 2 0 2 2 1 0 3 1

COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Add a comment
Know the answer?
Add Answer to:
Code in Python Problem 1 (2 Points) 1. Write a function randomWalk(.. .) which simulates one path (or trajectory) of a s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code in Python Problem 1 (2 Points) 1. Write a function randomWalk(.. .) which simulates one...

    Code in Python Problem 1 (2 Points) 1. Write a function randomWalk(.. .) which simulates one path (or trajectory) of a simple symmetric random walk with 2N time steps (i.e. from 0,1,2,...,2N) starting at So-0 nput: lengthofRandomWalk2N Output: samplePath: Array of length 2N+1 with the entire path of the random walk on 0,1,2,..,2N In def randomwalk(lengthofRandomwalk): ## WRITE YOUR OWN CODE HERE # HINT: USE np. random . choice ( ) TO SIMULATE THE INCREMENTS return samplePath In [ ]:...

  • This problem investigates how you can use random numbers to simulate a computer dice game write a function called twooice that simulates the rolling of two sik-sided dice. The function takes no input...

    This problem investigates how you can use random numbers to simulate a computer dice game write a function called twooice that simulates the rolling of two sik-sided dice. The function takes no inputs. Instead, it generates two random integers between 1 and 6, and output their sum. You may submit your work as many times as you want Try to get 100%) Your Function MATLAB Documentation Reset Code to call your function C Reset 1s-tuoDice ss-twoDice This problem investigates how...

  • Write a python function that returns a list or array of tuples containing every permutation of...

    Write a python function that returns a list or array of tuples containing every permutation of numbers 1 to n. For example: permutate(1): [(1,)] permutate(2): [(1, 2), (2, 1)] permutate(3): [(1, 2, 3), (1, 3, 2), (3, 1, 2), (3, 2, 1), (2, 3, 1), (2, 1, 3)] #here is some starting code def permutate(n): result = [] return result

  • #PYTHON# In this exercise you will write code which loads a collection of images (which are...

    #PYTHON# In this exercise you will write code which loads a collection of images (which are all the same size), computes a pixelwise average of the images, and displays the resulting average. The images below give some examples that were generated by averaging "100 unique commemorative photographs culled from the internet" by Jason Salavon. Your program will do something similar. Write a function in the cell below that loads in one of the sets of images and computes their average....

  • please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords,...

    please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords, Y_coords) below, which should take two arrays of equal length containing the x-andy- coordinates for some number of particles. We'll use a very simple random walk algorithm • For each particle, choose a random angle between 0 and 2 • The particle moves by 1 unit of distance in the direction given by d.o. It is displaced by (Ax, Ay) (cos, sino). We'll do...

  • need help with matLab Question 1 (20 Points) Write a well-documented MATLAB script hmwk7Q1.m that simulates...

    need help with matLab Question 1 (20 Points) Write a well-documented MATLAB script hmwk7Q1.m that simulates tossing 100 coins into a unit square. As shown in the scatter plot. Location of Simulated Coins In Unit Square 1 o0 Ooo 05 04 03 02 oo 0.1 2 03 4 05 07 1 xpostion Hmwk7Q1.fig Consider organizing your MATLAB script into the following sections. % housekeeping (performs clearing of figures, workspace, and command lines) % Initialize the Number of Coins To Simulate...

  • Python program Use the provided shift function to create a caesar cipher program. Your program s...

    python program Use the provided shift function to create a caesar cipher program. Your program should have a menu to offer the following options: Read a file as current message Save current message Type in a new message Display current message "Encrypt" message Change the shift value For more details, see the comments in the provided code. NO GLOBAL VARIABLES! Complete the program found in assignment.py. You may not change any provided code. You may only complete the sections labeled:#YOUR...

  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

  • 1. Consider the following function F(x) x 2 where x = [x1 x2]T (d) Write a code to implement conj...

    1. Consider the following function F(x) x 2 where x = [x1 x2]T (d) Write a code to implement conjugate gradient method on this function. In each case, start with an initial guess of [1 1]T and plot both the solution at each iteration and the contour plots of the function on the same plot to show the trajectory towards the solution. Does it matter what the initial guess is? 1. Consider the following function F(x) x 2 where x...

  • Needs to be done in visual studio. I keep having problems with making my own function...

    Needs to be done in visual studio. I keep having problems with making my own function to call for the statistics and the random number generator. they are both supposed to be separate functions from the main. Can anyone help? Language is C Overview: This third programming assignment will be the first to require the use of arrays. Like the second program, this program will be modular and make use of functions (including, this time, functions which process arrays). Brief...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT