Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
program a simple algorithm

Subject: program a simple algorithm

From: Anna Kaladze

Date: 29 Jul, 2010 04:26:03

Message: 1 of 16

Dear all,
I am new to MATLAB and to programming, and I need to solve quite a complex model, but it is mainly based on an algorithm which I have little clue how to program and which I will describe below via a simple example. If someone can help me with the code how to program it in MATLAB, that will be so great. Thanks a lot in advance.
So,
Step1: Define a guess vector, vector_guess = (x1_guess, x2_guess). Say, x1_guess = 10, x2_guess = 20.
Step 2. Based on the vector_guess, calculate a new feedback vector via a series of equations and operations. Say, vector_feedback = (x1_feedback, x2_feedback) = (12, 25).
Define a vector diff as a percentage difference between the guess vector and the feedback vector for the current iteration. So, diff = (0.2, 0.25).
Then update vector_guess for the next iteration according to the rule:
 vector_guess*(1 + diff/q), where q is a positive integer (say q =9) and repeat the process until the diff vector <= 0.01. That is, I need to update the guess vector by a factor that depends on the discrepancy between the guess and feedback of the previous iteration. Once the discrepancy is within some tolerance, I need to exit the iterations.
So, in my dummy “notations” it goes something like this (this example might not work as it is just an example, but I need to learn how to properly program it):
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.
Thanks a lot!
Anna.

Subject: program a simple algorithm

From: Torsten Hennig

Date: 29 Jul, 2010 06:53:57

Message: 2 of 16

> Dear all,
> I am new to MATLAB and to programming, and I need to
> solve quite a complex model, but it is mainly based
> on an algorithm which I have little clue how to
> program and which I will describe below via a simple
> example. If someone can help me with the code how to
> program it in MATLAB, that will be so great. Thanks a
> lot in advance.
> So,
> Step1: Define a guess vector, vector_guess =
> (x1_guess, x2_guess). Say, x1_guess = 10, x2_guess =
> 20.
> Step 2. Based on the vector_guess, calculate a new
> feedback vector via a series of equations and
> operations. Say, vector_feedback = (x1_feedback,
> x2_feedback) = (12, 25).
> Define a vector diff as a percentage difference
> between the guess vector and the feedback vector for
> the current iteration. So, diff = (0.2, 0.25).
> Then update vector_guess for the next iteration
> according to the rule:
> vector_guess*(1 + diff/q), where q is a positive
> e integer (say q =9) and repeat the process until the
> diff vector <= 0.01. That is, I need to update the
> guess vector by a factor that depends on the
> discrepancy between the guess and feedback of the
> previous iteration. Once the discrepancy is within
> some tolerance, I need to exit the iterations.
> So, in my dummy “notations” it goes
> something like this (this example might not work as
> it is just an example, but I need to learn how to
> properly program it):
> 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.
> Thanks a lot!
> Anna.

What is the final aim of your algorithm ?
Maybe you can find specialized MATLAB routines
that do the job for you.
 
Best wishes
Torsten.

Subject: program a simple algorithm

From: Anna Kaladze

Date: 29 Jul, 2010 07:18:22

Message: 3 of 16

Torsten Hennig <Torsten.Hennig@umsicht.fhg.de> wrote in message <1672989918.21168.1280386468043.JavaMail.root@gallium.mathforum.org>...
> What is the final aim of your algorithm ?
> Maybe you can find specialized MATLAB routines
> that do the job for you.
>
> Best wishes
> Torsten.
--------------------------------------------------
Hi Thorsten,
Thanks a lot for your reply. This is to solve a complex macroeconomic problem which consists of a lot of time-dependent non-integral equations. I used to write macro’s in Excel and use its Solver to crack problems like this (Gauss-Seidel e.g.,), but as you know it often is more efficient to do programming of these sort of things in MATLAB. Looping takes forever, but I have heard that a commands like “count=0 and while” help to crack the problem really fast. I tried a bunch of simultaneous equations solving techniques in Mathematica but it takes a lot of time and thus not efficient. If I only could know how to program my example algorithm, that would do a trick fine. Essentially, in equllbrium, x1_guess and x2_guess should be such that all equations "clear" (otherwise it is not equllbrium). Thanks again.

Subject: program a simple algorithm

From: Torsten Hennig

Date: 29 Jul, 2010 07:39:38

Message: 4 of 16

