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

1 visualización (últimos 30 días)
LightFury Yeji
LightFury Yeji el 7 de Feb. de 2021
Comentada: LightFury Yeji el 7 de Feb. de 2021
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

Respuestas (1)

Walter Roberson
Walter Roberson el 7 de Feb. de 2021
%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 comentarios
Walter Roberson
Walter Roberson el 7 de Feb. de 2021
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 el 7 de Feb. de 2021
Oh, thank you so much!

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by