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...
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 path (or trajectory) of a s...

    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...

  • Jupyter code (PYTHON) True error Write a function called trueError that takes the following inputs: •...

    Jupyter code (PYTHON) True error Write a function called trueError that takes the following inputs: • X, the new value • Xtrue, the previous value And returns the following output: • error, the calculated relative error The formula for the relative error is shown below: errue = 11100% In (): def trueError(x,xtrue): # YOUR CODE HERE raise Not ImplementedError() In (): # You can call and test your function here # YOUR CODE HERE raise Not ImplementedError()

  • 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...

  • #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....

  • 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...

  • Write Python code that uses the np.split function to break up the array z created in...

    Write Python code that uses the np.split function to break up the array z created in the previous problem into four arrays named with two columns each named zOne, zTwo, zThree, and zFour. The following is the code to get z: import numpy as np x = np.array ([4,4,11,11,0,7,10,6,9,11,14,6,4,5,6],dtype='int64').reshape((3, 5)) y = np.array ([6,7,4,2,2,7,5,2,3],dtype='int64').reshape((3, 3)) z = np.concatenate([x,y],axis=1)

  • 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

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • Write a Python Code for a Function: you need to come up with code for shift_char....

    Write a Python Code for a Function: you need to come up with code for shift_char. Write the code in a way in which you can shift the character by writing the number of shifts. Use the ASCII code for this. For example in lie 11, the input for char_to shift is r. U shift it by 1 so it becomes s. below is the function def shift_char(char_to_shift, amount_to_shift): ''' shifts any character by moving it a certain amount on...

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