Iteration error iter=0

2 visualizzazioni (ultimi 30 giorni)
heidi pham
heidi pham il 24 Dic 2018
Modificato: heidi pham il 25 Dic 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;

Risposta accettata

Geoff Hayes
Geoff Hayes il 24 Dic 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 Commento
heidi pham
heidi pham il 24 Dic 2018
Modificato: heidi pham il 25 Dic 2018
Thank you so so much.
I see what is wrong now: "which is less than TOL and so you never enter the loop."
that's why i got the result is 0 for all element of X.
I just meant to allocate memory by puting X a vector of zeros, then update it. But it causes the error as you pointed down.
I will revise my code and redo it to see if it goes well.
Thank you again!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by