Question

Matlab While loop help I'm doing a random walk problem and i need to make it...

Matlab While loop help

I'm doing a random walk problem and i need to make it so that when the walker goes over a certain position it gets reflected back onto the center (position=0). For this I'm trying to use while loops but they seem to get stuck. The program works fine until it reaches one of its limits (-10 or 10) then the while loops are supposed to prevent the position from going further and instead send the position closer to the center. The while loops seem to do this only once, then the program gets stuck and has to be forcefully ended. I suspect that there is a problem with how the while loops are written. How do I correct this problem?

num_stp=2000;   
position=zeros(size(num_stp));
for i=1:num_stp   
walk(i)=randi(2);   
if walk(i)==1
position(i+1)= position(i)-1;
elseif walk(i)==2
position(i+1)= position(i)+1;
  
while position(i)<=-10
if walk(i)==1
position(i+1)=position(i)+1;
elseif walk(i)==2
position(i+1)=position(i)+1;
end
end
  
while position(i)>=10
if walk(i)==1
position(i+1)=position(i)-1;
elseif walk(i)==2
position(i+1)=position(i)-1;
end
end
  
end
end

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Matlab While loop help I'm doing a random walk problem and i need to make it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • I need help in this problem but not with the physics part, I'm stuck with the...

    I need help in this problem but not with the physics part, I'm stuck with the algebra, the book skips some algebra steps and I'me getting stuck there, I need to understand how they eliminate K and q3, and also when they are solving for x, what happened to the other x, if you could do that part doing all the algebra steps I would appreciate it so much. Thanks Example 23.3 Where ls the Net Force Zero? AM Three...

  • Problem 1: Random Walk A random walk is a stochastic process. A stochastic process is a series of...

    python / visual studio Problem 1: Random Walk A random walk is a stochastic process. A stochastic process is a series of values that are not determined functionally, but probabilistically. The random walk is supposed to describe an inebriated person who, starting from the bar, intends to walk home, but because of intoxication instead randomly takes single steps either forward or backward, left or right. The person has no memory of any steps taken, so theoretically, the person shouldn't move...

  • I am creating a MATLAB game for my school project. The goal of the game is...

    I am creating a MATLAB game for my school project. The goal of the game is to create a 'Treasure Hunt Game' that asks the user to input the number of players, the difficult (easy, medium, or hard), and asks the user(s) to pick spots on a matrix until the correct spot is chosen, therefore winning the game. If a player misses the spot, the command window doesn't show how far away the treasure is, but what direction it is...

  • python / visual studio Problem 1: Random Walk A random walk is a stochastic process. A...

    python / visual studio Problem 1: Random Walk A random walk is a stochastic process. A stochastic process is a series of values that are not determined functionally, but probabilistically. The random walk is supposed to describe an inebriated person who, starting from the bar, intends to walk home, but because of intoxication instead randomly takes single steps either forward or backward, left or right. The person has no memory of any steps taken, so theoretically, the person shouldn't move...

  • Need help. Stuck and not sure how to finish this program. I'm using MatLab R2016a and...

    Need help. Stuck and not sure how to finish this program. I'm using MatLab R2016a and would appreciate any help. Thanks. In this project, you will perform transient analysis on a L 3 cm steel cube. The steel has the following properties: k 50 WImK, p 7800 kg/m3, cp 500 J/kgK. The cube is initially at a uniform temperature of 800 deg C, then is dropped into an oil bath at To 40 deg C. I. Create a program that...

  • Hi I got help on the above problem. While I now have the answer I'm still...

    Hi I got help on the above problem. While I now have the answer I'm still unsure algebratically how the person who solved this problem solved for q. So I get E=kq/r^2 but then solving for q. How did E end up negative? Did they flip fractions and when? did the subtract something? Can someone step by step break this down(like i'm literally in 3rd grade all over again) lol. Agh seriously you all are so much help!!!!The best! ....

  • Starting out with Python 4th edition I need help with while loops I can't even get...

    Starting out with Python 4th edition I need help with while loops I can't even get one started correctly. Write a program a program that predicts the approximate size of a population of organisms. Problem 4.13 – population Use a while loop; there are no text boxes, just regular input statements Enter number of organisms: 2 Enter average daily increase: 30 Enter number of days to multiply: 10 DAY       APPROXIMATE POPULATION ------------------------------------------------- 1            2 2            2.600 3            3.380 4            4.394...

  • Hello! i need help with a problem in this textbook, Essential MATLAB, 6th edition. Question 8.11:...

    Hello! i need help with a problem in this textbook, Essential MATLAB, 6th edition. Question 8.11: Use the Taylor Series, cosx=1- (x2/2!)+(x4/4!)-(x6/6!)+......... To write a program to compute cosx correct to 4 decimal places )x is in radians). see how many terms are needed to get 4 figure agreement with the MATLAB function cos. Dont make x too large; that could cause rounding errors. I can get an out put correct up to about only 10 radians, but then it...

  • Tried numerous times but I think I'm doing this problem wrong. Will give thumb's up for...

    Tried numerous times but I think I'm doing this problem wrong. Will give thumb's up for help. JAVA starter code: import java.util.Scanner; public class ArraySorter { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in k, which represents the maximum // distance between a number's current position // and sorted position int k = Integer.parseInt(sc.nextLine());    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers =...

  • need help adjusting my while loop to avoid this error, this is in matlab 1 clear...

    need help adjusting my while loop to avoid this error, this is in matlab 1 clear all close all 2 3 4 5 6 Egivens EO = 3; Sinput voltage ls = 7*10^-14; Svalue of ls R = 200; $value of resistor C = .026; $value of KT/ 7 8 9 10- V_vec = []; 11 vdiode (1) = 1; $initial guess for Vdiode 12 i=1; 13 14 %iterate until difference between last 2 Vdiodes < 10^-6 15 - while...

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