How to code a couple ODE with nodes
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ehsan rezaei
el 4 de Feb. de 2022
Respondida: ehsan rezaei
el 5 de Feb. de 2022
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!
0 comentarios
Respuesta aceptada
Davide Masiello
el 5 de Feb. de 2022
Editada: Davide Masiello
el 5 de Feb. de 2022
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 comentarios
Davide Masiello
el 5 de Feb. de 2022
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.
Más respuestas (1)
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!