How can I write such an equation on Matlab?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Torsten
2017년 2월 1일
To compute x(1), you will need x(0) and x(-1).
What is x(-1) ?
Best wishes
Torsten.
채택된 답변
Niels
2017년 2월 1일
편집: Niels
2017년 2월 1일
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
댓글 수: 0
추가 답변 (2개)
Niels
2017년 2월 1일
편집: Niels
2017년 2월 1일
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
Niels
2017년 2월 1일
편집: Niels
2017년 2월 1일
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
Roger Stafford
2017년 2월 2일
편집: Roger Stafford
2017년 2월 2일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!