How can i use symbolic equations in ode45
35 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Ameer Hamza
on 9 Nov 2020
Edited: Ameer Hamza
on 9 Nov 2020
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 Comments
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!