How can i fix this ode45 error: Not enough input arguments.

2 visualizaciones (últimos 30 días)
Hi,
I have two odes and use ode45 to solve them simultaneously. At first ode, there are two state variables: x1 and x2, but second ode has only x1. So, two initial conditions should be used for first. Additionaly, we can use one initial condition for second ode.
I have created my codes according to these rules, but i take an error from MATLAB as you can see at the title.
Is there any way to remove this error?
Thanks a lot!
clear all
clc
syms x1 x2
x1_dot = (6*atan((53*x1)/9))/(25*pi) - 40*x1 + (6*atan((179*x2)/18))/(25*pi)
x2_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','x2'});
x2_dot_num=matlabFunction(x2_dot, 'Vars', {'t', 'x1','x2'});
x1_0 = 4;
x2_0 = 5
tspan = [0 3];
[t,x1_dot_sol] = ode45(x1_dot_num,tspan,[x1_0 x2_0]);
[t,x2_dot_sol] = ode45(x2_dot_num,tspan,x1_0);

Respuesta aceptada

Star Strider
Star Strider el 12 de Nov. de 2020
Try this:
syms x1 x2 t
x1_dot = (6*atan((53*x1)/9))/(25*pi) - 40*x1 + (6*atan((179*x2)/18))/(25*pi);
x2_dot = (6*atan((53*x1)/9))/(25*pi) - 40*x1 + (6*atan((179*x1)/18))/(25*pi);
x_dot_num = matlabFunction([x1_dot; x2_dot], 'Vars', {t, [x1 x2]});
% x2_dot_num=matlabFunction(x2_dot, 'Vars', {'t', 'x1','x2'});
x1_0 = 4;
x2_0 = 5;
ic = [x1_0 x2_0];
tspan = [0 3];
[t,x_dot_sol] = ode45(@(t,in2)x_dot_num(t,in2.'),tspan,[x1_0 x2_0]);
% [t,x2_dot_sol] = ode45(x2_dot_num,tspan,x1_0);
figure
plot(t, x_dot_sol)
grid
The ‘[x1 x2]’ vectoir in the 'Vars' argument creates them as a parameter set, however it defines them as a row vector. Since the MATLAB ODE solvers want them as a column vector, this call to ‘x_dot_num’:
@(t,in2)x_dot_num(t,in2.')
(note the transpose of ‘in2’) in the ode45 call corrects for that.
This code runs without error. (I left the commented-out lines in so you can understand the changes.)
  3 comentarios
Volkan Yangin
Volkan Yangin el 12 de Nov. de 2020
Thanks Star Strider. It is obviously that i should change some parts of my work according to your answer. :))
Star Strider
Star Strider el 12 de Nov. de 2020
As always, my pleasure!
That may be necessary, however it only required a few small changes to get your code to work.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations 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