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

Vashist Hegde
Vashist Hegde 2020년 6월 30일
The code looks fine. The error does not seem to be for line:5
Are you sure that while running this function, you are giving t,x as arguments?
Sargondjani
Sargondjani 2020년 6월 30일
Look at the workspace when you get the error (with "dbstop if error"). Are K and x defined?
Rik
Rik 2020년 6월 30일
Did you run this function with the green button (or with the f5 hotkey)?
Reeshabh Kumar Ranjan
Reeshabh Kumar Ranjan 2020년 6월 30일
Did you save the file before running it again?
Stephen23
Stephen23 2020년 7월 1일
Rather than using global variables you should parameterize the function:
Gopika R
Gopika R 2020년 7월 1일
@Vashist Hedge: Yes, the arguments are t and x.
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일

0 개 추천

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

질문:

2020년 6월 30일

댓글:

2020년 7월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by