函数文件1:

 function b=F(f,x0,h,N)
% b(1,1)=x0(1)-h*x0(2)-u(1);
% b(2,1)=x0(2)+h*x0(1)^2-u(2)-h*f;
b=zeros(N,1);
b(1,1)=4*x0(1)-x0(2);
b(2,1)=h^2*x0(1)^2-2*x0(1)+x0(2)-h^2*f(1)
for i=3:N
b(i,1)=x0(i-2)+h^2*x0(i-1)^2-2*x0(i-1)+x0(i)-h^2*f(i-1);
end

函数文件2:

 function g=Jacobian(x0,h,N)
% g(1,1)=1;
% g(1,2)=-h;
% g(2,1)=2*h*x0(1);
% g(2,2)=1;
g=zeros(N);
g(1,1)=4;
g(1,2)=-1;
g(2,1)=2*h^2*x0(1)-2;
g(2,2)=1;
for i=3:N
g(i,i-2)=1;
g(i,i-1)=2*h^2*x0(i-1)-2;
g(i,i)=1;
end

函数文件3:

 function x=newton_Iterative_method(f,u,h,N)
% u:上一节点的数值解或者初值
% x0 每次迭代的上一节点的数值
% x1 每次的迭代数值
% tol 允许误差
% f 右端函数
x0=u;
tol=1e-11;
x1=x0-Jacobian(x0,h,N)\F(f,x0,h,N);
while (norm(x1-x0,inf)>tol)
%数值解的2范数是否在误差范围内
x0=x1;
x1=x0-Jacobian(x0,h,N)\F(f,x0,h,N);
end
x=x1;%不动点

脚本文件:

 tic;
clear
clc
N=100;
h=1/N;
x=0:h:1;
y=inline('x.^2.*sin(x).^2+2*cos(x)-x.*sin(x)');
fun1=inline('x.*sin(x)');
Accurate=fun1(x);
f=y(x(2:N));
Numerical=zeros(N+1,1);
Numerical(2:end,1)=newton_Iterative_method(f,Numerical(2:end,1),h,N);
error=Numerical-Accurate';
subplot(1,3,1)
plot(x,Accurate);
xlabel('x');
ylabel('Accurate');
grid on
subplot(1,3,2)
plot(x,Numerical);
xlabel('x');
ylabel('Numerical');
grid on
subplot(1,3,3)
plot(x,error);
xlabel('x');
ylabel('error');
grid on
toc;

效果图:

最新文章

  1. QSort函数对不同类型数据快速排序浅谈
  2. ecshop常用二次开发修改
  3. TestNG官方文档中文版(2)-annotation
  4. Rsession让Java调用R更简单
  5. SharpZipLib 文件/文件夹压缩
  6. android 开发 - 使用okhttp框架封装的开发框架
  7. Static Const
  8. 如何破解UltraEdit
  9. iOS内存管理retain,assign,copy,strong,weak
  10. java枚举类型enum的使用
  11. JNI/NDK开发指南(开山篇)
  12. (转)PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
  13. N个元素的集合划分成互斥的两个子集的数目
  14. Servlet的学习之Response响应对象(2)
  15. CCNA毕业测试
  16. Django 类方式view进行进行用户验证
  17. 微信小程序学习笔记(阶段二)
  18. Python 管道
  19. jsp的自定义标签
  20. Python基础-使用paramiko

热门文章

  1. Centos下搭建邮件服务器
  2. MATLAB变量
  3. android studio相关配置
  4. 从Win32程序中的主函数中获取命令行参数
  5. 《linux就该这么学》第三节课 第二节命令笔记
  6. 构建高性能的MYSQL数据库系统-主从复制
  7. CSRF(Cross Site Request Forgery, 跨站域请求伪造)
  8. WebForm内置对象:Application和ViewState、Repeater的Command用法
  9. springmvc学习路线1-基本配置
  10. 剑指offer(36)两个链表中的第一个公共节点