How to solve this differential equation using matlab?

12 views (last 30 days)
Anup
Anup on 16 Nov 2022
Answered: Simran on 10 Feb 2025 at 17:41
I know the value of n_(j-1), T_(j-1) and n_(j).
I want to solve this differential equation and get the numerical solution.
How can I do it in MATLAB?
  2 Comments
Torsten
Torsten on 16 Nov 2022
What do you mean by
I know the value of ndot_(j-1), T_(j-1) and ndot_(j)
?
Do you have time-dependent analytic functions for them ? Or only time-dependent vectors ? Or something else ?
Anup
Anup on 16 Nov 2022
I think I am having some problems here, I am not able to explain to you how i know those values.
I know the temperature T until a point where I have to use the differential equation. Let's say that is T(j-1).
Let's consider 'n' terms some constants.
How would you solve this?
If you haven't understand what I am saying, ignore all, just suggest me how would you approach the solution? Just your way if you are given equation only

Sign in to comment.

Answers (1)

Simran
Simran on 10 Feb 2025 at 17:41
Hi @Anup,
As I understand, you want to solve the following differential equation:
[ \frac{\partial T_j}{\partial t} = \dot{n}{j-1} T{j-1} - \dot{n}_j T_j ] using MATLAB. What you provided is a first-order differential equation for T_j.
As you mentioned, the values for - ( \dot{n}{j-1} ), ( T{j-1} ), and ( \dot{n}_j ) are known. All we need is an initial condition for ( T_j) at ( t = 0 ) . Lets say (T_j(0) = T_{j0} ) .
Next, create a function in MATLAB to represent the ODE (first-order differential equation) and name it odefun.m. Below is odefun.m:
function dTdt = odefun(t, Tj, nj_minus_1, Tj_minus_1, nj)
dTdt = nj_minus_1 * Tj_minus_1 - nj * Tj;
End
We can solve this ODE using the inbuilt MATLAB function ode45. You can refer to this link to understand about it more:
Below is the script to solve it:
% first define the known parameters
nj_minus_1 = 1.0; % example value, you can use your own value
Tj_minus_1 = 100; % example value
nj = 0.5; % example value
% Set the initial condition by defining T_{j0}.
Tj0 = 50; % example initial value
% Define time span for the solution
tspan = [0 10];
% Use MATLAB’S ODE solver (ode45) to solve ODE (first order differential equation)
[t, Tj] = ode45(@(t, Tj) odefun(t, Tj, nj_minus_1, Tj_minus_1, nj), tspan, Tj0);
% Lastly plot the solution
plot(t, Tj);
xlabel('Time');
ylabel('T_j');
title('Solution of the Differential Equation');
The graph of the ODE should look something like this:
The solution can be tested with some different parameter values to see if it behaves as expected.
Below are the links attached that maybe relevant to the discussion:

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!