0

I have attempted to code fixed point iteration to find the solution to (x+1)^(1/3). I keep getting the following error: error: 'g' undefined near line 17 column 6 error: called from fixedpoint at line 17 column 4

    clear -all;
clc;

function f = f(x)

    f = (x+1)^(1/3)

    f = g(x)
end

# Start out iteration loop
x1 = 0;
x2 = g(x1);

iterations = 0; # iteration counter

while abs(x2-x1 > 1e-5)
    plot([x1 x1], [x1 g(x1)], 'k-')
    plot([x1 x1], [x1 g(x1)], 'k--')
    pause
    x1 = x2;
    x2 = g(x1);
    iterations = iterations + 1;
end

iterations
x1
x2

I have no idea what is wrong. My logic seems to be correct.

1 Answer 1

2

I modified your code a little, it could get the solution of f(x)=cos(x)-x, and you could change g(x) to whatever you want.

clear;
clc;

%# You function here
g=@(x) cos(x);

%# Start out iteration loop
x1 = 0;
x2 = g(x1);

iterations = 0;% # iteration counter

ezplot(g,[0,1]);
hold on 
ezplot('x',[[0,1]])

while (abs(x2-x1) > 1e-5 && iterations<100)
    plot([x1 x1], [x1 x2], 'k-')
    plot([x1 x2], [x2 x2], 'k--')
     %pause
    iterations = iterations + 1;
    x1 = x2;
    x2 = g(x1);
end
iterations 
[x1 x2]

And the solution will be:

iterations =

    29

ans =

    0.7391    0.7391

With the output image: enter image description here


However, are you certain that your function is right? It seems that this function could not use Fixed Point Iteration to solve, since f(x)=0 equals to g(x)=x and g(x)=(x+1)^(1/3)+x here. But if we plot g(x)(blue curve) with h(x)=x(red curve), we have:

enter image description here

So if we start at 0, the iteration can't convergence (x1 will increase dramatically but the root is -1).

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.