Matlab:五点差分方法求解椭圆方程非导数边值问题






差分格式脚本文件:
tic;
clear
clc
M=32;%x的步数
N=16;%y的步数
h1=1/M;%x的步长
h2=1/N;%y的步长
x=0:h1:1;
y=0:h2:1;
u=zeros(M+1,N+1);%给数值解分配内存单元
U=u;%给精确解分配内存单元
u(1,:)=y.^3;%y边值
u(M+1,:)=1+y.^3;%y边值
u(:,1)=x.^3;%x边值。uo
u(:,N+1)=1+x.^3;%x边值。un
for i=1:M+1
for j=1:N+1
Accurate(i,j)=x(i)^3+y(j)^3;%精确解
end
end
fun=inline('-6*(x+y)','x','y');
for i=1:M-1
for j=1:N-1
f(i,j)=fun(x(i+1),y(j+1));
end
end
Numerical=u;
error=eye(M+1,N+1);
while norm(error,inf) >= 1e-5
for i=2:M
for j=2:N
Numerical(i,j)=(f(i-1,j-1)+N^2*u(i,j-1)+M^2*u(i-1,j)+M^2*u(i+1,j)+N^2*u(i,j+1))/(2*M^2+2*N^2);
end
end
error=Numerical-u;
u=Numerical;
end
[X,Y]=meshgrid(x,y);
subplot(1,3,1)
mesh(X,Y,Numerical');
title('the image of Numerical solution')
xlabel('x');ylabel('y');zlabel('u');
subplot(1,3,2)
mesh(X,Y,Accurate');
title('the image of Accurate solution')
xlabel('x');ylabel('y');zlabel('U');
subplot(1,3,3)
mesh(X,Y,(Numerical-Accurate)');
title('the image of error solution')
xlabel('x');ylabel('y');zlabel('error');
toc;
效果图:

紧差分格式:


tic;
clear
clc
M=100;%x的步数
N=100;%y的步数
h1=1/M;%x的步长
h2=1/N;%y的步长
x=0:h1:1;
y=0:h2:1;
u=zeros(M+1,N+1);%给数值解分配内存单元
U=u;%给精确解分配内存单元
u(1,:)=y.^3;%y边值
u(M+1,:)=1+y.^3;%y边值
u(:,1)=x.^3;%x边值。uo
u(:,N+1)=1+x.^3;%x边值。un
for i=1:M+1
for j=1:N+1
Accurate(i,j)=x(i)^3+y(j)^3;%精确解
end
end
b1=5/3*(M^2+N^2);
b2=1/6*(5*M^2-N^2);
b3=1/6*(5*N^2-M^2);
f=inline('-6*(x+y)','x','y');
for i=1:M-1
for j=1:N-1
ABf(i,j)=(1/144)*(f(x(i),y(j))+10*f(x(i),y(j+1))+f(x(i),y(j+2))...
+10*f(x(i+1),y(j))+100*f(x(i+1),y(j+1))+10*f(x(i+1),y(j+2))...
+f(x(i+2),y(j))+10*f(x(i+2),y(j+1))+f(x(i+2),y(j+2)));
end
end
Numerical=u;
error=eye(M+1,N+1);
while norm(error,inf) >= 1e-10
for i=2:M
for j=2:N
Numerical(i,j)=(ABf(i-1,j-1)+1/20*b1*Numerical(i-1,j-1)+b3*Numerical(i,j-1)+1/20*b1*Numerical(i+1,j-1)...
+b2*Numerical(i-1,j)+b2*u(i+1,j)...
+1/20*b1*u(i-1,j+1)+b3*u(i,j+1)+1/20*b1*u(i+1,j+1))/b1;
end
end
error=Numerical-u;
u=Numerical;
end
[X,Y]=meshgrid(x,y);
subplot(1,3,1)
mesh(X,Y,Numerical');
title('the image of Numerical solution')
xlabel('x');ylabel('y');zlabel('u');
subplot(1,3,2)
mesh(X,Y,Accurate');
title('the image of Accurate solution')
xlabel('x');ylabel('y');zlabel('U');
subplot(1,3,3)
mesh(X,Y,(Numerical-Accurate)');
title('the image of error solution')
xlabel('x');ylabel('y');zlabel('error');
toc;
效果图:

Matlab:五点差分方法求解椭圆方程非导数边值问题的更多相关文章
- Matlab:Crank Nicolson方法求解线性抛物方程
tic; clear clc M=[,,,,,,];%x的步数 K=M; %时间t的步数 :length(M) hx=/M(p); ht=/K(p); r=ht/hx^; %网格比 x=:hx:; t ...
- Matlab:椭圆方程的导数边值问题
tic; clear clc N=; M=*N; h1=/M; h2=/N; x=:h1:; y=:h2:; fun=inline('exp(x)*sin(pi*y)','x','y'); f=inl ...
- 【matlab】MATLAB程序调试方法和过程
3.8 MATLAB程序的调试和优化 在MATLAB的程序调试过程中,不仅要求程序能够满足设计者的设计需求,而且还要求程序调试能够优化程序的性能,这样使得程序调试有时比程序设计更为复杂.MATLAB ...
- Complete the sequence! POJ - 1398 差分方法找数列规律
参考链接:http://rchardx.is-programmer.com/posts/16142.html vj题目链接:https://vjudge.net/contest/273000#stat ...
- FESTUNG — 3. 采用 HDG 方法求解对流问题
FESTUNG - 3. 采用 HDG 方法求解对流问题[1] 1. 控制方程 线性对流问题控制方程为 \[\begin{array}{ll} \partial_t c + \nabla \cdot ...
- 彻底理解了call()方法,apply()方法和bind()方法
javascript中的每一个作用域中都有一个this对象,它代表的是调用函数的对象.在全局作用域中,this代表的是全局对象(在web浏览器中指的是window).如果包含this的函数是一个对象的 ...
- C语言多种方法求解字符串编辑距离问题的代码
把做工程过程经常用的内容记录起来,如下内容段是关于C语言多种方法求解字符串编辑距离问题的内容. { if(xbeg > xend) { if(ybeg > yend) return 0; ...
- 三种初步简易的方法求解数值问题 of C++
1. “二分法解方程” 在二分法中,从区间[a,b]开始,用函数值f(a)与f(b)拥有相反的符号.如果f在这个区间连续,则f的图像至少在x=a,x=b之间穿越x轴一次,因此方程f(x)=0在[a,b ...
- Minimum_Window_Substring两种方法求解
题目描述: Given a string S and a string T, find the minimum window in S which will contain all the chara ...
随机推荐
- https证书随记
下载证书之后: 1:域名跳转操作 <system.webServer> <rewrite> <rules> ...
- python框架之Django(8)-CBV中添加装饰器
现有如下检查登录装饰器: from functools import wraps def check_login(func): @wraps(func) def inner(request, *arg ...
- 【Data Structure】-NO.117.DS.1 -【Tree-23树】
[Data Structure]-NO.117.DS.1 -[Tree-23树] Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total ...
- ubuntu 安装完后对于开发需要做的事情
是从 https://www.osboxes.org/ubuntu/ 下载的vdi文件,估计vmware对应的应该也有. 1. 安装 openssh-server apt-get install op ...
- python常用函数总结
原文地址https://www.cnblogs.com/nice107/p/8118876.html 我们在学习python的时候,接触最多的往往则是那些函数,对于python函数,在这里为大家总结归 ...
- python进阶(三) 内建函数getattr工厂模式
getattr()这个方法最主要的作用是实现反射机制.也就是说可以通过字符串获取方法实例. 传入不同的字符串,调用的方法不一样. 原型:getattr(对象,方法名) 举个栗子: pyMethod类 ...
- Kube-DNS搭建(1.4版本)
目录贴:Kubernetes学习系列 1.介绍 之前介绍过DNS的搭建(基于Kubernetes集群部署skyDNS服务),但那个版本的DNS是随着Kubernetes1.2发布出来的,有点原始.本文 ...
- /* * 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据 *(包括学生号,姓名,三门课成绩),计算出平均成绩, *将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。 */
1.Student类:类中有五个变量,分别是学号,姓名,三门成绩 package test3; public class Student { private int num; private Stri ...
- 第九节 JS运动应用
多物体运动框架 多个物体同时运动 例子:多个Div,鼠标移入变宽 单定时器,存在问题 每个Div一个定时器 <!DOCTYPE html> <html lang="en&q ...
- PHP防止网页快速刷新+代理ip访问
前几天网站收到了一些CC攻击,比较郁闷...这里分享一下,防止网页自动刷新的方法以及阻止代理IP访问网站的方法,代码是分开的,两个功能,需要那个用那个,可以自定义时间间隔,这个代码不止可以防CC攻击, ...