Using ode45 on a symbolic to numeric function

조회 수: 2 (최근 30일)
Luke G.
Luke G. 2020년 11월 5일
댓글: Star Strider 2020년 11월 11일
Consider the code below. Both sections achieve the same result--an identical graph shown below. Does anyone have any warnings about starting in symbolic and converting to numeric? For my application (more complex than the example below), I need to divide two symbolic expressions and then plug the resulting expression into ode45 to be solved. Computation time doesn't matter (i.e., the problem solves in < 4s). Any warnings/advice would be greatly appreciated!
%% Numeric
ydot = @(t,y) 2*t;
[t_sol2,y_sol2] = ode45(ydot,[0 5],0);
figure
plot(t_sol2,y_sol2,'-o')
xlabel('Time'); ylabel('y(t)')
%% Symbolic -> Numeric
syms y t
ydot2(t,y) = 2*t;
ode = matlabFunction(ydot2);
[t_sol,y_sol] = ode45(ode,[0 5],0);
figure
plot(t_sol,y_sol,'-o')
xlabel('Time'); ylabel('y(t)')
Plot of the result:

채택된 답변

Star Strider
Star Strider 2020년 11월 5일
The situations in which I begin with symbolic expressions for differential equations are generally higher-order and nonlinear. It is always possible to do this manually, however the symbolic approach prevents algebra errors (and the accompanying frustration of dealing with them). I then use odeToVectorField and then matlabFunction to convert the result to an anonymous function. There are a number of other Symbolic Math Toolbox functions listed in Equation Solving that can make this much easier.
  댓글 수: 2
Luke G.
Luke G. 2020년 11월 11일
Thank you Star Strider! I appreciate the insight.
Star Strider
Star Strider 2020년 11월 11일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by