Fixed-point Iteration in MATLAB

  • Thread starter Fatima Hasan
  • Start date
  • Tags Tags
    Matlab
9 replies · 4K views · last reply
Fatima Hasan
Messages
315
Reaction score
14
Homework Statement
The question is attached below.
Relevant Equations
-
Printed homework problem asking for a MATLAB fixed-point iteration program, with four functions: a) g(x) = x^5 - 3x^3 - 2x^2 + 2, b) g(x) = cos(sin(x)), c) g(x) = x^2 - sin(x + 0.15), d) g(x) = x^(x - cos(x)) for x > 0.


I used the same code , but changing the equation ( line 4). In part (D) , I got a complex number , does it mean that the function is divergence ?
Here's my code :
Part c :
MATLAB editor and figure window: code defines g(x) = x.^2 - sin(x+0.15) and runs a fixed-point loop, with a plot of g(x) (blue curve) and the line y = x (orange) over x from 1 to 2.2.

Part d :
MATLAB screenshot: script code beside a figure window plotting g(x) = x.^(x-cos(x)) and the line y = x over 1 to 2.2, with the curves diverging after about x = 1.3; command window shows a complex fixed point result.


Could someone confirm my answer please ? Any help would be greatly appreciated !
 
Matlab:
x0=g(x0)
How can your program work at all ? Or don't I understand Matlab ?
C)
Did you check with the plot ? where is g(x) = x ? Not at 1.75 !

D)
1^anything = 1 so how come that solution is not found ?
 
BvU said:
Matlab:
x0=g(x0)
How can your program work at all ? Or don't I understand Matlab ?
C)
Did you check with the plot ? where is g(x) = x ? Not at 1.75 !

D)
1^anything = 1 so how come that solution is not found ?
I set the initial conditions ( Lines : 4 - 8 ). The fixed point iteration in part C is x0=g(x0) and to compute the error ,we have to find the absolute value of the difference between x and xold. Once we have computed the error, the current value of x is stored in xold. x(1) is g(x0) when i =1 , x2 is g(x1) when i =2 , and so on ...
 
Does x0 = g(x0) change the value of x ?
 
BvU said:
Does x0 = g(x0) change the value of x ?
No, I think I have to write x instead of x0 in line 10 ,so x is varying in each iteration. Therefore , the error will be changed as well. Am I right now ?
 
Last edited:
Doesn't part d say "## \text{for} ~~ x > 0 ##"? I don't completely understand this, but is that right to start with ##-1##? Can you start with a different arbitrarily small number like ##1e-15##? I'm curious to see what happens and might even give it a go myself.

##x0 = g(x0)## looks right to me.
 
Last edited:
Joshy said:
Doesn't part d say "## \text{for} ~~ x > 0 ##"? I don't completely understand this, but is that right to start with ##-1##? Can you start with a different arbitrarily small number like ##1e-15##? I'm curious to see what happens and might even give it a go myself.

##x0 = g(x0)## looks right to me.
I wrote the same code but changing the initial value of x0 , from ##-1## to ##1e-15## . Here's what I got :

Partc
MATLAB screenshot with code beside a plot window of y = x^2 - sin(x + 0.15) curve in blue and y = x in orange over x from 1 to 2.2, crossing near x = 1.6.

Part d
MATLAB editor, Figure 1 plot, and Command Window: plot of g(x) = x^(x-cos(x)) (blue, rising steeply) and y = x (orange line) for x from 1 to 2.2; Command Window shows 'Fixed point is' with value 999999999999965.
 
Okay I see another thing (what others clearly noticed before me)... I was so concentrated on the function itself, but you do have another ##x## you're comparing ##x0## to when you check for your error; you're also plotting with respect to ##x##, which your code does not update. Make sure the ##x## and ##x0## is consistent.

A recommendation... I think a more elegant approach requires a modest change in your code. Change your ##x## into an array just like in line 19, but do this in the beginning at line 7 instead. Make the array something like:

Matlab:
x = x0.*ones(1, 20); % can be arbitrary numbers you're going to overwrite it anyways make sure your first one x(1) is x0

In the for loop you can refer to xold as ##x(i-1)## and the current iteration of x as ##x(i)## .

