solving PDE problem : Linear Advection diffusion equation problem
Mostrar comentarios más antiguos
I try to learn how to solve Time dependent PDE in matlab by myself. This question is from Tobin's book.

The initial condtion is 
My matlab code is as follows:
n = 100 ; h = 2/n; %n intervals, width 2/n
x = -1 + h* (1: n-1)'; %node locations
D1 = diag(ones(n-1,1),1) - diag(ones(n-1,1),-1) * (1/(2*h)); % the centered finite diff matrix in place of u_x
D2 = toeplitz ([-2 1 zeros(1, n-3)]/ h^2); %diff matrix for u_xx
f = @(t, u) (0.2*D2*u ) - (D1 * u); % discretized du/dt
u0 = (1 - x.^2) ./ (1 + 50*x.^2); %initial condition
[t, u] = ode15s (f, [0 2], u0); %solve
waterfall (x, t, u)
When I run the code, I get the following error message;
Error using *
Inner matrix dimensions must agree.
Error in exercise_713>@(t,u)(0.2*D2*u)-(D1*u)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in exercise_713 (line 16)
[t, u] = ode15s (f, [0 2], u0); %solve
How can I correct this code?
Please give me a feedback whether my matlab code is totally correct or not. Where is the mistake? I try to learn by myself. And I have no one in order to ask. Please give me an idea/comment about my code.
Thanks a lot.
Respuesta aceptada
Más respuestas (1)
Bjorn Gustavsson
el 8 de Mayo de 2020
When you work with problems like this you have to make sure that your matrices end up the sizes you expect them to be. To do that you should make plenty use of size, and whos. When I ran your code I got this:
whos
Name Size Bytes Class Attributes
D1 100x100 80000 double
D2 99x99 78408 double
f 1x1 32 function_handle
h 1x1 8 double
n 1x1 8 double
u0 99x1 792 double
x 99x1 792 double
So D1*u will not work. To do this you need to make sure that your D1 and D2 matrices are the correct domensions. One way I simplify this is to make the problme small enough for visual inspection - instead of 100 points reduce it to something on the order of 5-10. Then it also becomes more obvious what you'll need to maintain the boundary-conditions too.
This seems to be pretty OK. My 2 additional comments would be to reduce the diffusion-coefficient from 0.2 to 0.05 or something like that, and to compare the solution you get with centred first derivative and an up-stream derivative. Also, as a physicist the boundary conditions make my head hurt, what is supposed to happen at the down-stream boundary?
HTH
1 comentario
Zeynep Toprak
el 8 de Mayo de 2020
Categorías
Más información sobre Boundary Conditions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
