Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % EA 1, Homework program assignment 3
- %
- % Name: Dhileepan, Vishal
- % Section: 21
- % Date: 10/18/2018
- %Task 1
- %Prompt the user for an input
- steps = input('How many steps would you like to take? ');
- %Creates matrix of possible random movements
- possible_points = randi([1,4],steps,1);
- figure; %creates a new figure for a plot
- h= animatedline(0,0); %initializes an animation of the random walk at the origin
- x=0;
- y=0;
- %Iterates the loop for all steps
- for i=1:steps
- if possible_points(i) == 1
- x = x + 1;
- elseif possible_points(i) == 2
- x = x - 1;
- elseif possible_points(i) == 3
- y = y + 1;
- elseif possible_points(i) == 4
- y = y - 1;
- end
- addpoints(h,x,y); %adds a line to point (x,y)
- drawnow %displays the current plot where x and y are the coordinates of the new position
- end
- %Task 2
- %Prompt the user for inputs
- steps = input('How many steps would you like to take? ');
- trials = input('How many trials would you like? ');
- trial = 1;
- trial_vector = zeros(1,trials);
- x
- y
- figure;
- %Iterates the loop for the number of trials
- while trial<=trials
- possible_points = randi([1,4],steps,1);
- x=0;
- y=0;
- %Iterates the loop for all steps
- for i=1:steps
- if possible_points(i) == 1
- x = x + 1;
- elseif possible_points(i) == 2
- x = x - 1;
- elseif possible_points(i) == 3
- y = y + 1;
- elseif possible_points(i) == 4
- y = y - 1;
- end
- final_distance = sqrt(x^2+y^2);
- end
- trial_vector(trial) = final_distance;
- trial = trial+1;
- end
- trial_vector
- average_distance = sum(trial_vector)/trials
- histogram(trial_vector)
- xlabel('Distance')
- ylabel('Number of instances')
- %Task 3
- %Prompt the user for inputs
- steps = input('How many steps would you like to take? ');
- trials = input('How many trials would you like? ');
- distance_threshold = input('Enter a distance threshold array: ');
- steps_array = [1:length(distance_threshold)];
- trial=1;
- figure;
- %Iterates the loop for all values of the distance threshold
- for j = 1:length(distance_threshold)
- %Iterates the loop for all trials
- while trial <= trials
- if mod(trial, 400) == 0
- fprintf("distance_threshold= %6.2f, trial= %6.2f\n", distance_threshold(j), trial);
- end
- step = 1;
- x=0;
- y=0;
- %Iterates the loop for all steps
- while step <= steps
- possible_points = randi([1,4],steps,1);
- distance = sqrt(x^2 + y^2);
- if distance >= distance_threshold(j)
- steps_array(j) = steps_array(j) + step;
- break
- end
- if possible_points(i) == 1
- x = x + 1;
- elseif possible_points(i) == 2
- x = x - 1;
- elseif possible_points(i) == 3
- y = y + 1;
- elseif possible_points(i) == 4
- y = y - 1;
- step = step+1;
- end
- if step > steps
- fprintf("Maximum number of steps exceeded!\n");
- break
- end
- end
- trial = trial+1;
- end
- trial = 1;
- end
- %Calculating and printing average number of steps
- fprintf("(distance, average number of steps)\n");
- hold on;
- for j = 1:length(distance_threshold)
- fprintf("(%6.2f, %6.2f)\n", distance_threshold(j), steps_array(j)/trials);
- end
- plot(distance_threshold, steps_array/steps);
- xlabel("Distance threshold");
- ylabel("Average step count");
- %%Outputs
- %Task 1:
- %The final position is (0,-10)
- %The distance from the origin is 10
- %Task 2
- %The average distance was 39.5620
- %Task 3
- %distance_threshold= 5.00, trial= 400.00
- % distance_threshold= 5.00, trial= 800.00
- % distance_threshold= 5.00, trial= 1200.00
- % distance_threshold= 5.00, trial= 1600.00
- % distance_threshold= 5.00, trial= 2000.00
- % distance_threshold= 10.00, trial= 400.00
- % distance_threshold= 10.00, trial= 800.00
- % distance_threshold= 10.00, trial= 1200.00
- % distance_threshold= 10.00, trial= 1600.00
- % distance_threshold= 10.00, trial= 2000.00
- % distance_threshold= 15.00, trial= 400.00
- % distance_threshold= 15.00, trial= 800.00
- % distance_threshold= 15.00, trial= 1200.00
- % distance_threshold= 15.00, trial= 1600.00
- % distance_threshold= 15.00, trial= 2000.00
- % distance_threshold= 20.00, trial= 400.00
- % distance_threshold= 20.00, trial= 800.00
- % distance_threshold= 20.00, trial= 1200.00
- % distance_threshold= 20.00, trial= 1600.00
- % distance_threshold= 20.00, trial= 2000.00
- % distance_threshold= 25.00, trial= 400.00
- % distance_threshold= 25.00, trial= 800.00
- % distance_threshold= 25.00, trial= 1200.00
- % distance_threshold= 25.00, trial= 1600.00
- % distance_threshold= 25.00, trial= 2000.00
- % distance_threshold= 30.00, trial= 400.00
- % distance_threshold= 30.00, trial= 800.00
- % distance_threshold= 30.00, trial= 1200.00
- % distance_threshold= 30.00, trial= 1600.00
- % distance_threshold= 30.00, trial= 2000.00
- % distance_threshold= 35.00, trial= 400.00
- % distance_threshold= 35.00, trial= 800.00
- % distance_threshold= 35.00, trial= 1200.00
- % distance_threshold= 35.00, trial= 1600.00
- % distance_threshold= 35.00, trial= 2000.00
- % distance_threshold= 40.00, trial= 400.00
- % distance_threshold= 40.00, trial= 800.00
- % distance_threshold= 40.00, trial= 1200.00
- % distance_threshold= 40.00, trial= 1600.00
- % distance_threshold= 40.00, trial= 2000.00
- % distance_threshold= 45.00, trial= 400.00
- % distance_threshold= 45.00, trial= 800.00
- % distance_threshold= 45.00, trial= 1200.00
- % distance_threshold= 45.00, trial= 1600.00
- % distance_threshold= 45.00, trial= 2000.00
- % distance_threshold= 50.00, trial= 400.00
- % distance_threshold= 50.00, trial= 800.00
- % distance_threshold= 50.00, trial= 1200.00
- % distance_threshold= 50.00, trial= 1600.00
- % distance_threshold= 50.00, trial= 2000.00
- % (distance, average number of steps)
- % ( 5.00, 7.41)
- % ( 10.00, 26.77)
- % ( 15.00, 60.50)
- % ( 20.00, 101.89)
- % ( 25.00, 158.06)
- % ( 30.00, 233.35)
- % ( 35.00, 315.02)
- % ( 40.00, 413.74)
- % ( 45.00, 507.54)
- % ( 50.00, 638.38)
- %The specific function that approximates this is line 124, where the steps
- %array divides the number of trials that is chosen by the user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement