why my iteration prog doesn't work ?
1 次查看(过去 30 天)
显示 更早的评论
% Use NR method f(x)= x^3-5x^2+7x-3
clc
TV=3;
x=(4);
tol=0.0007;
format long
for i=1:5;
fx=(x(i))^3-5*(x(i))^2+7*(x(i))-3;
fxx=3*(x(i))^2-10*(x(i))+7;
x(i+1)=(x(i))-(fx/fxx);
E_T(i)=(abs((TV-x(i+1))/TV))*100;
end
for i=1:5;
e(i)=(x(6))-(x(i));
fx=(x(i))^3-5*(x(i))^2+7*(x(i))-3;
fxx=3*(x(i))^2-10*(x(i))+7;
fxxx=6*(x(i))-10;
e(i+1)=(-fxxx/2*fxx)*(e(i))^2;
end
if abs(TV-(x(i+1)))<tol
disp(' enough to here')
disp(' -----------')
disp(' x(i+1) ')
disp(' -----------')
x;
disp(' -----------')
disp(' T.V ')
disp(' -----------')
E_T;
disp(' -----------')
disp(' E(i+1) ')
disp(' -----------')
e;
disp(' ------------- ')
end
%-----------------------------------------------------------
7 个评论
Walter Roberson
2017-1-25
If you need 50 or 80 digits you will need to switch to the Symbolic Toolbox, and display using vpa()
NumDig = 50;
x = sym(-0.1);
y = sym(0);
fprintf(' i x \n')
fprintf(' --- ------------- \n')
for i=1:5;
fx=(x(i))^3-(x(i))^2+2;
fxx=3*(x(i))^2-2*(x(i));
y(i)=x(i)-(fx/fxx);
fy=(y(i))^3-(y(i))^2+2;
fyy=3*(y(i))^2-2*(y(i));
x(i+1)=x(i)+([fy-fx]/fyy);
end
for i=1:length(x)
fprintf('%2i %s\n', i, char(vpa(x(i),NumDig)))
end
采纳的回答
Walter Roberson
2017-1-23
In your section
if abs(TV-(x(i+1)))<tol
disp(' enough to here')
disp(' -----------')
disp(' x(i+1) ')
disp(' -----------')
x;
disp(' -----------')
disp(' T.V ')
disp(' -----------')
E_T;
disp(' -----------')
disp(' E(i+1) ')
disp(' -----------')
e;
disp(' ------------- ')
end
remove the ';' from the 'x;' and 'E_T;' and 'e;' -- so
if abs(TV-(x(i+1)))<tol
disp(' enough to here')
disp(' -----------')
disp(' x(i+1) ')
disp(' -----------')
x
disp(' -----------')
disp(' T.V ')
disp(' -----------')
E_T
disp(' -----------')
disp(' E(i+1) ')
disp(' -----------')
e
disp(' ------------- ')
end
更多回答(1 个)
Lateef Adewale Kareem
2017-1-23
increase your number of iteration, you will meet the tolerance target
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Special Values 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!