Solving ODE using discretization and Euler's method

23 vues (au cours des 30 derniers jours)
Abdulrahman Odhah
Abdulrahman Odhah le 15 Jan 2021
Hello there,
I have solved a PDE for a tank with heater using Euler's method as per the code below, I believe it is true and I got the expected results.
% mm model for the tank with heater
% Given
v_tank=2;%1 L
%v_in=1;v_out=1; 1liter / minute
v_in=0.0166667;v_out=1; %1liter / sec
p=1000;%1 watt @Steady State
T1in0=15;% 15 c degrees at t =0
%rho=1000; density kg/m^3
rho=1;%density kg/L
CS=4200;%J/kg*c
%assumption of integration step h=0.1
h=0.1;
%Steady state equation
T10=(T1in0+(p/(v_in*rho*CS))); %f(x,y) function, where dy/dx=f(x,y), x(x0)=y0.
n=1500;
T1in=T1in0;
Tvect=ones(n/h);
i=1;
for t=0:h:n
if t>=300
T1in=20;% 20 c degrees at t=5
end
if t>=900
p=1500;% 1500 watt at t=15
end
%Eurl's integral equation
T=T10+h*((v_in/v_tank)*(T1in-T10)+(p/(v_tank*rho*CS)));
T10=T;
Tvect(i)=T10;
i=i+1;
end
hold on;
t=(0.1:0.1:n);
plot(t,Tvect)
%axis([0,25,0,25])
grid on;
title('Tank with heater'); xlabel('Time [sec]'); ylabel('Temp ℃ ');
But now the second step is to solve PDE using the discretization and then Euler's method, the concept is quit clear, which is having two loops one for space discretization and the other for time (Euler's method) but I am stuck at this point. It should be two for loops (discretization loops) inside one loop (time loop). I am confused to how set the equations up inside the loops. The set of equations are attached and this is my try, it should look something like this
%time loop
for
%space loop to find ds value
for
end
%space loop to find the S value
for
end
end

Réponses (1)

Zuber Khan
Zuber Khan le 18 Sep 2024
Hi,
I understand that you want to solve PDE using the discretization and Euler's method. A general framework for Euler's method is outlined as follows.
% Euler's Method
% Initial conditions and setup
h = (enter your step size here); % step size
x = (enter the starting value of x here):h:(enter the ending value of x here); % the range of x
y = zeros(size(x)); % allocate the result y
y(1) = (enter the starting value of y here); % the initial y value
n = numel(y); % the number of y values
% The loop to solve the DE
for i=1:n-1
f = % the expression for y' in your DE%
y(i+1) = y(i) + h * f;
end
In order to arrive at the solution, you can follow the below mentioned steps.
  • Set up the spatial and temporal discretization parameters.
  • Start with the initial conditions.
  • Iterate over each time step.
  • Update the dependent variable at each spatial location using the discetized PDE.
Now, to set up the discretization loop, you can use method of finite differences. A general example is shown below.
for i = 1:N
var_new(i) = var(i) + dt * (var(i+1) - 2*var(i) + var(i-1));
end
Ensure that the boundary conditions and original variables are appropriately updated within the time loop.
You may also use the following File Exchange resource for reference.
I hope it helps.
Regards,
Zuber

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by