Matlab code wont disply the figure 2
Show older comments
c= 1; % unit conversion
L=1; % t=[L]/[c]
C=0.1;
N=1000;
Np=N+1;
dx= L/N;
x=(0:dx:L)';
dt=C*dx/c;
sqrC=C*C;
% step2 convert PDE to linear algebraic equations
A= sparse(Np,Np); % matrix creation as rows,columns
u= zeros(Np,1); % unknowns
b= zeros(Np,1); %RHS
% generate a wave function
u = sin(3*pi*x);
%figure(1);clf;hold on
plot(x,u,'-b','Linewidth',L);
xlabel('x([L])')
ylabel('Amplitude([L])');
title('Intial Wave')
set(gca,'Linewidth',1,'fontsize',12)
box on;
ylim([-1,1]);
uset{1} = u; % future
uset{2} = u; % present
uset{3} = u; % previous
uset{4} = u; % previusly previous
% Apply boundary conditions
A(1,1)= 1; b(1)= 0;
A(Np,Np) = 1; b(Np) = 0;
for i= 2:Np-1
A(i,i) =2 +sqrC*2;
A(i,i-1) = -sqrC;
A(i,i+1) = -sqrC;
b(i) =5*uset{1}(i) - 4*uset{2}(i) + uset{3}(i);
end
%u=A/b
%uset{1}=u;
t=0;
Nt=100000;
for k=0:Nt
t= t+dt;
for i=2:Np-1
b(i) =5*uset{1}(i) - 4*uset{2}(i) + uset{3}(i);
end
u=A\b;
uset{3}=uset{2};
uset{2}= uset{1};
uset{1}=u;
end
if k==1
figure(2); clf; hold on;
xlabel('x')
ylabel('y','amplitude')
set(gca,'Linewidth',1,'fontsize',12)
box on
h=1;
ylim([-1,1])
end
if mod(k,50)==0
% figure(2);
if h~=0; delete(h); end
h = plot(x,u, '-b', 'Linewidth', 2);
title(['Time:' num2str(t)]);
end
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!