How can i use symbolic equations in ode45

조회 수: 26 (최근 30일)
Volkan Yangin
Volkan Yangin 2020년 11월 9일
댓글: Ameer Hamza 2020년 11월 9일
In my code, i tried to convert my symbolic function to numeric to solve it via ode45, but took this error message:
Error using odearguments (line 113)
Inputs must be floats, namely single or double.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Untitled8 (line 8)
[t,x1_dot_sol] = ode45(x1_dot_num,tspan,x1_0);
Is there any way to fix it?
Thx,
clear all
clc
syms x1
x1_dot=(6*atan((53*x1)/9))/(25*pi) - 40*x1 + (6*atan((179*x1)/18))/(25*pi), (6*atan((53*x1)/9))/(25*pi) - 40*x1 + (6*atan((179*x1)/18))/(25*pi), (6*atan((53*x1)/9))/(25*pi) - 40*x1 + (6*atan((179*x1)/18))/(25*pi)
x1_dot_num = @(t,x1) matlabFunction(x1_dot);
x1_0 = 4;
tspan = [0 3];
[t,x1_dot_sol] = ode45(x1_dot_num,tspan,x1_0);

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 9일
편집: Ameer Hamza 2020년 11월 9일
This is the correct way to write this code
% clear all
clc
syms x1
x1_dot = (6*atan((53*x1)/9))/(25*pi) - 40*x1 + (6*atan((179*x1)/18))/(25*pi)
x1_dot_num = matlabFunction(x1_dot, 'Vars', {'t', 'x1'});
x1_0 = 4;
tspan = [0 3];
[t,x1_dot_sol] = ode45(x1_dot_num,tspan,x1_0);
plot(t, x1_dot_sol)
Also, for your ODE, you don't need symbolic toolbox, Following code is also correct
clc
x1_dot_num = @(t, x1) (6*atan((53*x1)/9))/(25*pi) - 40*x1 + (6*atan((179*x1)/18))/(25*pi);
x1_0 = 4;
tspan = [0 3];
[t,x1_dot_sol] = ode45(x1_dot_num,tspan,x1_0);
plot(t, x1_dot_sol)
  댓글 수: 2
Volkan Yangin
Volkan Yangin 2020년 11월 9일
Now, i am trying to adapt your approach for my problem with for loop. Thanks a lot!
Ameer Hamza
Ameer Hamza 2020년 11월 9일
I am glad to be of help!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by