1 view (last 30 days)

Show older comments

Hello everyone,

I need to know how to write such an equation on Matlab, and define what is x(t) before computing it:

Furthermore, how can I get the distribution of x(50), plot its histogram and compute its standard deviation?

Thank you in advance.

Niels
on 1 Feb 2017

Edited: Niels
on 1 Feb 2017

i am not implementing your homework, i am just put the code in the right order so that it looks like you wanted it to do in the way your showed me so far. To me it still looks like this code probably isnt the monte carlo simulation, but that might be your homework to figure out how it works.

function [x_t] = x(t) % you call the function with x(anyvalue) to get x_t

% if you saved it as x.m in the current folder

% define the number of simulations

Nsim= 10000; %%====== is unused

% extract values from epsilon

% epsilon is distributed as a standard normal

N = 10000; %======= shouldnt be this the same as your n /or x?

u=unifrnd(0,1,N,1);

epsilon=norminv(u,0,1);

%define the variables

% n=25; % not needed anymore

% x_t=0;

x_t1=0; % x_t-1

x_t2=1; % x_t-2

for i=1:t

x_t = x_t1 -0.01* x_t2 + 0.1* x_t1* epsilon(i);

x_t2 = x_t1;

x_t1 = x_t;

end

end

type

x(50)

in your command window to get x_50

Niels
on 1 Feb 2017

Edited: Niels
on 1 Feb 2017

In general you would write a recursiv function

function result=rekursiv(xn)

if xn==-1

result=missingInYourCode;

elseif xn==0

result=1;

else

result=rekursiv(xn-1)- 0.01* rekursiv(xn-2)...;

end

end

When rekursiv(1) is called there the value of x_-1 is needed. No x_-1 => no solution

Roger Stafford
on 2 Feb 2017

Edited: Roger Stafford
on 2 Feb 2017

Recursion is not a good idea on an iteration of this sort where at each step two different earlier levels are consulted. At the first step there must be two recursive calls. At the second step there must be two calls, each of which requires two calls. As you progress along the iteration, the number of calls expands exponentially. This is of course a great waste because there will be recursive calls made repeatedly for the same value of i.

It is much, much better to do the iteration with a for-loop or while-loop:

for i = 2:50

x(i) = x(i-1)*(1+0.1*e(t(i)))-0.01*x(i-2);

end

Note: you haven’t told us what e(t) is. Is this where the stochastic property comes in? If so, you would have the repeat the above loop a great many times to simulate this stochastic behavior accurately.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!