matlab - 在 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');
你有什么建议修复它?
解决方案
推荐阅读
- python - 使用pyqt5中的另一个按钮激活按钮,我应该使用if语句吗?
- javascript - 在异步操作的新窗口中打开签名的 S3 URL 时出错
- python - 使用 python 排序函数对 Pandas DataFrame 中的值进行排序
- batch-file - 我需要脚本来处理所有 dav 文件
- javascript - 仅当文本框具有使用 JavaScript 从数据库中显示的某些值时,它才会可见
- html - 模板驱动形式将所有内容标记为已触摸 - Angular 7
- mysql - SQL - 过程函数
- c - 如何制作在 C 中重复的菜单
- javascript - 传单设置指定 geoJSON 图层属性的缩放级别
- reactjs - 使用 formik 在表单中进行 onchange 字段的值不会更新