资源描述:
《matlab程序范例》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.二阶椭圆方程functioneqsolv(1).%-ddu/dxx+u=fon[aa,bb]withu(a)=sin(aa),u(b)=sin(bb)formatlong%formatshortaa=-1;bb=1;h=0.00005;N=(bb-aa)/hx=zeros(N+1,1);RHS=zeros(N-1,1);un=zeros(N+1,1);ue=zeros(N+1,1);fork=1:N+1x(k)=aa+h*(k-1);ue(k)=sin(x(k));endfork=1:N-1RHS(k)=2*h*2*sin(x(k+1));endRHS
2、(1)=RHS(1)+ue(1)*2/h;RHS(N-1)=RHS(N-1)+ue(N+1)*2/h;%tousegaussmethod%A=zeros(N-1,N);%fork=1:N-2%A(k,k+1)=-2/h;%A(k,k)=4/h+2*h;%A(k+1,k)=-2/h;%end%A(N-1,N-1)=4/h+2*h;%A(1:N-1,N)=RHS;%un(2:N)=gaussc(A);A=zeros(N-1,3);fork=1:N-1A(k,2)=4/h+2*h;%A(k,k)=4/h+2*h;A(k,1)=-2/h;%A(k+1,k)=-2/
3、h;A(k,3)=-2/h;%A(k,k+1)=-2/h;endL=zeros(N-1,1);R=zeros(N-1,2);R(1,1)=A(1,2);%R(1,1)=A(1,1);fork=1:N-2R(k,2)=A(k,3);%R(k,2)=A(k,k+1);endfork=2:N-1L(k)=A(k-1,1)/R(k-1,1);%L(k)=A(k,k-1)/R(k-1,1);R(k,1)=A(k,2)-L(k)*R(k-1,2);%R(k,1)=A(k,k)-L(k)*R(k-1,2);end%tosolveLY=dy=zeros(N-1,1);y(
4、1)=RHS(1);fork=2:N-1y(k)=RHS(k)-L(k)*y(k-1);end%%tosolveUX=Yun(N)=y(N-1)/R(N-1,1);fork=N-1:-1:2un(k)=(y(k-1)-R(k-1,2)*un(k+1))/R(k-1,1);end%tocomputethemaxerrun(1)=ue(1);un(N+1)=ue(N+1);un=un-ue;err_max=abs(un(1));fork=2:N+1iferr_max5、unctionejtyfc%-d(p*du/dx)/dx+q*u=fon[aa,bb]withu(a)=sin(aa),u(b)=sin(bb)%p(a)=cos(aa),p(b)=cos(bb)q(a)=aa^2,q(b)=bb^2formatlongformatshortaa=-1;bb=1;h=0.0005;N=(bb-aa)/h;x=zeros(N+1,1);x1=zeros(N,1);g=zeros(N-1,1);ue=zeros(N+1,1);un=zeros(N+1,1);fori=1:N+1x(i)=aa+h*(i-1);ue(i)=sin
6、(x(i));endfori=1:Nx1(i)=(x(i)+x(i+1))/2;endfori=1:N-1g(i)=2*h*(sin(2*x(i+1))+x(i+1)^2*sin(x(i+1)));endg(1)=g(1)+ue(1)*2*cos(x1(1))/h;g(N-1)=g(N-1)+ue(N+1)*2*cos(x1(N))/h;if(0)A=zeros(N-1,N);fori=1:N-2A(i,i)=2*(cos(x1(i+1))+cos(x1(i)))/h+2*h*x(i+1)^2;A(i+1,i)=-2*cos(x1(i+1))/h;A(i,
7、i+1)=-2*cos(x1(i+1))/h;endA(N-1,N-1)=2*(cos(x1(N))+cos(x1(N-1)))/h+2*h*x(N)^2;%g=A(1:N-1,1:N-1)*ue(2:N);A(1:N-1,N)=g;un(2:N)=gaussc(A);un(2:N)-ue(2:N)un(1)=ue(1);un(N+1)=ue(N+1);un=un-ue;err_max=abs(un(1));fori=2:N+1iferr_max8、ros(N-1,3);fori=1:N-2A(i+1,1)=-2*