Wrong output while finding the root by Fixed Point Iteration
显示 更早的评论
I have an equation g = @(x)(5-((5/2)*exp(x/2))-((7/2).x^2)-3*x).^1/3 and, according to specs, the equation has 3 roots. I'd like my code produce me both both x and g(x) value in 2 columns because it'd be easier to get those roots. But my code behaves wrong, it gives me the following when I try to run it:
xc = fpi(g, 0.5, 20)
x0 g(x0)
1.0e+188 *
0.0000 -0.0000
-0.0000 0.0000
0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -0.0000
-0.0000 0.0000
0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -0.0000
-0.0000 -1.4749
-1.4749 -Inf
-Inf NaN
NaN NaN
NaN NaN
NaN NaN
xc =
NaN
What did I do wrong? How could it be fixed?
%Program 1.2 Fixed-Point Iteration
%Computes approximate solution of g(x)=x
%Input: inline function g, starting guess x0,
% number of steps k
%Output: Approximate solution xc
function xc=fpi(g,x0,k)
x(1)=x0;
for i=1:k
x(i+1)=g(x(i));
end
xc=x(k+1);
disp('x0 g(x0)');
disp([x', g(x)']);
回答(1 个)
Walter Roberson
2015-9-16
g = @(x)(5-((5/2)*exp(x/2))-((7/2).x^2)-3*x).^1/3
is invalid syntax. You need * between the (7/2) and the x^2
With that correction, the equation does not have 3 roots, it has 5 roots, 2 of which are real. They are approximately
-1.5622276995472072141
.42511928810478633643
13.857652165630850665+22.906093584614758165*i
11.453142346661211379+8.8602737198488354732*i
11.453142346661211379-8.8602737198488354732*i
类别
在 帮助中心 和 File Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!