How to get a solution of f(x)=x^3 +x-1 by iteration on Matlab? And x=g(x) ......x(n+1)=g(x) when n>=0. Give that g(x)=1/(1+x^2)
Show older comments
How to get a solution of f(x)=x^3 +x-1 by iteration on Matlab? And x=g(x) ......x(n+1)=g(x) when n>=0. Give that g(x)=1/(1+x^2)
clear
% Value of f(x)
f(x)=x.^3 +x-1;
% First guess for the square root of x
x(1) = 1;
i = 1;
% If the error is greater than 10^-4, continue iterating
while abs (x (i) - f(x) ) >= 10^-4
i = i + 1;
x ( i ) = 1./(1+(x.^2));
end
% Show the result
x ( i )
plot ( abs ( x - f(x) ) )
grid on
xlabel('Number of iterations')
ylabel('error')
Answers (2)
Walter Roberson
on 12 Nov 2012
Change
x ( i ) = 1./(1+(x.^2));
to
x ( i ) = 1./(1+(x(i-1).^2));
Andrei Bobrov
on 12 Nov 2012
f=@(x)x.^3 +x-1;
x(1) = 1;
i1 = 1;
while abs(f(x)) >= 10^-4
i1 = i1 + 1;
x(i1) = 1./(1+x(i1 - 1).^2);
end
Categories
Find more on External Language Interfaces in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!