How to solve this question in MATLAB?

1 view (last 30 days)
Nannthini
Nannthini on 11 Jan 2023
Commented: Nannthini on 11 Jan 2023
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
Jiri Hajek
Jiri Hajek on 11 Jan 2023
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

KSSV
KSSV on 11 Jan 2023
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
KSSV
KSSV on 11 Jan 2023
Edited: KSSV on 11 Jan 2023
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)
Nannthini
Nannthini on 11 Jan 2023
Thank you so much. Now the value U(x,t) is match with my manual calculation.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!