필터 지우기
필터 지우기

I only need to display the first 11 rows of the answer

조회 수: 1 (최근 30일)
LightFury Yeji
LightFury Yeji 2021년 2월 7일
댓글: LightFury Yeji 2021년 2월 7일
My program displays an output with so many rows, but I only need the first 11. How do i solve this? Please help
This is the output (and not everything is shown because there are a lot of rows):
This is the desired output:
0.1 0.1 0.0
0.2 0.1 0.5877852523
0.3 0.1 0.8090169944
0.4 0.1 0.9510565163
0.5 0.1 1.0
0.6 0.1 0.9510565163
0.7 0.1 0.8090169944
0.8 0.1 0.5877852523
0.9 0.1 0.3090169944
1.0 0.1 0.0
This is my program:
l=1; %endpoint
T=1; %maximum time
alpha = 2; %constant
m=10;
N=20;
syms f(x) g(x,t)
f(x)=sin(pi*x);
g(x)=(sin(pi*x))*(cos(2*pi*t));
%STEP1
h=l/m;
k=T/N;
lambda=k*alpha/h;
%STEP2
wij = zeros(m,N,'sym');
for j=2:N
wij(1,j)=0; %wij(0,j)
wij(m,j)=0;
end
%STEP3
wij(1,1)=f(0); %wij(0,0)
wij(m,1)=f(l);
%STEP4
for i=2:m-1
wij(i,1)=subs(f(x),x,(i*h)); %wij(i,0)
wij(i,2)=(1-lambda^2)*(subs(f(x),x,(i*h)))+(lambda^2/2)*(subs(f(x),x,((i+1)*h)))+(subs(f(x),x,((i-1)*h)))+k*(subs(g(x),x,(i*h)));
end
%STEP5
for j=2:N-1
for i=2:m-1
wij(i,j+1)=2*(1-lambda^2)*wij(i,j)+lambda^2*(wij(i+1,j)+wij(i-1,j))-wij(i,j-1);
end
end
%STEP6
for j=1:N
t=j*k;
for i=1:m
x=i*h;
fprintf( '%.1f %.1f %s\n',x, t, char(vpa(wij(i,j),10)));
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2021년 2월 7일
%STEP6
stopping_loop = false;
for j=1:N
t=j*k;
for i=1:m
x=i*h;
if ~isempty(symvar(wij(i,j))
stopping_loop = true;
break;
end
fprintf( '%.1f %.1f %.10f\n',x, t, double(wij(i,j)));
end
if stopping_loop; break; end
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 2월 7일
l=1; %endpoint
T=1; %maximum time
alpha = 2; %constant
m=10;
N=20;
syms f(x) g(x,t)
f(x)=sin(pi*x);
g(x)=(sin(pi*x))*(cos(2*pi*t));
%STEP1
h=l/m;
k=T/N;
lambda=k*alpha/h;
%STEP2
wij = zeros(m,N,'sym');
for j=2:N
wij(1,j)=0; %wij(0,j)
wij(m,j)=0;
end
%STEP3
wij(1,1)=f(0); %wij(0,0)
wij(m,1)=f(l);
%STEP4
for i=2:m-1
wij(i,1)=subs(f(x),x,(i*h)); %wij(i,0)
wij(i,2)=(1-lambda^2)*(subs(f(x),x,(i*h)))+(lambda^2/2)*(subs(f(x),x,((i+1)*h)))+(subs(f(x),x,((i-1)*h)))+k*(subs(g(x),x,(i*h)));
end
%STEP5
for j=2:N-1
for i=2:m-1
wij(i,j+1)=2*(1-lambda^2)*wij(i,j)+lambda^2*(wij(i+1,j)+wij(i-1,j))-wij(i,j-1);
end
end
%STEP6
stopping_loop = false;
for j=1:N
t=j*k;
for i=1:m
x=i*h;
if ~isempty(symvar(wij(i,j)))
stopping_loop = true;
break;
end
fprintf( '%.1f %.1f %.10f\n',x, t, double(wij(i,j)));
end
if stopping_loop; break; end
end
0.1 0.1 0.0000000000 0.2 0.1 0.5877852523 0.3 0.1 0.8090169944 0.4 0.1 0.9510565163 0.5 0.1 1.0000000000 0.6 0.1 0.9510565163 0.7 0.1 0.8090169944 0.8 0.1 0.5877852523 0.9 0.1 0.3090169944 1.0 0.1 0.0000000000 0.1 0.1 0.0000000000
LightFury Yeji
LightFury Yeji 2021년 2월 7일
Oh, thank you so much!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by