How can i use symbolic equations in ode45

23 visualizaciones (últimos 30 días)
Volkan Yangin
Volkan Yangin el 9 de Nov. de 2020
Comentada: Ameer Hamza el 9 de Nov. de 2020
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);

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 9 de Nov. de 2020
Editada: Ameer Hamza el 9 de Nov. de 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 comentarios
Volkan Yangin
Volkan Yangin el 9 de Nov. de 2020
Now, i am trying to adapt your approach for my problem with for loop. Thanks a lot!
Ameer Hamza
Ameer Hamza el 9 de Nov. de 2020
I am glad to be of help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by