Creating the nonlinear system with odeFunction

3 ビュー (過去 30 日間)
Ivan Khomich
Ivan Khomich 2020 年 8 月 1 日
回答済み: Ivan Khomich 2020 年 8 月 2 日
Hello everyone!
I'm trying to write something what would create the nonlinear system of differential equations automatically from linear system matrix.
I have some succes with it, but my solutions requires a lot of time when I "invent" this novelties in my main programm. The best result I get was 50 sec, while handwritten system requires less than 1.5 sec.
I was recommended to use the odeFunction(), in case of speed, but I meet some problems that I don't understand nor with documentation, nor while reading the other answers. There are them:
1)
syms xs(t) [1 3];
B=[0;0;1];
xs(t)
f = [xs2(t)^2;
-xs2(t)+xs3(t);
-2*xs3(t)];
f=f+B;
vars=xs;
initConditions=[-1 0 0];
odefun = odeFunction(f,vars);
ode45(odefun, [0 2], initConditions)
This works okay, but there the system is handwritten. So, I write something like this
syms xs(t) [1 SysOrder]
iN=1;
jN=2;
Nonlinearity=@(x) x^2;
A=[0 1 0; ...
0 -1 1;...
0 0 -2];
LinearSystemHomogen=A*xs(t).';
LinearSystemHomogen(iN)=subs(LinearSystemHomogen(iN), xs(jN),Nonlinearity(xs(jN)))
And I do not know how to correctly adress to the needed element of vector xs(t).
If anyone can suggest something good to approach this problem, I'll appreciate this.
Thank you in advance!
P.S. 2) Just a funny question: how to turn off the plot output when calling an ode45 with odeFunction()?

回答 (1 件)

Ivan Khomich
Ivan Khomich 2020 年 8 月 2 日
While waiting for answer, I've tried to find the solutions myself, and I think, get something.
Nonlin=@(x) x^2;
SysOrder=3
iN=1;
jN=2;
syms xs(t) [1 SysOrder]
A=[0 1 0; 0 -1 1;0 0 -2];
B=[0 ;0;1];
LinearSystem=A*xs(t).';
SubsVarStr=strcat('xs', num2str(jN),'(t)');
LinearSystem(iN)=subs(LinearSystem(iN), str2sym(SubsVarStr), Nonlin(str2sym(SubsVarStr)));
inc=[-1 0 0];
odefun=odeFunction(LinearSystem+B, xs);
[T,X]=ode45(odefun, [0 2], inc)
In my main programm this 'method' gives a calculation time about 2 seconds compared to the original 1.5 seconds(when the system is handwritten and there is no any symbolic computations) and the 50 seconds( when the system is created symbolically and then transfered to the solver), so for me it is a good result.
Maybe anyone will need this somewhen.

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by