Prev: request for an algorithm or code
Next: struct_ array
From: Saurabh Mahapatra on 29 Jul 2010 22:09 These are my thoughts: I have a concern with the problem definition. I can always think of y1 and y2 are parameters of the equation. The way you have defined the problem, it makes most sense to think of them as constants during each iteration: > y = y1 - x1^2 - x1 > z = y2^2 - x2 - x2^2 Maxima of Y wrt x1 can be found at 2x1+1=0 (it doesn't matter what you guessed) Double derivative shows -2, hence a maxima. x1=--1/2 Maxima of Z wrt x2 is similar: 2x2+1=0 gives x2=-1/2. Is this the right answer really? Start with arbitrary x1_guess and x2_guess. x1 and x2 =-1/2 should give you the maxima(they are quadratics anyway). Iterate again. The equilibirum (or fixed point of this simple dynamical system) is -1/2 and -1/2. I am not sure what you were trying to achieve but a closed form solution already exists from this problem definition. An interesting problem for me would have been this: Write an algorithm that computes the fixed points of this system: y(n)=[(x1(n) +x2(n)] -x1(n+1)-[x1(n+1)]^2 z(n)=[[(x1(n) +x2(n)]^2 -x2(n+1)-[x2(n+1)]^2 with x(n+1) so chosen such that y(n)>y(n-1) and z(n)>z(n-1) The choice of x(n+1) to get quick convergence would be very interesting. Thanks, Saurabh > vector_guess = (x1_guess, x2_guess) > y1 = x1_guess + x2_guess > y2 = x2_guess^2 + x1_guess > y = y1 - x1^2 - x1 > z = y2^2 - x2 - x2^2 > x1_feedback = the value of x1 which maximizes y with respect to x1 > x2_feedback = the value of x2 which maximizes z with respect to x2 > vector_feedback = (x1_feedback, x2_feedback). > And then keep updating vector_guess until the “equilibrium” is reached. I have no clue how this can be programmed. Please help.
From: Anna Kaladze on 30 Jul 2010 05:11 "Saurabh Mahapatra" <saurabh.mahapatra(a)mathworks.com> wrote in message <i2tc80$pkv$1(a)fred.mathworks.com>... > These are my thoughts: I have a concern with the problem definition. > > I can always think of y1 and y2 are parameters of the equation. The way you have defined the problem, it makes most sense to think of them as constants during each iteration: > > y = y1 - x1^2 - x1 > > z = y2^2 - x2 - x2^2 > > Maxima of Y wrt x1 can be found at 2x1+1=0 (it doesn't matter what you guessed) > Double derivative shows -2, hence a maxima. x1=--1/2 > > Maxima of Z wrt x2 is similar: 2x2+1=0 gives x2=-1/2. > > Is this the right answer really? Start with arbitrary x1_guess and x2_guess. x1 and x2 =-1/2 should give you the maxima(they are quadratics anyway). > > Iterate again. The equilibirum (or fixed point of this simple dynamical system) is -1/2 and -1/2. > > I am not sure what you were trying to achieve but a closed form solution already exists from this problem definition. > > An interesting problem for me would have been this: > > Write an algorithm that computes the fixed points of this system: > > y(n)=[(x1(n) +x2(n)] -x1(n+1)-[x1(n+1)]^2 > z(n)=[[(x1(n) +x2(n)]^2 -x2(n+1)-[x2(n+1)]^2 > > with x(n+1) so chosen such that y(n)>y(n-1) and z(n)>z(n-1) > > The choice of x(n+1) to get quick convergence would be very interesting. > > Thanks, > > Saurabh > > > vector_guess = (x1_guess, x2_guess) > > y1 = x1_guess + x2_guess > > y2 = x2_guess^2 + x1_guess > > y = y1 - x1^2 - x1 > > z = y2^2 - x2 - x2^2 > > x1_feedback = the value of x1 which maximizes y with respect to x1 > > x2_feedback = the value of x2 which maximizes z with respect to x2 > > vector_feedback = (x1_feedback, x2_feedback). > > And then keep updating vector_guess until the “equilibrium” is reached. I have no clue how this can be programmed. Please help. Hi Saurabh: Thanks a lot for yur reply. The example I gave below was a simple one because all I need to do is to program the above step. My real y1, y2 functions are way too long and complex to post herer. Once I can program it in MATLAB (I suspect using "count" and "while" commands), then I will be able to use it im my complex model which involves complex equations in place of z, y, and many more steps in between. So, I am not asking about the non-MATLAB solution to the above problem, I just wonder if somene can kindly give me code how to program the above in MATLAB. I suspect I will NOT get helped at all, but please kindly note that I am not a free-rider and this is not an assignment (it is my independent research in Economics, which I can handle in Excel VB) but I need MATLAB and I just honestly declared that I know nothing at all about programming in MATLAB. The reason I have posted it here b/c I was told it is so simple that someone who just type in a few commans in no time. Thanks again for anyone who can and will want to help.
From: Torsten Hennig on 30 Jul 2010 02:29 > "Saurabh Mahapatra" <saurabh.mahapatra(a)mathworks.com> > wrote in message <i2tc80$pkv$1(a)fred.mathworks.com>... > > These are my thoughts: I have a concern with the > problem definition. > > > > I can always think of y1 and y2 are parameters of > the equation. The way you have defined the problem, > it makes most sense to think of them as constants > during each iteration: > > > y = y1 - x1^2 - x1 > > > z = y2^2 - x2 - x2^2 > > > > Maxima of Y wrt x1 can be found at 2x1+1=0 (it > doesn't matter what you guessed) > > Double derivative shows -2, hence a maxima. > x1=--1/2 > > > > Maxima of Z wrt x2 is similar: 2x2+1=0 gives > x2=-1/2. > > > > Is this the right answer really? Start with > arbitrary x1_guess and x2_guess. x1 and x2 =-1/2 > should give you the maxima(they are quadratics > anyway). > > > > Iterate again. The equilibirum (or fixed point of > this simple dynamical system) is -1/2 and -1/2. > > > > I am not sure what you were trying to achieve but a > closed form solution already exists from this problem > definition. > > > > An interesting problem for me would have been this: > > > > Write an algorithm that computes the fixed points > of this system: > > > > y(n)=[(x1(n) +x2(n)] -x1(n+1)-[x1(n+1)]^2 > > z(n)=[[(x1(n) +x2(n)]^2 -x2(n+1)-[x2(n+1)]^2 > > > > with x(n+1) so chosen such that y(n)>y(n-1) and > z(n)>z(n-1) > > > > The choice of x(n+1) to get quick convergence would > be very interesting. > > > > Thanks, > > > > Saurabh > > > > > vector_guess = (x1_guess, x2_guess) > > > y1 = x1_guess + x2_guess > > > y2 = x2_guess^2 + x1_guess > > > y = y1 - x1^2 - x1 > > > z = y2^2 - x2 - x2^2 > > > x1_feedback = the value of x1 which maximizes y > with respect to x1 > > > x2_feedback = the value of x2 which maximizes z > with respect to x2 > > > vector_feedback = (x1_feedback, x2_feedback). > > > And then keep updating vector_guess until the > “equilibrium” is reached. I have no clue > how this can be programmed. Please help. > Hi Saurabh: > Thanks a lot for yur reply. The example I gave below > was a simple one because all I need to do is to > program the above step. My real y1, y2 functions are > way too long and complex to post herer. Once I can > program it in MATLAB (I suspect using "count" and > "while" commands), then I will be able to use it im > my complex model which involves complex equations in > place of z, y, and many more steps in between. So, I > am not asking about the non-MATLAB solution to the > above problem, I just wonder if somene can kindly > give me code how to program the above in MATLAB. I > suspect I will NOT get helped at all, but please > kindly note that I am not a free-rider and this is > not an assignment (it is my independent research in > Economics, which I can handle in Excel VB) but I need > MATLAB and I just honestly declared that I know > nothing at all about programming in MATLAB. The > reason I have posted it here b/c > I was told it is so simple that someone who just type > in a few commans in no time. Thanks again for anyone > who can and will want to help. I'm not a specialist in MATLAB syntax ; so the following code fragment may contain errors. If you have problems, maybe other users of the forum can help. x_old(1) = 5; x_old(2) = 10; error=1.0; x_lower = -20.0; x_upper = 20.0; while error > 0.01 y_array(1) = x_old(1)+x_old(2); y_array(2) = x_old(2)^2+x_old(1); y = @(x) y_array(1)-x^2-x; z = @(x) y_array(2)^2-x-x^2; x_new(1) = fminbnd(y,x_lower,x_upper); x_new(2) = fminbnd(z,x_lower,x_upper); error = norm(x_old-x_new); x_old = x_new; end But I doubt this can serve as a basis to solve such difficult equilibrium problems you are talking about. I made the experience that it makes much more sense to understand and use good existing code than to do code development on one's own. Make a google search to find MATLAB code for solving dynamic equilibrium models in macroeconomics - there are plenty of them. Good luck ! Best wishes Torsten.
From: Torsten Hennig on 30 Jul 2010 02:57 > > "Saurabh Mahapatra" > <saurabh.mahapatra(a)mathworks.com> > > wrote in message > <i2tc80$pkv$1(a)fred.mathworks.com>... > > > These are my thoughts: I have a concern with the > > problem definition. > > > > > > I can always think of y1 and y2 are parameters > of > > the equation. The way you have defined the > problem, > > it makes most sense to think of them as constants > > during each iteration: > > > > y = y1 - x1^2 - x1 > > > > z = y2^2 - x2 - x2^2 > > > > > > Maxima of Y wrt x1 can be found at 2x1+1=0 (it > > doesn't matter what you guessed) > > > Double derivative shows -2, hence a maxima. > > x1=--1/2 > > > > > > Maxima of Z wrt x2 is similar: 2x2+1=0 gives > > x2=-1/2. > > > > > > Is this the right answer really? Start with > > arbitrary x1_guess and x2_guess. x1 and x2 =-1/2 > > should give you the maxima(they are quadratics > > anyway). > > > > > > Iterate again. The equilibirum (or fixed point > of > > this simple dynamical system) is -1/2 and -1/2. > > > > > > I am not sure what you were trying to achieve but > a > > closed form solution already exists from this > problem > > definition. > > > > > > An interesting problem for me would have been > this: > > > > > > Write an algorithm that computes the fixed > points > > of this system: > > > > > > y(n)=[(x1(n) +x2(n)] -x1(n+1)-[x1(n+1)]^2 > > > z(n)=[[(x1(n) +x2(n)]^2 -x2(n+1)-[x2(n+1)]^2 > > > > > > with x(n+1) so chosen such that y(n)>y(n-1) and > > z(n)>z(n-1) > > > > > > The choice of x(n+1) to get quick convergence > would > > be very interesting. > > > > > > Thanks, > > > > > > Saurabh > > > > > > > vector_guess = (x1_guess, x2_guess) > > > > y1 = x1_guess + x2_guess > > > > y2 = x2_guess^2 + x1_guess > > > > y = y1 - x1^2 - x1 > > > > z = y2^2 - x2 - x2^2 > > > > x1_feedback = the value of x1 which maximizes > y > > with respect to x1 > > > > x2_feedback = the value of x2 which maximizes > z > > with respect to x2 > > > > vector_feedback = (x1_feedback, x2_feedback). > > > > And then keep updating vector_guess until the > > “equilibrium” is reached. I have no clue > > how this can be programmed. Please help. > > Hi Saurabh: > > Thanks a lot for yur reply. The example I gave > below > > was a simple one because all I need to do is to > > program the above step. My real y1, y2 functions > are > > way too long and complex to post herer. Once I can > > program it in MATLAB (I suspect using "count" and > > "while" commands), then I will be able to use it > im > > my complex model which involves complex equations > in > > place of z, y, and many more steps in between. So, > I > > am not asking about the non-MATLAB solution to the > > above problem, I just wonder if somene can kindly > > give me code how to program the above in MATLAB. I > > suspect I will NOT get helped at all, but please > > kindly note that I am not a free-rider and this is > > not an assignment (it is my independent research > in > > Economics, which I can handle in Excel VB) but I > need > > MATLAB and I just honestly declared that I know > > nothing at all about programming in MATLAB. The > > reason I have posted it here b/c > > I was told it is so simple that someone who just > type > > in a few commans in no time. Thanks again for > anyone > > who can and will want to help. > > I'm not a specialist in MATLAB syntax ; so the > following code fragment may contain errors. > If you have problems, maybe other users of the forum > can help. > > x_old(1) = 5; > x_old(2) = 10; > error=1.0; > x_lower = -20.0; > x_upper = 20.0; > while error > 0.01 > y_array(1) = x_old(1)+x_old(2); > y_array(2) = x_old(2)^2+x_old(1); > y = @(x) y_array(1)-x^2-x; > z = @(x) y_array(2)^2-x-x^2; > x_new(1) = fminbnd(y,x_lower,x_upper); > x_new(2) = fminbnd(z,x_lower,x_upper); > error = norm(x_old-x_new); > x_old = x_new; > end > > But I doubt this can serve as a basis to > solve such difficult equilibrium problems you are > talking about. > I made the experience that it makes much more sense > to understand and use good existing code than to do > code development on one's own. > Make a google search to find MATLAB code > for solving dynamic equilibrium models in > macroeconomics - there are plenty of them. > > Good luck ! > > Best wishes > Torsten. The first error I found by myself -:) x_old(1) = 5; x_old(2) = 10; error=1.0; x_lower = -20.0; x_upper = 20.0; while error > 0.01 y_array(1) = x_old(1)+x_old(2); y_array(2) = x_old(2)^2+x_old(1); y = @(x) -(y_array(1)-x^2-x); z = @(x) -(y_array(2)^2-x-x^2); x_new(1) = fminbnd(y,x_lower,x_upper); x_new(2) = fminbnd(z,x_lower,x_upper); error = norm(x_old-x_new); x_old = x_new; end Best wishes Torsten.
From: Andy on 30 Jul 2010 09:15
The basic "count = 0 and while" structure should look something like this: count = 0; maxcount = 1000; while error > tol count = count+1; if count > maxcount disp('Reached maximum iterations before error within tolerance'); break end % prepare variables for next iteration end I don't have time to comment on the rest of the code provided above, but if you don't add the count to the while loop you run the risk of the while loop never ending. |