How to display a certain answer

20 vues (au cours des 30 derniers jours)
LightFury Yeji
LightFury Yeji le 14 Fév 2021
I currently have the output
If I only want to display the value of w(i,j) for, let's say, when xi=0.5 and tj=0.3 (so the answer should be -0.2781152949), how do I do it?
I wanna do something like
fprintf( 'The value of w when x = %.1f and t = %.1f is %.10f \n',xi, tj, wij(i,j));
%this is my code by the way, for reference
%Step 0
l= 1;
T= 0.5;
alpha = 1;
m=10;
N=5;
syms f(x) g(x)
f(x)= 0.9*cos(2*pi*x);
g(x)= 0;
%STEP1
h=l/m;
k=T/N;
lambda=k*alpha/h;
%STEP2
wij = zeros(m,N);
for j=2:N %j=1: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); %wij(m,0)
%STEP4
for i=2:m-1 %i=1:m-1
wij(i,1)=f(i*h); %wij(i,0)
wij(i,2)=(1-lambda^2)*(f(i*h))+((lambda^2)/2)*(f((i+1)*h)+(f((i-1)*h))+k*(g(i*h))); %wij(i,1)
end
%STEP5
for j=2:N-1 %j=1:N-1
for i=2:m-1 %i=1: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
disp('xi tj w(i,j)')
for j=1:N %j=0:N
t=j*k;
for i=1:m %i=0:m
x=i*h;
end
end
%i am planning to print here

Réponses (1)

Walter Roberson
Walter Roberson le 14 Fév 2021
[first_found, first_idx] = ismembertol(xi, list_of_xi);
[second_found, second_idx] = ismembertol(tj, list_of_tj);
if first_found && second_found
fprintf( 'The value of w when x = %.1f and t = %.1f is %.10f \n',xi, tj, wij(first_idx,second_idx));
else
fprintf('Nothing close enough to x = %.1f and t = %.1f exists in the matrix\n', xi, tj)
end

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by