Iteration error iter=0
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to solve a 2nd order difference equation with given bounds. Specifically:
I need to solve for a sequence of
( for t = 1,2,...,50) such that:
$X_{t+2} = f( X_{t+1}, X_t) $, the exact function is inside the below loop.
Here is my code to solve this system, using shooting method. And it is very strange that I got a sequence of X are all equal for 0 for all t. And more strange, the number of iterations executed (which is denoted iter in the loop are 0).
I have been checking for hours, but could not find the mistake. If any one see what is wrong with my code, plz kindly let me know.
Thanks so much,
T = 5;
X = zeros(T+1,1)
X(1) = 10;
iter=0
while (abs(X(T+1)) > TOL);
X(2) = (LEFT + RIGHT)/2;
for i = 1:T-1;
X(i+2,1) = A * (1 + ALPHA * BETA)* (X(i+1,1))^ALPHA + (1-DELTA) * (1 + BETA) * X(i+1,1)...
- BETA * (A * (X(i,1))^ALPHA + (1 - DELTA) * X(i,1)) * (A * ALPHA * X(i+1,1)^(ALPHA - 1) + (1 - DELTA));
end;
if X(T+1,1)< 0
LEFT = X(2,1);
elseif X(T+1,1) > 0
RIGHT = X(2,1);
else break;
end;
iter=iter+1
end;
0 个评论
采纳的回答
Geoff Hayes
2018-12-24
heidi - try using the MATLAB debugger to step through your code to determine what is going on. Note the following code
T = 5;
X = zeros(T+1,1)
X(1) = 10;
iter=0
while (abs(X(T+1)) > TOL);
What is T supposed to represent? The maximum number of iterations or something else? Should you increment it on each iteration of the while loop? See how X is a 6x1 array of zeros except for the first element which is 10. Then, in the condition for the loop, you are accessing the sixth element (since T is five) of X which is zero...which is less than TOL and so you never enter the loop.
If X is supposed to represent all of the outputs on each iteration of the loop, then you probably want to initialize T to be one and then increment on each iteration and setting X(T) to be some value...but that will conflict with this code
for i = 1:T-1;
X(i+2,1) = A * (1 + ALPHA * BETA)* (X(i+1,1))^ALPHA + (1-DELTA) * (1 + BETA) * X(i+1,1)...
- BETA * (A * (X(i,1))^ALPHA + (1 - DELTA) * X(i,1)) * (A * ALPHA * X(i+1,1)^(ALPHA - 1) + (1 - DELTA));
end;
Perhaps this the shooting part of the algorithm (which I don't understand). Please use the debugger or add some comments to your code which describe what is being attempted.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!