MATLABHelp Solving Equation with Matlab - Get Expert Advice Now!
Thread starterxuej1112
Start date
AI Thread Summary
The discussion centers on solving a partial differential equation (PDE) using MATLAB, with a specific focus on implementing boundary conditions. The initial condition is defined, and participants share code snippets to illustrate how to express boundary conditions in MATLAB. Key points include the need to update boundary conditions at each time step and the suggestion to use matrix forms for the equations to simplify calculations. An implicit difference method is mentioned, with a discussion on the advantages of forward versus backward evaluation of the PDE. Participants also explore the creation of a matrix to represent the system of equations and how to initialize and update values within the MATLAB code. There are requests for code reviews and troubleshooting, indicating ongoing challenges in getting the implementation to work correctly. The conversation highlights the importance of correctly defining boundary conditions and the structure of the solution matrix in numerical methods for PDEs.
#1
xuej1112
17
0
I am working for a week to solve this equation by matlab,but I am lose.I don't know how to process the boundary condition.
yes, I know these are the boundary conditions.I mean that I don't know how to express them in the MATLAB program .
but thanks for your reply.
#4
AiRAVATA
172
0
You'll have to specify them in the first time step, then change them according to the evolution of your program. That means modifying them after each step is calculated. Let u^i be the vector of B_j^i entries (j=1,...n), then
Code:
r = 0:h:r*
u = ones(n+1,1) % Initial condition
% Begin time steps
for i=1:m
u(1,1) = u(1,1) - (a*k/h)*(u(2,1)-u(1,1)); % Left boundary condition
u(2:n,1) = Matrix*u(2:n,1); % Matrix form of the PDE
u(n+1,1) = u(n,1); % Right boundary condition
v(:,i) = u(:,1); % This will create a matrix with the value of u^i in each column
end
Last edited:
#5
xuej1112
17
0
Thank you so much! you a so kind!
I wrote some code this afternoon.but i think there is some thing wrong
r is the longitudinal coordinates, t is the x-coordinate:
dr = 0.01; %each depth step
Nr = 100; % Choose the number of depth steps
Nt = 40; % Choose the number of time steps
dt = (10*365*24*60*60)/Nt; %Length of each time step in seconds
T = zeros(Nr+1,Nt+1); %Create temperature matrix with Nz+1 rows, and Nt+1 columns
time = [0:120/Nt:120]; %120 months in 10 years
T(:,1) = 1; %Set initial condition
maxiter = 500
for iter = 1:maxiter
%Tlast = T; %Save the last guess
%T(:,1) = Tlast(:,end); %Initialize the temp at t=0 to the last temp
for i=2:Nt+1
T(1,i)=(1+dt/dr)*T(1,i-1)-(dt/dr)*T(2,i-1);
B^{i+1} = \left( \begin{array}{cccccc} \alpha_2 & \beta_2 & \gamma_2 & 0 & 0 & \cdots \\ 0 & \alpha_3 & \beta_3 & \gamma_3 & 0 & \cdots \\ 0 & 0 & \alpha_4 & \beta_4 & \gamma_4 & \cdots \\ \vdots & & & & & \end{array}\right) B^i = \Lambda B^i,
u = ones(1,n) ; % Initial condition
MatrixLambda = something % Definition of Lambda.
for i=1:m
u(1,1) = u(1,1) - (a*k/h)*(u(2,1)-u(1,1)); % Left boundary (j = 0)
u(2,1) = a_1 u(1,1) + b_1 u(2,1) + c_1 u(3,1) % j = 1
u(3:n,1) = MatrixLambda*u(3:n,1); % j = 2, ..., n
u(n+1,1) = u(n,1); % Right boundary
V(:,i) = u(1:n+1,1); % Matrix V with the values of u^i in each column
end
To generate the matrix \Lambda, I recommend you to check the Matlab function diag.
Code:
help diag
Puff! Hope this helps.
Last edited:
#7
xuej1112
17
0
thank u so mush!but I think my difference method is implicit difference that is:
B_j^i = \frac{\sigma_j k}{h} B_{j-1}^{i+1} + \left(1-\frac{2 \sigma_j k}{h^2}-\frac{\mu_j k}{h}-r_j\right)B_j^{i+1} + \left(\frac{\sigma_j k}{h^2}+\frac{\mu_j k}{h}\right)B_{j+1}^{i+1},
so:
B^i = \left( \begin{array}{cccccc} \alpha_2 & \beta_2 & \gamma_2 & 0 & 0 & \cdots \\ 0 & \alpha_3 & \beta_3 & \gamma_3 & 0 & \cdots \\ 0 & 0 & \alpha_4 & \beta_4 & \gamma_4 & \cdots \\ \vdots & & & & & \end{array}\right) B^{i+1} = \Lambda B^{i+1},
maybe this is a equation system
#8
AiRAVATA
172
0
Why are you evaluating the PDE in t_{i+1}?
#9
xuej1112
17
0
because I use the implicit difference method.It is steady.So,maybe this is a equation system.
#10
AiRAVATA
172
0
Why are you going backwards if you can go forward?
#11
xuej1112
17
0
Thank you so much.
by the way,could I find the analyze solution of this equation by matlab?
#12
AiRAVATA
172
0
What exactly do you mean?
You want plots and stuff?
I did sort of the same thing for my undergraduate thesis, but my pde was nonlinear, so I had to use the Crank-Nicholson scheme (basically the same thing you did), that's why I was able to help :).
#13
xuej1112
17
0
thank you man! I am just back from norway.
I tried the code you give me,but it is not work.
The following is the code,could you help me to check it?
>>dr = 0.005;
Nr = 20;
dt = 0.1;
Nt = 20;
a=0.2339*0.0189;
b=0.2339;
delta=sqrt(0.0073);
u=zeros(Nr+1,Nt+1);
u(1,:)=1;
for j=3:Nr
alpha=(1/2)*delta^2*dr*(j-1);
mu=a-b*dr*(j-1);
rho=dt/(dr)^2;
A=diag((1-2*rho*alpha-(dt/dr)*mu-dt*dr*(j-1))*ones(Nr-2,1))-diag((rho*alpha+(dt/dr)*mu)*ones(Nr-3,1),1)-diag((rho*alpha)*ones(Nr-3,1),-1);
end
MatrixLambda = A; % Definition of Lambda.
for i=1:Nt
u(1,1) = u(1,1) - (a*dt/dr)*(u(2,1)-u(1,1)); % Left boundary (j = 0)
u(2,1) = rho*(1/2)*delta^2*dr*1* u(1,1) + 1-2*rho*(1/2)*delta^2*dr*1-(dt/dr)*(a-b*dr*1)-dt*dr*1* u(2,1) + rho*(1/2)*delta^2*dr*1+(dt/dr)*(a-b*dr*1)* u(3,1); % j = 1
u(3:Nr,1) = MatrixLambda*u(3:Nr,1); % j = 2, ..., n
u(Nr+1,1) = u(Nr,1); % Right boundary
V(:,i) = u(1:Nr+1,1); % Matrix V with the values of u^i in each column
end