> Torsten Hennig <Torsten.Hennig@umsicht.fhg.de> wrote
> in message
> <1672989918.21168.1280386468043.JavaMail.root@gallium.
> mathforum.org>...
> > What is the final aim of your algorithm ?
> > Maybe you can find specialized MATLAB routines
> > that do the job for you.
> >
> > Best wishes
> > Torsten.
> --------------------------------------------------
> Hi Thorsten,
> Thanks a lot for your reply. This is to solve a
> complex macroeconomic problem which consists of a lot
> of time-dependent non-integral equations. I used to
> write macro’s in Excel and use its Solver to
> crack problems like this (Gauss-Seidel e.g.,), but as
> you know it often is more efficient to do programming
> of these sort of things in MATLAB. Looping takes
> forever, but I have heard that a commands like
> “count=0 and while” help to crack the
> problem really fast. I tried a bunch of simultaneous
> equations solving techniques in Mathematica but it
> takes a lot of time and thus not efficient. If I only
> could know how to program my example algorithm, that
> would do a trick fine. Essentially, in equllbrium,
> x1_guess and x2_guess should be such that all
> equations "clear" (otherwise it is not equllbrium).
> Thanks again.

So you have a system of algebraic equations to solve ?
Or a system of ordinary differential equations ?
Or do you have to optimize an objective function ?

Best wishes
Torsten.

Subject: program a simple algorithm

From: Anna Kaladze

Date: 29 Jul, 2010 08:58:07

Message: 5 of 16

>
> So you have a system of algebraic equations to solve ?
> Or a system of ordinary differential equations ?
> Or do you have to optimize an objective function ?
>
> Best wishes
> Torsten.
--------------
Hi Torsten,
The model has a mix of equations, which give rise to a number of functions which needed to be maximized. Briefly, by assumption, an “agent” takes certain values as given and does his maximization exercises (Much like you and I go to the supermarket and take all the prices there as given and decide "optimally" what to buy). But aggregated across many agents, those exercises affect the state of the economy, which require the agents to re-optimize ande the process continues until the equillbrium is reached (all equations clear and agents have no further incentives to reoptimize (or even if they, the arrive at the same vakues as before, so the aggregate economy’s state is not affected). That is, what everyone buys at the end, determines the new set of prices in the supermarkets, which you and I again take as given in our optimization execrise.
In my model, many integrals are mathematically simply not integrable. If I assume very simple functions, they are, but the analytical solutions take several pages in length still. As for the maximization, I can of couse find the first/derivatives and sent them equal to zero and combine those with other equations and solve everything simultaneously), but the functions are so complex and they are “nearly” flat along large intervals. The shapes of the functions are extremely sensitive to the parameters of the model. Importantly, there are plenty of local maxima. All I need is to program the above example. The rest I hope to figure out myself (and I need global maximum). Of course, one wayb to ensure the global maxima is to predefine a range x1 and make sure y is evaluated at all those x1 points, and then tha best is chosen. But I am not worried about it at the moment. All I need
is when I define a guess vector, the program to iterate until the feedback and the guess vectors are "equal". Preferably using “count” and “while” functions please. The example is again:
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).
The above procedure needs to continue until the percentage difference between x1_feeback and x1_ guess (and also x2_feedback and x2_guess) <=0.01.
That is all I need. Thanks again.

Subject: program a simple algorithm

From: Saurabh Mahapatra

Date: 30 Jul, 2010 02:09:04

Message: 6 of 16

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.

Subject: program a simple algorithm

From: Anna Kaladze

Date: 30 Jul, 2010 09:11:08

Message: 7 of 16

"Saurabh Mahapatra" <saurabh.mahapatra@mathworks.com> wrote in message <i2tc80$pkv$1@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.

Subject: program a simple algorithm

From: Torsten Hennig

Date: 30 Jul, 2010 10:29:19

Message: 8 of 16

> "Saurabh Mahapatra" <saurabh.mahapatra@mathworks.com>
> wrote in message <i2tc80$pkv$1@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.

Subject: program a simple algorithm

From: Torsten Hennig

Date: 30 Jul, 2010 10:57:36

Message: 9 of 16

> > "Saurabh Mahapatra"
> <saurabh.mahapatra@mathworks.com>
> > wrote in message
> <i2tc80$pkv$1@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.

Subject: program a simple algorithm

From: Andy

Date: 30 Jul, 2010 13:15:08

Message: 10 of 16

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.

Subject: program a simple algorithm

From: Torsten Hennig

Date: 30 Jul, 2010 13:45:01

