Matlab code wont disply the figure 2
2 views (last 30 days)
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
0 Comments
Accepted Answer
Walter Roberson
on 13 Oct 2023
h is not initialized so you cannot test
if h~=0; delete(h); end
2 Comments
Dyuman Joshi
on 13 Oct 2023
No, that statement is subjected to a condition i.e. k==1. And as the condition is not satisfied (because the value of k is Nt), h=1 is not executed.
You can directly over-write h (if it has been defined), there's no need to delete it.
More Answers (0)
See Also
Categories
Find more on Geometry and Mesh 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!