%Ecuacion de la onda por Diferencias Finitas Centradas
m=input('Dar el numero de intervalos para la variable espacial: m = ');
n=input('Dar el numero de intervalos para la variable temporal: n = '); a=input('Ingrese el extremo a = '); b=input('Ingrese el extremo b = '); T=input('Ingrese el tiempo final T = '); c=input('Ingrese el coeficiente C = '); fun=input('Sea u(x,0)=f(x), ingrese: f(x) = ','s'); gun=input('Sea du/dt(x,0)=g(x,0), ingrese: g(x) = ','s'); f=inline(fun); g=inline(gun); h1un=input('Dar u(a,t) = h1(t): h1(t) = ','s'); h2un=input('Dar u(b,t) = h2(t): h2(t) = ','s'); h1=inline(h1un); h2=inline(h2un);
h=(b-a)/m; k=T/n; r=((c^2)*(k^2))/(h^2);
x=linspace(a,b,m+1); t=linspace(0,T,n+1); u(1,1:n+1)=h1(t); u(m+1,1:n+1)=h2(t); u(2:m,1)=f(x); %Wo u(2:m,2)=f(x)+k*g(x); A=2*(1-r)*eye(m-1)+r*diag(ones(1,m-2),+1)+r*diag(ones(1,m-2),-1); for j=3:n+1 u(2:m,j)=A*u(2:m,j-1)-u(2:m,j-2)+r*[h1(t(j-1));zeros(m-3,1);h2(t(j- 1))]; end x=[a x b]; %[x,t]=meshgrid(x,t); w=u' surf(x,t,w); xlabel('x'); ylabel('t');