How to display a certain answer

2 ビュー (過去 30 日間)
LightFury Yeji
LightFury Yeji 2021 年 2 月 14 日
回答済み: Walter Roberson 2021 年 2 月 14 日
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

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 2 月 14 日
[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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by