How to solve this question in MATLAB?

I have tried this question manually as well as in MATLAB but the value of U(x,t) not match with my manual calculation answer. I dont know where I make the mistake.
%Evaluate BC
dx = 0.25;
X = 0:dx:1;
dt = 0.1;
t = 0:dt:1;
h = 1/(length(X)-1) ; %step size
u = zeros(length(t),length(X));
for j = 1:length(t)
for i=1:length(X)
u(j,i) = exp((-pi*j)/4)*sin((pi*i)/2);
end
end
disp([X' u'])

1 Comment

Hi, in your expression, you are not using as inputs x(i) and t(j), but just the indices...

Sign in to comment.

 Accepted Answer

dx = 0.25 ;
dt = 0.1 ;
x = 0:dx:1 ;
t = 0:dt:1 ;
[x,t] = meshgrid(x,t) ;
U = exp(-pi*t/4).*sin(pi*x/2) ;
surf(x,t,U)

2 Comments

You have used the indices of loop in your code. You need index x and t in the loop.
%Evaluate BC
dx = 0.25;
x = 0:dx:1;
dt = 0.1;
t = 0:dt:1;
u = zeros(length(t),length(x));
for j = 1:length(t)
for i=1:length(x)
u(j,i) = exp((-pi*t(j))/4)*sin((pi*x(i))/2);
end
end
surf(x,t,u)
Thank you so much. Now the value U(x,t) is match with my manual calculation.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 11 Jan 2023

Commented:

on 11 Jan 2023

Community Treasure Hunt

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

Start Hunting!