Solving Differential Equation with initial conditions

Hello,
I tried to make a simple code for solving a coupled differential equation with initial conditions, but I got the following errors.(two separate errors) Would you check my code with the error message and make a correction?
syms S_1(t) S_2(t);
K=1;
k_1=20;
k_2=5;
k_3=5;
k_4=5;
k_5=2;
n=4;
S_1(0)=0.0;
S_2(0)=0.6;
dsolve(diff(S_1,t)==k_1*S_2^n/(K^n+S_2^n)-k_3*S_1-k_5*S_1)
dsolve(diff(S_2,t)==k_2+k_5*S_1-k_4*S_2)
Error using sym/subsasgn (line 942)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
Error in cal_test11 (line 10)
S_1(0)=0.0;

 採用された回答

Star Strider
Star Strider 2018 年 2 月 10 日

0 投票

You need to add the initial conditions as arguments to dsolve, and use the ‘double equal’ to assign them:
S_1s = dsolve(diff(S_1,t)==k_1*S_2^n/(K^n+S_2^n)-k_3*S_1-k_5*S_1, S_1(0)==0.0)
S_2s = dsolve(diff(S_2,t)==k_2+k_5*S_1-k_4*S_2, S_2(0)==0.6)

5 件のコメント

onsagerian
onsagerian 2018 年 2 月 10 日
編集済み: onsagerian 2018 年 2 月 11 日
Thank you. It works, but when I run my code with the correction, the result includes an integral form inside of it. Is it typical to have such an answer containing an integral implicitly? If not, do I need to compute the "integral" separately as part of obtaining an exact form of full answer? Or, do I need to make some modifications of my code? Please see what I have obtained.
ans =
(3*exp(-5*t))/5 + exp(-5*t)*int(exp(5*x)*(2*S_1(x) + 5), x, 0, t, 'IgnoreSpecialCases', true, 'IgnoreAnalyticConstraints', true)
Walter Roberson
Walter Roberson 2018 年 2 月 10 日
Yes, it is common for outputs to include int()
However, your functions are interrelated so you should be solving the pair of them at the same time.
Unfortunately solving both at the same time with initial conditions appears to be impractical. Solving without initial conditions is not possible in MATLAB either. Maple says
[{S_2(t) = ODESolStruc(_a, [{(diff(_b(_a), _a))*_b(_a)+(12*_a^4*_b(_a)+35*_a^5-75*_a^4+12*_b(_a)+35*_a-35)/(_a^4+1) = 0}, {_a = S_2(t), _b(_a) = diff(S_2(t), t)}, {t = Int(1/_b(_a), _a)+_C1, S_2(t) = _a}])}, {S_1(t) = (1/2)*(diff(S_2(t), t))+(5/2)*S_2(t)-5/2}]
which is a mess to understand. What it kinda of says is that for every time, there is a different pair of integrals, _a and _b that have magic relationships that can be used to construct S_2 for that time in such a way that it satisfies the needed conditions.
... In other words, you are probably going to need to do numeric integration.
Star Strider
Star Strider 2018 年 2 月 10 日
@onsagerian — My pleasure. It is reasonably easy to convert your differential equations to a form the numeric integration routines can use. See the documentation on odeToVectorField (link), odeFunction (link) and others to convert your symbolic ODE to an anonymous function suitable for the numerical integration functions.
@Walter — Thank you!
onsagerian
onsagerian 2018 年 2 月 11 日
Thank you all for your explanation. I will see the documents.
Star Strider
Star Strider 2018 年 2 月 11 日
My pleasure.
Those links completely describe the code needed to convert your symbolic ODEs to anonymous functions.
Do not include the initial conditions ‘S_1(0)’ and ‘S_2(0)’ in the code you send to odeToVectorField. You will set the initial conditions as arguments to the ODE solver you choose.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by