How can i use symbolic equations in ode45
조회 수: 6 (최근 30일)
이전 댓글 표시
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);
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!