数值分析上机题Matlab(东南大学)2

  • 格式:pdf
  • 大小:353.77 KB
  • 文档页数:6

else break; end end end
(3) 完整程序见附录。
三、结果
>> exp2(); Test ================================================== k 0 1 2 3 End test Find max delta ================================================== Max delta is between 0.700000 and 0.800000 Max delta is between 0.770000 and 0.780000 Max delta is between 0.774000 and 0.775000 Max delta is between 0.774500 and 0.774600 Max delta is between 0.774590 and 0.774600 Max delta is between 0.774596 and 0.774597 -------------------------------------------------Max delta is 0.774596 ================================================== End find max delta Test Result ================================================== x0 -1000000.0 -10000.0 -100.0 -1.1 -0.9 -0.5 result -1.732051 -1.732051 -1.732051 -1.732051 1.732051 0.000000 -------------------------------------------------xk 0.100000 -0.000673 0.000000 0.000000 --------------------------------------------------
数值分析上机题 2
2010/9/29
一、题目
P.19, 20.(上机题)Newton 迭代法
二、程序
: (1) 解方程根的通用程序如下(Matlab)
function res=getNewtonResultMsgOff(f,x0,e) f = sym(f); x = x0; xk = x - subs(f) / subs(diff(f)); while abs(xk-x) > e x = xk; xk = x - subs(f) / subs(diff(f)); end res = xk; end
f = sym(f); x = x0; xk = x - subs(f) / subs(diff(f)); while abs(xk-x) > e x = xk; xk = x - subs(f) / subs(diff(f)); end res = xk; end function res=getNewtonResultFigOn(f,x0,e,x1,x2) f = sym(f); x = x1:1e-3:x2; figure('Name',sprintf('x0 = %0.1f',x0)); plot(x,subs(f),'r'); title(sprintf('x0 = %0.1f',x0)); h = line([x1,x2],[0,0]); set(h,'Color','k'); fprintf('==================================================\n'); fprintf('\tk\t\t\txk\n'); fprintf('--------------------------------------------------\n'); x = x0; k = 0; fprintf('\t%d\t\t%f\n',k,x0); k = k + 1; xk = x - subs(f) / subs(diff(f)); h = line([x,x],[0,subs(f)]); set(h,'Color','c'); line([x,xk],[subs(f),0]); text(x,0,'x0') fprintf('\t%d\t\t%f\n',k,xk); while abs(xk-x) > e x = xk; k = k + 1; xk = x - subs(f) / subs(diff(f)); h = line([x,x],[0,subs(f)]); set(h,'Color','c'); line([x,xk],[subs(f),0]); fprintf('\t%d\t\t%f\n',k,xk); end res = xk; fprintf('==================================================\n'); end
% ② fprintf('\nTest Result\n'); fprintf('==================================================\n'); f = 'x^3/3-x'; e = 1e-4; fprintf('\t\tx0\t\t\t\t\t\tresult\n'); fprintf('--------------------------------------------------\n'); for x0 = [-1e6,-1e4,-1e2,-1.1,-0.9,-0.5,-0.1,0.1,0.5,0.9,1.1,1e2,1e4,1e6] fprintf('\t%12.1f\t\t\t\t%f\n',x0,getNewtonResultMsgOff(f,x0,e)); end fprintf('==================================================\n'); fprintf('End test Result\n'); fprintf('\nTest x0 = 0.9\n') f = 'x^3/3-x'; x0 = 0.9; e = 1e-4; getNewtonResultFigOn(f,x0,e,-3,2); fprintf('End test x0 = 0.9\n'); end function res=getNewtonResult(f,x0,e) f = sym(f); fprintf('==================================================\n'); fprintf('\tk\t\t\txk\n'); fprintf('--------------------------------------------------\n'); x = x0; k = 0; fprintf('\t%d\t\t%f\n',k,x0); k = k + 1; xk = x - subs(f) / subs(diff(f)); fprintf('\t%d\t\t%f\n',k,xk); while abs(xk-x) > e x = xk; k = k + 1; xk = x - subs(f) / subs(diff(f)); fprintf('\t%d\t\t%f\n',k,xk); end res = xk; fprintf('==================================================\n'); end function res=getNewtonResultMsgOff(f,x0,e)
(2) 求最大������:
f = 'x^3/3-x'; e = 1e-4; maxdelta = 0; for steplength = [1e-1,1e-2,1e-3,1e-4,1e-5,1e-6] for delta=maxdelta:steplength:1 x0 = delta; res = getNewtonResultMsgOff(f,x0,e); if abs(res)<e maxdelta = delta;
==================================================
-0.1 0.1 0.5 0.9 1.1 100.0 10000.0 1000000.0 End test Result Test x0 = 0.9
0.000000 0.000000 0.000000 -1.732051 1.732051 1.732051 1.732051 1.732051
================================================== End test x0 = 0.9
四、分析
Newton 迭代法能在给定的初值附近较快地找到方程的根,但有时会发生找到的根不在 给定初值附近(例如给定初值为 0.9 时) 。
五、附录
完整程序:
function exp2() % 编程语言 Matlab % @姓名 % @学号 % @日期 2010/9/29 % (1) % Test fprintf('Test\n') f = 'x^3/3-x'; x0 = 0.1; e = 1e-4; getNewtonResult(f,x0,e); fprintf('End test\n'); % End test % (2) % ① fprintf('\nFind max delta\n'); fprintf('==================================================\n'); f = 'x^3/3-x'; e = 1e-4; maxdelta = 0; for steplength = [1e-1,1e-2,1e-3,1e-4,1e-5,1e-6] for delta=maxdelta:steplength:1 x0 = delta; res = getNewtonResultMsgOff(f,x0,e); if abs(res)<e maxdelta = delta; else break; end end fprintf('Max delta is between %f and %f\n',maxdelta,maxdelta+steplength); end fprintf('--------------------------------------------------\n'); fprintf('Max delta is %f\n',maxdelta); fprintf('==================================================\n'); fprintf('End find max delta\n');