using ode45 for trajectories

8 views (last 30 days)
Clarisa Williams
Clarisa Williams on 6 Jul 2012
I cannot get my code to work. Here is the function code:
function [zdot] = integration( t,z )
%The function integration returns the latitude, longitude, and altitude
%Remember ydot must be a column vector or you will get an error!
% declaring variables as global
global headingInRad;
global pathInRad;
global vHistInKnots;
zdot(1)=z(2);
%z(1)=x
%z(2)=xdot
%d/dt(z(1))=z(2)
%d/dt(z(2))=vHistInKnots*cos(headingInRad)*cos(pathInRad)
%This is the latitude
zdot(2)= vHistInKnots(t)*cos(headingInRad(t))*cos(pathInRad(t));
zdot(3)=z(4);
%This is the longitude
zdot(4)= vHistInKnots(t)*sin(headingInRad(t))*cos(pathInRad(t));
z(5)=z(6);
%This is the altitude
zdot(6)= vHistInKnots(t)*sin(pathInRad(t));
%zdot will display the latitude, longitude, and altitude in a column
%vector, respectively.
zdot=[zdot(1);zdot(2);zdot(3);zdot(4);zdot(5);zdot(6)]
%which always must be a column vector
end
Here is the other file:
global headingInRad;
global pathInRad;
global vHistInKnots;%declaring the necessary variables as global
%loading the file that contains the variables
load exB772InMay712
tspan=[2 754];%states the time span
zo=[0;0.710174472636493;0;4.98858733451279;0;0]%initial conditions, always a column vector
[t,z]=ode45(@integration,tspan,zo)%calls the function integration and performs and solves the ode
size(t)%gives number of time points
size(z)%(i)th column is z(i) at t(i)
The error code is:
Attempted to access headingInRad(2); index must be a positive integer or logical.
Error in integration (line 20)
zdot(2)= cos(headingInRad(t))*cos(pathInRad(t));
Error in ode45 (line 262)
f(:,2) = feval(odeFcn,t+hA(1),y+f*hB(:,1),odeArgs{:});
Error in trajectories_again (line 14)
[t,z]=ode45(@integration,tspan,zo)%calls the function integration and performs and
solves the ode
  1 Comment
Yash
Yash on 8 Jul 2012
where did u get this code from? try trouble shooting it, its working

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 8 Jul 2012
You do not show any definition for headingInRad, but your line
global headingInRad
implicitly declares headingInRad to be a variable, and initializes it to the empty array if it does not already exist.
You then attempt to access headingInRad(t) which would be intepreted as an indexing operation in most circumstances. But if t did not happen to be exactly an integer then that would fail. The t value should not be expected to be exact integers for ode45() even if the tspan happens to start with an integer, as ode45() will probably attempt to evaluate "just inside" the lower end of the range, such as at 2*(1+eps(1)).
The only circumstances under which headingInRad(t) would not be considered to be an indexing operation would be if headingInRad had been initialized to be a function handle.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!