Help Solving Equation with Matlab - Get Expert Advice Now!
Context: MATLAB
Thread starterxuej1112
Start date
Click For Summary
Discussion Overview
The discussion revolves around solving a specific equation using MATLAB, focusing on the implementation of boundary conditions and numerical methods. Participants share code snippets and seek advice on how to properly express and implement these conditions in their MATLAB programs.
Discussion Character
Technical explanation
Mathematical reasoning
Homework-related
Debate/contested
Main Points Raised
One participant expresses difficulty in processing boundary conditions in MATLAB and requests code examples.
Another participant provides boundary conditions in mathematical form and suggests how to implement them in MATLAB code.
There is a discussion about the necessity to modify boundary conditions after each time step in the numerical solution.
A participant shares their own MATLAB code but suspects there may be errors in their implementation.
Some participants discuss the implicit difference method and its implications for the system of equations being solved.
Questions arise about the choice of evaluating the PDE at different time steps and the reasoning behind using implicit methods.
One participant inquires about the possibility of finding an analytical solution for the equation using MATLAB.
Another participant shares their experience with a similar problem involving a nonlinear PDE and mentions using the Crank-Nicholson scheme.
There are requests for help in debugging shared code snippets, indicating ongoing issues with implementation.
Areas of Agreement / Disagreement
Participants do not reach a consensus on the best approach to implement the boundary conditions or the numerical method to use. Multiple competing views and methods are presented, and the discussion remains unresolved regarding the optimal solution.
Contextual Notes
Participants express uncertainty about specific implementations and the correctness of their MATLAB code. There are mentions of potential errors in the code shared, but no specific corrections are provided.
Who May Find This Useful
Readers interested in numerical methods for solving partial differential equations, MATLAB programming, and boundary condition implementation may find this discussion relevant.
#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 [itex]u^i[/itex] be the vector of [itex]B_j^i[/itex] 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);
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 [itex]\Lambda[/itex], 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:
[tex]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},[/tex]
Why are you evaluating the PDE in [itex]t_{i+1}[/itex]?
#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