How can I write such an equation on Matlab?

1 Ansicht (letzte 30 Tage)
N/A
N/A am 31 Jan. 2017
Kommentiert: N/A am 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.
  2 Kommentare
Torsten
Torsten am 1 Feb. 2017
To compute x(1), you will need x(0) and x(-1).
What is x(-1) ?
Best wishes
Torsten.
N/A
N/A am 1 Feb. 2017
Bearbeitet: N/A am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Niels
Niels am 1 Feb. 2017
Bearbeitet: Niels am 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

Weitere Antworten (2)

Niels
Niels am 1 Feb. 2017
Bearbeitet: Niels am 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
  3 Kommentare
Niels
Niels am 1 Feb. 2017
Bearbeitet: Niels am 1 Feb. 2017
you didnt write any function, look at what i did
function result=rekursiv(xn)
this is the start of a function that you call. + you changed my code. why didnt you just copy my code and filled in what was missing or commented what you are trying to do?
% 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 t?
u=unifrnd(0,1,N,1);
epsilon=norminv(u,0,1);
%define the variables
n=25;
x_n=rekursiv(n,epsilon) % didnt notice before that epsilon is related to t
% define the model
function result=rekursiv(xn,epsilon)
if xn==-1
result=0;
elseif xn==0
result=1;
else
result = rekursiv(xn-1,epsilon)-0.01* rekursiv(xn-2,epsilon)+0.1* rekursiv(xn-1,epsilon)* epsilon(xn);
end
end
note that rekursion is a very slow way to calculate anything. every time this function is called, it calls itself 3 more times, so if t /or n=50 it calls itself 3^50 times, so you will have quite some time for the result for any t>20
and you cant just guess some value for t=-1, or maybe you can but the result may be some random values. anyway good luck trying.
Edit: you can also use the explicit way with a for loop. That's probably what you have been trying. Coming soon
N/A
N/A am 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.

Melden Sie sich an, um zu kommentieren.


Roger Stafford
Roger Stafford am 2 Feb. 2017
Bearbeitet: Roger Stafford am 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.
  1 Kommentar
N/A
N/A am 2 Feb. 2017
Yes, e(t) is a Normal(0,1), so it's where the stochastic property comes in..

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by