Matlab:
for i = 2:20 % start with 2 instead since your initialized version is x(1)
   
    x(i) = g( x(i-1) ); % x(i) is your current x, and x(i-1) is xold
    err = abs ( x(i) - x(i-1) );
   
    if(tol > err)
        break;
    end
end

When you get to plotting... no need to overwrite x or to create a new array.
 
Joshy said:
Okay I see another thing (what others clearly noticed before me)... I was so concentrated on the function itself, but you do have another ##x## you're comparing ##x0## to when you check for your error; you're also plotting with respect to ##x##, which your code does not update. Make sure the ##x## and ##x0## is consistent.

A recommendation... I think a more elegant approach requires a modest change in your code. Change your ##x## into an array just like in line 19, but do this in the beginning at line 7 instead. Make the array something like:

Matlab:
x = x0.*ones(1, 20); % can be arbitrary numbers you're going to overwrite it anyways make sure your first one x(1) is x0

In the for loop you can refer to xold as ##x(i-1)## and the current iteration of x as ##x(i)## .

Matlab:
for i = 2:20 % start with 2 instead since your initialized version is x(1)
  
    x(i) = g( x(i-1) ); % x(i) is your current x, and x(i-1) is xold
    err = abs ( x(i) - x(i-1) );
  
    if(tol > err)
        break;
    end
end

When you get to plotting... no need to overwrite x or to create a new array.
Thanks for your reply. Could you please tell me how to write a MATLAB code in PF ?

I changed the code and that's what I got:
MATLAB editor showing a fixed-point iteration script with g=@(x) x.^(x-cos(x)), x0=1e-15, tol=1e-4, a for loop over 2:20, and plot(x,g(x),x,x); beside a plot of a line from 0 to 10x10^14 on both axes, and a Command Window reading Fixed point is 1e-15.
 
I made a mistake changing ##x## into an array- my apologies (you can revert to your older version I think it worked better). I think your purpose for plotting both were to see where they intersect... shows that it should have converge.

There's an icon just above the text box.
Forum editor toolbar with formatting buttons, with a tooltip reading "Code" over the code icon.


I have no further feedback I think I'm going to have to fidget with this if I want to give you a better response. I might, but won't promise anything.

edit:

Did a little more digging. I think those last two are suppose to be difficult on purpose :) (difficult as in it won't converge easily) but it was fun to explore. That starting value really matters a lot for those two.

If I were to look at (roughly) ##x^2 - sin(x)## and I knew that ##x## was a really tiny magnitude ie. ##|x| << 1##... breaking it up into two parts I would know that ##x^2## is super tiny might be possible to neglect it and ##sin(x)## would be almost itself. If ##x## is large, then the sinusoidal is negligible and somehow ##x = x^2##. Good luck.

Plot of g(x) against idx from 0 to 50: a blue oscillating sequence that grows in amplitude then settles between about -0.35 and 0.33, with a red error magnitude curve on a log right axis falling from about 0.5 to 0.01.


Part d was really interesting. I should have looked at the formula... if you start with a tiny number like I suggested then it makes sense... ##x## is some small number to the power ##x-cos(x)## (probably a negative number), which makes it something like ##1 \over x^\text{small number}##. Of course it'll become ginormous just as we saw. Our trick for part c doesn't work here.

MATLAB plot of y and g(x) against x from 0 to 10; the g(x) curve stays near zero until about x = 8.5, then rises steeply to about 7 x 10^10 at x = 10, while y lies flat along zero.


(note the ##10^{10}## scale)

Your best bet is starting off with a bigger number, but how big? Funny enough... as you start close to 1 (+ or - minus a small fraction) then it's close to convergence. ##1^x##... it's going to be ##1##. You stray further away from that then ie ##0.5^{0.5-cos(0.5)}## and iterating it over and over again it's going to become some large number again.

Somewhere starting between 0.5 and 0.7 interesting things happen. I would recommend playing around there. I recommend plotting the error to see what's going on too ;) (this plot is when my starting value was 0.7).

MATLAB plot of fixed-point iteration: blue curve g(x) on left axis (0.7 to 1.05) jumps to about 1.02 then settles at 1; red error magnitude on log right axis (10^0 to 10^-20) falls roughly linearly, flattening near 10^-16 at about iteration 43.
 
Last edited: