Iteration error iter=0
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
heidi pham
am 24 Dez. 2018
Bearbeitet: heidi pham
am 25 Dez. 2018
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 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 24 Dez. 2018
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.
1 Kommentar
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!