隐式欧拉方法matlab

时间:2016-03-10 18:58:19

标签: matlab

我想在Matlab中解决隐式Euler方法 当f(x)= 0时我已经完成了代码,但我不明白我怎么能改变代码,因为我有f(x)=(cost +π2sint)sin(πx) f(x)= 0的代码:

function Comp3task1(Nx,Nt,n1)

a=-1;b=1;Tf=1;
h=(b-a)/(Nx+1);
taf=Tf/Nt;
m=taf/(h^2);

u=zeros(Nx+1,Nt+1);
%Define x(i)    x(1)=a;
for i=2:(Nx+2)
    x(i)=x(i-1)+h;
    display(x)
end
%define t(i) t(1)=0;
for i=2:(Nx+1)
    t(i)=t(i-1)+taf;
end
%using the initial condition
for i=2:(Nx+1)
    u(i,1)=sin(pi*X(i));
end
%using boundary condition
for n=1:(Nt+1)
    u(1,n)=0;
    u(Nx+2,n)=0;
end
%calculate the approximation of the solution u(t(n),x(i))
for n=1:Nt
    for i=2:(Nx+1)
         u(i,n)=-m*u(i-1,n+1)+(1+2*m)*u(i,n+1)-m*u(i+1,n+1);
    end
end
%plot the approxrimations
n2=n1+1;
i=1:Nx+2;
plot(x,u(i,n2));
axis([0 0.1 0 0.1]);

end

0 个答案:

没有答案