How to solve 2nd ODE equation in numerical and analytical method at same plot graph?

Hello,
I am was looking for help to solve this equation
a. dx/dt = -x
b. dx/dt = -x+1
in Matlab, can I get help to solve this equation of plot the numerical solutions and analytical solutions in the same graph and compare them.
with ode45
can anyone help me with this code?

6 件のコメント

Star Strider
Star Strider 2022 年 9 月 28 日
移動済み: Star Strider 2022 年 9 月 28 日
See my Answer in How to solve 2nd ODE equation in numerical and analytical method at same plot graph? and make the appropriate changes for each function.
Shreyas Sangamesh
Shreyas Sangamesh 2022 年 9 月 28 日
移動済み: Star Strider 2022 年 9 月 28 日
I was looking for these kind of equation as well, by the way 2nd ODE solution was very helpful.
can you help me in these kind of equation as well?
majorly for the dsolve part.
Star Strider
Star Strider 2022 年 9 月 28 日
移動済み: Star Strider 2022 年 9 月 28 日
Just substitute each of those equations into ‘DEqn’ in my previous code, and go from there with each one separately. Make appropriate changes in the initial conditions and time limits as necessary (since I guessed at those).
John D'Errico
John D'Errico 2022 年 9 月 28 日
PLease stop asking the same question every 30 minutes. You asked this question before, and got an answer.
Shreyas Sangamesh
Shreyas Sangamesh 2022 年 9 月 28 日
Its not the same one I asked before,
I just wanted to have some assistance on how these equation behave in 1st order ODE,
I have got the revelant idea on how to carry it out!!
Thank you
Torsten
Torsten 2022 年 9 月 28 日
編集済み: Torsten 2022 年 9 月 28 日
a .
dx/dt = - x -> dx/x = -dt -> log(x/x0) = (t0-t) -> x = x0 * exp(t0-t)
b.
dx/dt = -x+1 -> dx/(x-1) = -dt -> log((x-1)/(x0-1)) = t0-t -> x-1 = (x0-1)*exp(t0-t) -> x = 1 + (x0-1)*exp(t0-t)

サインインしてコメントする。

回答 (1 件)

Sai
Sai 2022 年 10 月 12 日
I understand that you are trying to solve the differential equations both numerically and analytically and get the plots on same graph for comparison.
You can use available MATLAB functions “ode45” for numerical approach and “dsolve” for analytical approach.
Please refer to the attached code snippets, which can help you solve the problem.
NOTE: Since the initial conditions are not given, they are assumed
a). dx/dt = -x
%Numerical Method
[t,x] = ode45(@(t,x) -x,[0 20],1)
plot(t,x)
hold on
%Analytical Method
syms x(t)
dx = diff(x)
eqn = dx==-x
x(t) = dsolve(eqn,x(0)==1)
t = 0:20
plot(t,x(t))
hold off
legend("Numerical","Analytical")
b). dx/dt = -x+1
%Numerical Method
[t,x] = ode45(@(t,x) -x+1,[0 20],0)
plot(t,x)
hold on
%Analytical Method
syms x(t)
dx = diff(x)
eqn = dx==-x+1
x(t) = dsolve(eqn,x(0)==0)
t = 0:20
plot(t,x(t))
hold off
legend("Numerical","Analytical")
You can also refer to the below links regarding “ode45” and “dsolve” for future references.

カテゴリ

質問済み:

2022 年 9 月 28 日

回答済み:

Sai
2022 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by