How to input a "sym" type equation into ODE45 to solve first order differential equation?

조회 수: 6 (최근 30일)
% Sample
close all;
clear all;
clc;
syms t tt;
a=1;
b=2;
c=3;
fs_d=50*t^2+60*t^4+70*t^9;
fd_d=diff(fs_d,t);
dTime=1e-6; % time step
Tfinal=timeduration; % time final
zeit=0:dTime:Tfinal; %
% ODE solver
% initial condition
x1_0=0;
dx1_0=0;
%ode45
options = odeset('RelTol',1.e-6);
[tt,dx] = ode45(@(tt,x) Output123(tt, x, a, b, c, fd_d, t), zeit, x1_0, options);
function dx = Output123(tt, x, a, b, c, fd_d, t)
tic;
%dx(1)=x(2);
dx=-a*subs(fd_d,t,tt)*b/(c)-x*(b/(c));
dx = dx'; % output result
toc;
end
The above the a sample code I want to achieve, it is a first order differential equation, the "subs" method works for second order differential equation.
Can anyone help me with this problem?
Thanks,

채택된 답변

Star Strider
Star Strider 2021년 9월 5일
I do not understand what you are doing.
However the correct way to use a symbolic different ia equation with the numeric solvers would be something like this —
syms a b c t tt x
fs_d=50*t^2+60*t^4+70*t^9;
fd_d=diff(fs_d,t);
f=-a*subs(fd_d,t,tt)*b/(c)-x*(b/(c))
f = 
Output123 = matlabFunction(f, 'Vars',{tt,x,a,b,c})
Output123 = function_handle with value:
@(tt,x,a,b,c)-(b.*x)./c-(a.*b.*(tt.*1.0e+2+tt.^3.*2.4e+2+tt.^8.*6.3e+2))./c
a=1;
b=2;
c=3;
timeduration = 1;
dTime=1e-6; % time step
Tfinal=timeduration; % time final
zeit=0:dTime:Tfinal; %
x1_0=0;
dx1_0=0;
options = odeset('RelTol',1.e-6);
[tt,x] = ode45(@(tt,x) Output123(tt, x, a, b, c), zeit, x1_0, options);
figure
plot(tt, x)
grid
Make appropriate changes to ger the resullt you want.
.
  댓글 수: 11
Walter Roberson
Walter Roberson 2021년 9월 5일
Then code it by hand if it is morally important for you.
Reminder: the ode*() functions strictly require that the output is double() or single(), but the output of subs() is always symbolic.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by