首页 > 解决方案 > 在 MatLAB 中绘制 Newton 和 Ostrowski 图的数据参数无效

问题描述

我想将 f 函数绘制为红色,并将 p 值限制为下面显示的蓝色结果,但我遇到了 INVALID DATA ARGUMENT 错误。

% tolerance (as relative error set to 1e-5)
% N0 (max. iterations) set to 100
% Output: the value p
% Example: visualiseConvergence1(@(x) x^3+4*x^2-10, @(x) 3*x^2+8*x, 1)
function p = visualiseConvergence1(f, df, p0)
fprintf('%3d:%16.9f\n', 0, p0); 
%Step 1:
ostp0=p0;
i = 1;
TOL=1e-5;
N0=100;
fullx=linspace(-10, 10, 1000);
%Step 2:
while i <= N0
   %Step 3 Newton:
   p = p0 - f(p0)/df(p0);
   fprintf('%3d:%16.9f\n', i, p); 
   %Step 4:
   if abs(p - p0) < TOL
       fprintf('Solution found p = %g\n', p);
       break
   end
   %Step 5:
   i = i + 1;
   %Step 6:
   p0 = p;
   convergeNewtonX=linspace(-10, p0, 1000);
end
%Step 2 Ostrowski:
i=1;
while i <= N0
   %Step 3:
   p = ostp0 - f(ostp0)/df(ostp0);
   q= ostp0 - (f(ostp0)/df(ostp0)) * ((f(ostp0)-f(p))/(f(ostp0)-(2*f(p))));
   fprintf('%3d:%16.9f\n', i, q); 
   %Step 4:
   if abs(q - ostp0) < TOL
       fprintf('Solution found p = %g\n', q);
       break
   end
   %Step 5:
   i = i + 1;
   %Step 6:
   ostp0 = q;
   convergeOstroX=linspace(-10, ostp0, 1000);
end
subplot(10,10,1);
originalfunction=f;
plot(fullx,originalfunction,'r',convergeNewtonX,originalfunction,'b');
title(Newton);

subplot(10,10,2);
originalfunction=f;
plot(fullx,originalfunction,'r',convergeOstroX,originalfunction,'b');
title(Ostrowski);
end

当我输入函数时:visualiseConvergence1(@(x) x^3+4 x^2-10, @(x) 3 x^2+8*x, 1),它找到了 p 个解决方案,但不是绘图,给出这个:

  1:     1.454545455
  2:     1.368900401
  3:     1.365236600
  4:     1.365230013
Solution found p = 1.36523
  1:     1.367904991
  2:     1.365230013
  3:     1.365230013
Solution found p = 1.36523
Error using plot
Invalid data argument.

Error in visualiseConvergence1 (line 50)
plot(fullx,originalfunction,'r',convergeNewtonX,originalfunction,'b');

你有什么建议修复它?

标签: matlabgraph

解决方案


推荐阅读