Not enough input arguments

4 ビュー (過去 30 日間)
Gopika R
Gopika R 2020 年 6 月 30 日
コメント済み: Walter Roberson 2020 年 7 月 1 日
function xdot = OD(t,x)
global A B C Da K
Aa=A+(B*K*C);
Ba=B*Da;
u=K*x;
xdot=(Aa*x)+(Ba*u);
end
When running this code it show error in the line u=K*x; as not enough inputs. Here A,B,C,Da,K are all 2x2 matrix.
  8 件のコメント
Gopika R
Gopika R 2020 年 7 月 1 日
@Rik: I ran it with the play button, the green one.
Walter Roberson
Walter Roberson 2020 年 7 月 1 日
When you use the green run button, then where are you expecting that MATLAB will look for the values of t and x to use inside the routine?

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 7 月 1 日
It seems that you are trying to solve a matrix differential equation. If you are using ode45, one possible way this error can occur if you call it like this
ode45(@(t,x) OD, [0 10], [1; -1; 0; 0])
The correct syntax is
ode45(@(t,x) OD(t,x), [0 10], [1; -1; 0; 0]) % or
ode45(@OD, [0 10], [1; -1; 0; 0])
The following shows working code with random values for matrices
global A B C Da K
A = rand(4);
B = rand(4);
C = rand(4);
Da = rand(4);
K = rand(4);
ode45(@(t,x) OD, [0 10], [1; -1; 0; 0])
function xdot = OD(t,x)
global A B C Da K
Aa=A+(B*K*C);
Ba=B*Da;
u=K*x;
xdot=(Aa*x)+(Ba*u);
end
Also, using global variables is not a good coding practice. Following shows how to pass these matrices to function OD without using global
A = rand(4);
B = rand(4);
C = rand(4);
Da = rand(4);
K = rand(4);
ode45(@(t,x) OD(t,x,A,B,C,Da,K), [0 10], [1; -1; 0; 0])
function xdot = OD(t,x,A,B,C,Da,K)
Aa=A+(B*K*C);
Ba=B*Da;
u=K*x;
xdot=(Aa*x)+(Ba*u);
end
  1 件のコメント
Gopika R
Gopika R 2020 年 7 月 1 日
OK. Thank you. I will try with this.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by