How can I integrate state-space equations in 6DOF with ode45?

7 views (last 30 days)
I have a state-space representation of a system in six degrees of freedom, and have used the forward Euler method to update the state, as seen in the attached script. However, this gave an inaccurate result, so I want to change to the Runge-Kutta method. I have tried to use the ode45 function, but am unsure about the syntax.
Here is my attempt (with the rest of the code unchanged):
[t,x]=ode45(@(t,x) f(t, x, N, u, A, B),t,x0);
for k=1:N-1 % need -1 here so that x(n+1) does not lead to an index>N
for i = 1:1:12
y(i,k+1)=C(i,:)*x(:,k+1)+D(i,:)*u(:,k+1); % y is an output based on x and u, so is not integrated
end
% Calculate new version of A
A=calcA(x(:,k+1));
end
function [t, x]=f(t, x, N, u, A, B)
for k=1:N-1
for i = 1:1:12
x(i,k+1)=A(i,:)*x(:,k)+B(i,:)*u(:,k);
end
end
end
I get the following errors:
Error using odearguments (line 95) @(T,X)F(T,N,X,U,A,B) returns a vector of length 1, but the length of initial conditions vector is 12. The vector returned by @(T,X)F(T,N,X,U,A,B) and the initial conditions vector must have the same number of elements.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in newsys4 (line 63) [t,x]=ode45(@(t,x) f(t, x, N, u, A, B),t,x0);
I think the correct length is 12, since there are 12 states. How can I make @(T,X)F(T,N,X,U,A,B) return a vector of length 12?
  2 Comments
Jan
Jan on 6 Feb 2017
The function, which is integrated by ODE45 replies one output only: the derivative as vector. dx = f(t, x, ...) . It is not clear to me, which operations you apply and why a matrix is returned.
EllaM
EllaM on 6 Feb 2017
The ode45 function should return a vector with 12 elements (since there are 12 states), but apparently it returns a vector with only 1 element, and I'm not sure why.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 6 Feb 2017
Your derivative function f should only be returning the derivative of x, not t also. I.e., the signature of f should be something like this:
function dx = f(t, x, N, u, A, B)
dx = _______; % Add code here to calculate the derivative of x at time t
end
The way you have it now, ode45 is simply getting as a result of the f call the first returned argument, t, which is a scalar (1 element). Hence the error.
  5 Comments
Jan
Jan on 7 Feb 2017
Edited: Jan on 7 Feb 2017
xdot = ode45(@(t,x) f(t, x, N, u, A, B), t, x0)
not @(x,u). And xdot must be a vector, if you reply it to ode45. It must have the same dimensions as the inital value x0, because it is the derivative ate the time t and position x.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!