Message: 11 of 16

> 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
> re 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.

Good point.
So the actual status of the program looks something like

count = 0;
maxcount = 1000;
tol = 0.01;
error = 1.0;
x_lower = -20.0;
x_upper = 20.0;
x_old(1) = 5;
x_old(2) = 10;
while error > tol
count = count+1;
if count > maxcount
disp('Reached maximum iterations before error within tolerance');
break
end
% prepare variables for next iteration
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.

Subject: program a simple algorithm

From: Anna Kaladze

Date: 30 Jul, 2010 15:43:04

Message: 12 of 16

Torsten hi,
Thanks a lot for your code! I have slightly adjusted your code, but looks like it does not work for a slightly different problem. It takes too long (or maybe I did smth. wrong). However, looks like if one sets x_old to [0.124993625436672, -0.624980876303235] it will equal to the feedback vector. But I can’t see why the code does not find that. Can you please have a look? Thanks.

tol=0.01;
q=9;
x_old(1) = 0.1;
x_old(2) = 0.1;
error=1.0;
x_lower = -20.0;
x_upper = 20.0;
while error > tol
    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);
    x_feedback(1) = x_new(1)-x_old(1);
    x_feedback(2) = x_new(2)-x_old(2);
        error = norm(x_old-x_new);
    x_old = x_old.*(1+error/q);
 end

Subject: program a simple algorithm

From: Anna Kaladze

Date: 30 Jul, 2010 16:15:07

Message: 13 of 16

Hi Andy and Torsten,
Thanks a lot for your replies. I didn't realize that you posted those last messages, so please ignore my last post. I will try to correct the code based on the "count" reccomendations to see if it works.

Subject: program a simple algorithm

From: Anna Kaladze

Date: 31 Jul, 2010 06:28:24

Message: 14 of 16

Hi. The method does not work unfortunately.
The code is:

count = 0;
maxcount = 1000;
tol = 0.01;
error = 1.0;
x_lower = -20.0;
x_upper = 20.0;
x_old(1) = 1;
x_old(2) = 1;
while error > tol
count = count+1;
if count > maxcount
disp('Reached maximum iterations before error within tolerance');
break
end
% prepare variables for next iteration
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^2-x);
x_new(1) = fminbnd(y,x_lower,x_upper);
x_new(2) = fminbnd(z,x_lower,x_upper);
x_feedback(1)=x_old(1)-x_new(1);
x_feedback(2)=x_old(2)-x_new(2);
error = norm(x_old-x_feedback);
x_old = x_feedback;
end
The answer is x_old=[-0.0411221739472606,-0.126633531641413]. Can you please see what might be wrong? Thanks a lot.

Subject: program a simple algorithm

From: Torsten Hennig

Date: 31 Jul, 2010 10:29:42

Message: 15 of 16

> Hi. The method does not work unfortunately.
> The code is:
>
> count = 0;
> maxcount = 1000;
> tol = 0.01;
> error = 1.0;
> x_lower = -20.0;
> x_upper = 20.0;
> x_old(1) = 1;
> x_old(2) = 1;
> while error > tol
> count = count+1;
> if count > maxcount
> disp('Reached maximum iterations before error within
> tolerance');
> break
> end
> % prepare variables for next iteration
> 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^2-x);
> x_new(1) = fminbnd(y,x_lower,x_upper);
> x_new(2) = fminbnd(z,x_lower,x_upper);
> x_feedback(1)=x_old(1)-x_new(1);
> x_feedback(2)=x_old(2)-x_new(2);
> error = norm(x_old-x_feedback);
> x_old = x_feedback;

I don't understand why you set x_old = x_feedback here
instead of x_old = x_new.
If the iteration converges, you will have
x_feedback = 0 in the end and thus x_old=0 in the end.
I don't think that this is what you want.

From your first mail I assumed that you wanted
to update x_guess by x_new - but this does not seem
to be the case - does it ?

> end
> The answer is
> x_old=[-0.0411221739472606,-0.126633531641413]. Can
> you please see what might be wrong? Thanks a lot.

Best wishes
Torsten.

Subject: program a simple algorithm

From: Anna Kaladze

Date: 31 Jul, 2010 14:06:05

Message: 16 of 16

Yes, Torsten, you are right. I need to add one more "step" involving another vector x_feedback. That is, given x_new, x_feedback will take specific values and I need now the program update x_old in such a way to make x_feeback eventually equal x_old. Thanks a lot.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us