How to code a couple ODE with nodes

조회 수: 13 (최근 30일)
ehsan rezaei
ehsan rezaei 2022년 2월 4일
답변: ehsan rezaei 2022년 2월 5일
Hello everyone. I am trying to model a thermal system. It is a system of the equation that must be solved:
Xi and Yi are the temperatures at a hypothetical element. (i-1) and (i+1) refer to the previous and next node and A to G are some known coefficients. I consider the ode45 to solve these coupled equations, but the problem is with defining a new parameter such as Z to involve both X and Y, how the nodes (i-1 i i+1) be considered?
Can the following code be run with a few changes?
function zprime = myfun(t,z);
zprime=[A*x(1)(i) + B*x(1)(i-1) + C*x(1)(i+1) + D*x(2)(i) ; E*x(2)(i) + F*x(2)(i-1) + G*x(1)(i)]; % This is absoloutly wrong call!
z0=[a b];
tspan=[0,20];
[t,x]=ode45(@myfun,tspan,z0);
Many thanks!

채택된 답변

Davide Masiello
Davide Masiello 2022년 2월 5일
편집: Davide Masiello 2022년 2월 5일
The way you describe your problem looks similar to a discretization of a PDE.
You could try to modify the function as follows
function zprime = myfun(t,z);
N = 30; % Number of nodes
A = 1; B = 1; C = 1; D = 1; E = 1; F = 1; G = 1; % Your known constants
x = z(1:N); % Define variable x as first 30 elements of z
y = z(N+1:2*N); % Define variable y as second 30 elements of z
dxdt(1) = DEFINE BC;
dxdt(2:end-1) = A*x(2:end-1)+B*x(1:end-2)+C*x(3:end)+D*y(2:end-1);
dxdt(end) = DEFINE BC;
dydt(1) = DEFINE BC;
dydt(2:end-1) = E*y(2:end-1)+F*y(1:end-2)+G*x(2:end-1);
dydt(end) = DEFINE BC;
zprime=[dxdt;dydt];
end
As you can see, the code above is incomplete because you have to define the boundary conditions.
The reason your formulae would not work at, say, i=1 is that i-1=0, which matlab does not accept as index.
Also, at i=30 you have i+1=31 which is larger than the size of x and y.
Hope that clarifies it a bit.
  댓글 수: 4
ehsan rezaei
ehsan rezaei 2022년 2월 5일
The z0 was selected hypothetically. Its refer to x0 and y0. I want to know where is the problem?
Davide Masiello
Davide Masiello 2022년 2월 5일
You need the array of initial condition z0 to be Nx2 long.
You can fix this problem by modifying the definition of N in the function to
N = length(z)/2;
I also noticed something else. You have assigned
dxdt(1) = 2;
dxdt(end) = 5;
dydt(1) = 1;
dydt(end) = 3;
dxdt and dydt are values of the time derivative of x and y, not the values of x and y themselves. Are you sure they are constants?
If you need to set the value of x and y themselves, than you need to solve algebraic equations at the first and last nodes, and your system switches to a DAE. Matlab can handle this, but you'd need to change ODE solver.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

ehsan rezaei
ehsan rezaei 2022년 2월 5일
Dear Davide,
Thanks for your recommendations, the problem solved!

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by