Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with MatLab! (other programs such as Python, do not work with this because I need to turn it in so that MatLab can correctly

Help with MatLab! (other programs such as Python, do not work with this because I need to turn it in so that MatLab can correctly read it. Thanks!)

Hi! Need someone to check my work please! I do have some error messages within the code so but when I run it I feel like Im getting the answers but not sure if theyre correct. Below is my goals I must do for the assignment. I believe I did everything but Im not sure if its correct! See the pic for the assignment!

image text in transcribed

With my function I created, my issue is that I keep getting the message the variable xxxxx appears to change size for every loop iteration for "position", "walklength", 'finalposition", and "walkpaths". I tried looking up the error but I'm not sure what to do entirely.

This is my function:

function [walklength,finalposition,walkpaths] = rwalk1(wall,startingpoint,drift,Nwalk)

%randomwalk implements a random walk with steps drawn from a normal distribution (mean 0, standard deviation 1). The random walk terminated

%when it reaches a boundary specified by user input

%INPUT:

%wall - location of the boundaries. Function assumes there is a boundary at

% +wall and at -wall(Optional, defaults to 5)

%startingpoint - starting point for the random walk. (Optional, defaults to 0)

%drift - drift rate towards one of the walls as a result of information in

%the stimulus (Optional, defaults to 0)

%Nwalk - number of random walks to calculate. (Optional, defaults to 1)

%

%OUTPUT:

%walklength - vector containing the number of steps taken in each walk.

%finalposition - vector containing the ending position for each walk

%walkpaths - cell array that contains the path taken in each random walk.

%

%Modification History

%created on 2/17/2016 by RS.

%made useful on 2/24/2018 by AK

%test for input arguments

if nargin >4 %nargin = number of input arguments

error ('too many input arguments')

end

%test for empty variables ('=[]')

if isempty(wall) %check if wall is an empty vector

wall = 5;

end

if isempty(startingpoint) %check if startingpoint is an empty vector

startingpoint = 0;

end

if isempty(drift) %check if drift is an empty vector

drift = 0;

end

if isempty(Nwalk) %check if Nwalk is an empty vector

Nwalk = 5;

end

%apply defaults

if nargin == 3 %start if statement for optional parameters

Nwalk = 5;

elseif nargin == 2 %start if statement for optional parameters

drift = 0;

Nwalk = 5;

elseif nargin == 1 %start if statement for optional parameters

startingpoint = 0;

drift = 0;

Nwalk = 5;

elseif nargin == 0 %start if statement for optional parameters

wall = 5; %defaults to five for location of boundaries

startingpoint = 0; %defaults to zero for starting point for random walk

drift = 0; %defaults to zero for drift rate towards one of the walls due to information in the stimulus

Nwalk = 5; %defaults to five random walks

end %end if statement for optional parameters

%test startingpoint

if startingpoint > wall || startingpoint

error ('startingpoint input is outside of walls')

end

for w = 1:Nwalk %loop for random walkers

position = []; %I need to initialize an empty random walk

steps = 1; %initialize at step 1 - note this is before any steps are taken;

position(steps) = startingpoint; %start the random walker at the starting point;

while position(steps) -wall %loop that stops when walker reaches wall

position(steps+1) = position(steps) + drift+ randn(1,1); %add a step; include drift

steps = steps + 1; %increment steps counter

end %close while loop

walklength(w) = steps-1; %save the number of steps for this walk to a vector

%note, subtracted 1 because the starting point was counted as step 1.

finalposition(w)= position (steps); %final position at the end

if nargout > 1

walkpaths{w} = position; %save the path of this walk to a cell array

end %close if loop for nargout

end %close for loop for random walkers.

end

Next, with my example script I'm getting a warning message like so: "The value assigned here 'variable' appears to be unused. Consider replacing it by ~." However, when I run each script I'm getting the outputs I need. I'm just not sure how to know if it's calculating them correctly. Each case resulted in a 1x5 matrix for finalposition and walklength, walkpaths (w/1x5 cell) but I'm not sure if they are correct.

%how to use function rwalk.m

%% Case 1

%This function can be used with no input variables by assuming the default value

%call to function

[walklength, finalposition, walkpaths] = rwalk();

%% Case 2

% This function can also be used with only one parameter assuming the

% default value for the others

%PARAMETERS

walls = 15;

%call to function

[walklength, finalposition, walkpaths] = rwalk(15);

%% Case 3

% This function can also be used with defaults for all parameters, except the number of walkers which is 20.

[walklength, finalposition,walklengths] = rwalk([],[],[],20);

%% Case 4

% This function can also be used to work for anything intermediate as well, including changing the number of output arguments.

[walklength, finalposition] = rwalk(15,[],0.5,1000);

Also, if you see any errors or if I didn't do something, I'd appreciate it if you could point it out! Thanks again, and I am extremely sorry for the inconvenience. I look forward to anyones reply!

Modify function to return final position of each walk. Inputs: wall, starting point, drift, Nwalk outputs: walklength, finalposition, walkpaths Default values: Wall = 5; startingpoint = 0; drift = 0; Nwalk = 5; ** function should work with only some input variables (even with no inputs) by assuming default values, and also work if parameter is an empty vector (e.g. wall = []) by using default values. Function should be "ldiot Proof" Function must test if too many parameters are provided startingpoint must be located in between the walls In case of either of these errors, use the error function to display an error message and exit. Next, make a separate example script called examplerwalk.m that shows how to run it for these 4 cases (1) [walklength, na!position, walkpaths] = rwalk(); - Function should work with no inputs provided and use all defaults!! (2) [walk!ength, nalposition, walkpaths) = rwalk(15); in this case, use defaults for parameters except walls (3) [walklength, nalposition,walklengths] rwalk ([),[1,0,20); - In this case, function should use the defaults for all parameters except the number of walkers which is 20 (4) [walklength, na!position] = rwalk(15,[],0.5,1000): Of course it should work for anything intermediate as well, including changing the number of output arguments. Modify function to return final position of each walk. Inputs: wall, starting point, drift, Nwalk outputs: walklength, finalposition, walkpaths Default values: Wall = 5; startingpoint = 0; drift = 0; Nwalk = 5; ** function should work with only some input variables (even with no inputs) by assuming default values, and also work if parameter is an empty vector (e.g. wall = []) by using default values. Function should be "ldiot Proof" Function must test if too many parameters are provided startingpoint must be located in between the walls In case of either of these errors, use the error function to display an error message and exit. Next, make a separate example script called examplerwalk.m that shows how to run it for these 4 cases (1) [walklength, na!position, walkpaths] = rwalk(); - Function should work with no inputs provided and use all defaults!! (2) [walk!ength, nalposition, walkpaths) = rwalk(15); in this case, use defaults for parameters except walls (3) [walklength, nalposition,walklengths] rwalk ([),[1,0,20); - In this case, function should use the defaults for all parameters except the number of walkers which is 20 (4) [walklength, na!position] = rwalk(15,[],0.5,1000): Of course it should work for anything intermediate as well, including changing the number of output arguments

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions