Error in ODE45 function
1 view (last 30 days)
Show older comments
When I type up this code, I keep getting that there is an undefined variable, however, the variable is defined and I do not know what is happing. Here is the erro I am recieving:
Undefined function or variable 'dt'.
Error in Homework3_solutioncode2018>pose_dot (line 68)
ind = floor(t/dt) + 1;
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1}
to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan,
y0, options, varargin);
Error in Homework3_solutioncode2018 (line 30)
[t_out,Xi] = ode45(@pose_dot,[T_init T_final],dt,Xi_0);
Here is my overall code:
clc;
clear all;
close all;
VarInput = load('Hw3_quadData.mat');
qDat = VarInput.quadrotor;
[n,~] = size(qDat); % n is the number of timesteps
% Extract the initial condition, and body rate info, and time info
Xi_0 = [qDat(1,2:4)'; qDat(1,8:10)'];
v = qDat(:,5:7);
omega = qDat(:,11:13);
t = qDat(:,1);
dt = qDat(2,1) - qDat(1,1);
T_init = qDat(1,1);
T_final = qDat(end,1);
% Call pose_dot once to initialize persistant variables
pose_dot(1,Xi_0,{dt,v,omega});
% Call ODE 45 to integrate for Xi = [x,y,z,phi,theta,psi]
[t_out,Xi] = ode45(@pose_dot,[T_init T_final],Xi_0);
% Plot solutions and truth values
% Euler Angles
figure(1)
plot(t_out,Xi(:,4:6)*(180/pi),':');
hold on
plot(t,qDat(:,8:10)*(180/pi),'--');
legend('\phi_{calc}','\theta_{calc}','\psi_{calc}','\phi_{true}','\theta_{true}','\psi_{true}');
grid on
title('Euler Angles vs. Time');
ylabel('Euler Angles (Degrees)');
xlabel('Time (Seconds)');
% Position
figure(2)
plot(t_out,Xi(:,1:3),':');
hold on
plot(t,qDat(:,2:4),'--')
legend('x_{calc}','y_{calc}','z_{calc}','x_{true}','y_{true}','z_{true}');
grid on
title('CoM Position vs Time');
ylabel('Position (meters)');
xlabel('Time (seconds)');
function Xi_dot1 = pose_dot(t,Xi,prstVars)
% Declare persistant variables that will remain between function calls
% persistant dt v omega
if nargin > 2
dt = prstVars{1};
v = prstVars{2};
omega = prstVars{3};
end
% Find values the aircraft velocity in the body frame and the body angular
% rates for time t.
ind = floor(t/dt) + 1;
nu = [v(ind,:)'; omega(ind,:)'];
% Find trig values for neater presentation of kinematic equations below:
cPsi = cos(Xi(6));
sPsi = sin(Xi(6));
cPhi = cos(Xi(4));
sPhi = sin(Xi(4));
cTh = cos(Xi(5));
sTh = sin(Xi(5));
tTh = tan(Xi(5));
Xi_dot1 = [cPsi*cTh cPsi*sTh*sPhi-sPsi*cPhi sPsi*sPhi+cPsi*sTh*cPhi 0 0 0;...
sPsi*cTh cPsi*cPhi+sPsi*sTh*sPhi sPsi*sTh*cPhi-cPsi*sPhi 0 0 0;...
-sTh cTh*sPhi cTh*cPhi 0 0 0;...
0 0 0 1 sPhi*tTh cPhi*tTh;...
0 0 0 0 cPhi -sPhi;...
0 0 0 0 sPhi/cTh cPhi/cTh]*nu;
end
quadData is a 1759x13 array
0 Comments
Answers (1)
Walter Roberson
on 7 Oct 2019
You have
% persistant dt v omega
where the statement is commented out. You need to uncomment and fix the spelling:
persistent dt v omega
However, we would recommend that you not do this trick of initializing variables by calling once with extra arguments. Instead see http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!