MATLAB Answers

How can I write such an equation on Matlab?

1 view (last 30 days)
Randoman on 31 Jan 2017
Commented: Randoman on 2 Feb 2017
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.
Randoman on 1 Feb 2017
Unluckily, I don't have any instruction on x(-1). I need to write a loop for t= 0:Nsim (where Nsim is 10000) and as t changes, the equation adjusts accordingly. I suppose I could just write that x(-1) = 0.

Sign in to comment.

Accepted Answer

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?
%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;
in your command window to get x_50

More Answers (2)

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
elseif xn==0
result=rekursiv(xn-1)- 0.01* rekursiv(xn-2)...;
When rekursiv(1) is called there the value of x_-1 is needed. No x_-1 => no solution
Randoman on 1 Feb 2017
Ok I'm posting the whole assignment, so the problem is more clear:
Yes, I tried to write a for loop, in order to perform the Monte Carlo method. Thank you again for your kind help.

Sign in to comment.

Roger Stafford
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);
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.
  1 Comment
Randoman on 2 Feb 2017
Yes, e(t) is a Normal(0,1), so it's where the stochastic property comes in..

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by