资源描述:
《偏微分方程数值算法及matlab实验报告(6)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、偏微分方程数值实验报告六实验题目:用Ritz-Galerkin方法求边值问题的第n次近似,基函数为并用表格列出0.25,0.5,0.75三点处的真解和时的数值解。实验算法:将上述边值问题转化为基于虚功方程的变分问题为:求,满足其中记,引入的n维近似子空间利用Ritz-Galerkin公式:,则问题关于下的近似变分问题解中的系数满足程序代码:%主函数pde=modeldata();I=[0,1];%积分精度quadorder=10;n=[1,2,3,4];fori=1:length(n)uh{i}=Galerkin(pde,I,n(i),qu
2、adorder);endshowsolution(uh{1},'-bo');holdonshowsolution(uh{2},'-rx');holdonshowsolution(uh{3},'-.ko');holdonshowsolution(uh{4},'--k*');holdofftitle('thesolutionoftheun');xlabel('x');ylabel('u');legend('n=1','n=2','n=3','n=4');%计算这三点的数值解x=[1/4,1/2,3/4];un=zeros(length(n),3
3、);fori=1:length(n)[v,]=basis(x,n(i));un(i,:)=(v'*uh{i})';endformatshortedisp('un');disp(un);%存储数据functionpde=modeldata()pde=struct('f',@f);functionz=f(x)z=x.*x;endend%Galerkin方法functionuh=Galerkin(pde,I,n,quadorder)h=I(2)-I(1);[lambda,weight]=quadpts1d(I,quadorder);nQuad=l
4、ength(weight);A=zeros(n,n);b=zeros(n,1);forq=1:nQuadgx=lambda(q);w=weight(q);x=[0.25;0.5;0.75];[phi,gradPhi]=basis(gx,n);A=A+(-gradPhi*gradPhi'+phi*phi')*w;b=b+pde.f(gx)*phi*w;endA=h*A;b=h*b;uh=Ab;end%构造基函数function[phi,gradPhi]=basis(x,n)m=length(x);w=sin(pi*x);v=ones(n,m
5、);v(2:end,:)=bsxfun(@times,v(2:end,:),x);v=cumprod(v,1);phi=bsxfun(@times,v,w);gw=pi*cos(pi*x);gv=[zeros(1,m);v(1:end-1,:)];gv(3:end,:)=bsxfun(@times,(2:n-1)',gv(3:end,:));gradPhi=bsxfun(@times,v,gw)+bsxfun(@times,gv,w);end%数值解的图像functionshowsolution(u,varargin)x=0:0.01:1;
6、n=length(u);[v,]=basis(x,n);y=v'*u;plot(x,y,varargin{:});%varargin:aninputvariableinafunctionend%系数矩阵Afunction[lambda,weight]=quadpts1d(I,quadorder)numPts=ceil((quadorder+1)/2);ifnumPts>10numPts=10;endswitchnumPtscase1A=[02.0000000000000000000000000];case2A=[0.577350269189
7、62576450914881.0000000000000000000000000-0.57735026918962576450914881.0000000000000000000000000];case3A=[00.88888888888888888888888890.77459666924148337703585310.5555555555555555555555556-0.77459666924148337703585310.5555555555555555555555556];case4A=[0.3399810435848562648
8、0266580.65214515486254614262693610.86113631159405257522394650.3478548451374538573730639-0