how to solve the error? Error in fixed_point_3 (line 10) while iter <= max_iter

1 次查看(过去 30 天)
function [x] = fixed_point_3(init,error,max_iter)
% 2x^3 + 4x^2 - 10 = 0 can be written as
% 4x^2 = 10 - 2x^3 --> x^2 = 1/4 *(10 - 2x^3)
% if only positive root is taken, then x = 1/4 ((10 - 2x^3)^(1/2)
%now we can use fixed point iteration
iter = 0;
now
%max_iter = maxium number of iterations
while iter <= max_iter
max_iter = 7.3814e+05;
x = (1/4)*((10- init^3)^(1/2));
iter = iter + 1;
if abs(x-init)<error
return
end
end
  2 个评论
Matt Gaidica
Matt Gaidica 2020-12-13
I don't get an error passing in (1,1,1). Note: it's bad practice to use init and error as variable names, those are reserved in MATLAB. Also, use a code block to share.
function [x] = fixed_point_3(init,error,max_iter)
% 2x^3 + 4x^2 - 10 = 0 can be written as
% 4x^2 = 10 - 2x^3 --> x^2 = 1/4 *(10 - 2x^3)
% if only positive root is taken, then x = 1/4 ((10 - 2x^3)^(1/2)
%now we can use fixed point iteration
iter = 0;
now
%max_iter = maxium number of iterations
while iter <= max_iter
max_iter = 7.3814e+05;
x = (1/4)*((10- init^3)^(1/2));
iter = iter + 1;
if abs(x-init)<error
return
end
end
Walter Roberson
Walter Roberson 2020-12-13
??
init is used in some obscure methods such as idnlarx but it is by no means reserved.
error() is not actually reserved, but it is not a great idea to use it as a variable name.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by