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

조회 수: 2 (최근 30일)
Volkan Yangin
Volkan Yangin 2020년 11월 12일
댓글: Star Strider 2020년 11월 12일
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);

채택된 답변

Star Strider
Star Strider 2020년 11월 12일
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
Volkan Yangin
Volkan Yangin 2020년 11월 12일
Thanks Star Strider. It is obviously that i should change some parts of my work according to your answer. :))
Star Strider
Star Strider 2020년 11월 12일
As always, my pleasure!
That may be necessary, however it only required a few small changes to get your code to work.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by