Question: 5.3) MATLAB Exercises 1. Extend the factorial function defined above, such that it checks for a positive input argument. 2. Write a function that calculates

5.3) MATLAB Exercises 1. Extend the factorial function defined above, such that it checks for a positive input argument. 2. Write a function that calculates a random walk trajectory based on the iteration xi+1 = xi + 5.3) MATLAB Exercises 1. Extend the factorial function defined above, such that , where it checks for a positive input argument. 2. Write a function that is a uniformly distributed random variable between -1 and 1. Plot the resulting trajectory. (Please change A for calculates a random walk trajectory based on the iteration xi+1 = xi in the formula in order to check previous calculus are correct it was my mistake on previous instructions) 3. Change the sorting algorithm given below such that it sorts the elements in descending order. Compare the speed of your function with the built-in MATLAB function sort.

4. Approximation of . The area of a circle is given by A = r^2 where r is the radius. Assume a circle with r = 0.5 embedded in a unit square. A point in the unit square is defined by p = (x,y) where 0 x,y 1. If we draw ntot times two uniformly distributed random numbers (x,y) and count how often the corresponding points falls into the circle (ncirc), we get an approximation of the circle area by ncirc tot and by that of the number . Write a function which approximates by the described method. How many draws do you need to get the rst three digits right?

Note: I forgot to put + , where is a uniformly distributed random variable between -1 and on instructions on part 2 replace in formula of part 2 where is A for epsilon (1. Plot the resulting trajectory. (Please change A for in the formula) in order to proceed to part 4 check if all previous results stay the same then proceed to part 4.

function y=calculate_factorial(n)

% check whether input argument is negative if (n1 n=n-1; y=y*n; % calculate the factorial value end end end

SCREENSHOT OF CODE:

in order to check previous calculus are correct it was my mistake

SAMPLE OUTPUT:

on previous instructions) 3. Change the sorting algorithm given below such that

part 2)

clc; clear all; close all;

n = 100; % Length of the x-axis, also known as the length of the random walks. m = 400; % The amount of random walks. xt(1) = 0; % start from the origin yt(1) = 0; for i=1:m for j = 1:n % Looping all values of N into xt(n). A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND. xt(j+1) = xt(j) + A; A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND. yt(j+1) = yt(j) + A; end end

plot(xt, yt); xlabel('x'); ylabel('y'); grid on title('2D random walk')

=============================== SCREENSHOT OF CODE

it sorts the elements in descending order. Compare the speed of your

============================================ SAMPLE OUTPUT

function with the built-in MATLAB function sort. 4. Approximation of . The

part 3 descending order

function v = gsort_reverse(v) pos = 2; while pos 2 % decrease position pos = pos - 1; end end end

area of a circle is given by A = r^2 where r

calculate factorial.m+ function y=calculate factorial (n) check whether input argument 13 negative warning ('Input argument must be non-negative'): disp'Ignoring the negative sign) n=abs (n) ; end % check whether input argument is zero if n 0 10 Y-1: 12else 13 % otherwise calculate the factorial y=n; while 15 16 n>1 n-n-1: y=y*n; % calculate the factorial value end end end Command Window > calculate factorial (-3) Warning: Input argument must be non-negative > In calculate factorial (line 5) Ignoring the negative sign ans - >> calculate_factorial (3) ans - >> calculate_factorial (0) ans - fx >> main_Script.m+ clc: clear all: close all: n = 100; % Length of the x-axis, also known as the length of the random walks. m 400; % The amount of random walks. xt (1) % start from the origin yt (1) = 0; for i-1:m 4- for 1 : n % Looping all values of N into xt (n) . % Generates either +1/-1 depending on the SIGN of RAND A 3ign (randn) ; xt(j+1) = xt (j) + A; A = 3ign (randn) ; 10 % Generates either +1/-1 depending on the SIGN of RAND 12 13 14 end end 16 17 plot (xt, yt); x1abe1 ( x' ) ; ylabel ( 'y') ; grid on title ('2D random wall) 20 2D random walk 2 -2 -4 -6 -8 -10 -12 -6 -2 2 -10 -8 function vgsort reverse (v) |pos = 2; while pos 2 % decrease position 10 pos pos1: 12 - end end end Command Window > gsort (I3,4, 6,1,5, 81) ans - 1 f >> calculate factorial.m+ function y=calculate factorial (n) check whether input argument 13 negative warning ('Input argument must be non-negative'): disp'Ignoring the negative sign) n=abs (n) ; end % check whether input argument is zero if n 0 10 Y-1: 12else 13 % otherwise calculate the factorial y=n; while 15 16 n>1 n-n-1: y=y*n; % calculate the factorial value end end end Command Window > calculate factorial (-3) Warning: Input argument must be non-negative > In calculate factorial (line 5) Ignoring the negative sign ans - >> calculate_factorial (3) ans - >> calculate_factorial (0) ans - fx >> main_Script.m+ clc: clear all: close all: n = 100; % Length of the x-axis, also known as the length of the random walks. m 400; % The amount of random walks. xt (1) % start from the origin yt (1) = 0; for i-1:m 4- for 1 : n % Looping all values of N into xt (n) . % Generates either +1/-1 depending on the SIGN of RAND A 3ign (randn) ; xt(j+1) = xt (j) + A; A = 3ign (randn) ; 10 % Generates either +1/-1 depending on the SIGN of RAND 12 13 14 end end 16 17 plot (xt, yt); x1abe1 ( x' ) ; ylabel ( 'y') ; grid on title ('2D random wall) 20 2D random walk 2 -2 -4 -6 -8 -10 -12 -6 -2 2 -10 -8 function vgsort reverse (v) |pos = 2; while pos 2 % decrease position 10 pos pos1: 12 - end end end Command Window > gsort (I3,4, 6,1,5, 81) ans - 1 f >>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!