필터 지우기
필터 지우기

Matlab code wont disply the figure 2

조회 수: 1 (최근 30일)
Bijaya
Bijaya 2023년 10월 13일
댓글: Dyuman Joshi 2023년 10월 13일
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