Matlab code wont disply the figure 2

1 次查看(过去 30 天)
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
Unrecognized function or variable 'h'.

采纳的回答

Walter Roberson
Walter Roberson 2023-10-13
h is not initialized so you cannot test
if h~=0; delete(h); end
  2 个评论
Bijaya
Bijaya 2023-10-13
移动:Dyuman Joshi 2023-10-13
I have already defined h=1; BUT it still keep on giving same error . How do i fix it ?
Dyuman Joshi
Dyuman Joshi 2023-10-13
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by