Errors using ode45

1 view (last 30 days)
Vishank Battar
Vishank Battar on 25 Nov 2020
Commented: Stephan on 25 Nov 2020
I'm getting the following errors when trying to use ode45, and I'm not sure how to fix them.
function:
function xdot = eom(t, x, Ix, Iy, Iz)
%define constants
%Assume no external moments
Mx = 0;
My = 0;
Mz = 0;
xdot(1) = (Mx./Ix) - (Iz-Iy)*x(2)*x(3)/Ix;
xdot(2) = (My./Iy) - (Ix-Iz)*x(1)*x(3)/Iy;
xdot(3) = (Mz./Iz) - (Iy-Ix)*x(1)*x(2)/Iz;
xdot = xdot';
return
ode:
%Define PMOIs
Ix = 2887; %kg-m^2
Iy = 2887; %kg-m^2
Iz = 5106; %km-m^2
%Define initial conditions
x1_0 = 0.6; % OmegaX rad/s
x2_0 = 0; % OmegaY rad/s
x3_0 = 1.1; % OmegaZ rad/s
IC = [x1_0; x2_0; x3_0]; %initial conditions matrix
%define timestep
t0 = 0; %initial time
tf = 10;
interval = [t0 tf]; %timestep
%Numerically solve ODE
[t,x] = ode45('eom', interval, IC,Ix,Iy,Iz);
omegaX = x(:,1);
omegaY = x(:,2);
omegaZ = x(:,3);
Errors:
Error using /
Matrix dimensions must agree.
Error in eom (line 8)
xdot(1) = (Mx./Ix) - (Iz-Iy)*x(2)*x(3)/Ix;
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 HW11 (line 18)
[t,x] = ode45('eom', interval, IC,Ix,Iy,Iz);
I can't figure out what the errors mean or how to get rid of them. If somebody could please help me properly use ode45 to find the solution to this function that would be great!

Accepted Answer

Stephan
Stephan on 25 Nov 2020
%Define PMOIs
Ix = 2887; %kg-m^2
Iy = 2887; %kg-m^2
Iz = 5106; %km-m^2
%Define initial conditions
x1_0 = 0.6; % OmegaX rad/s
x2_0 = 0; % OmegaY rad/s
x3_0 = 1.1; % OmegaZ rad/s
IC = [x1_0; x2_0; x3_0]; %initial conditions matrix
%define timestep
t0 = 0; %initial time
tf = 10;
interval = [t0 tf]; %timestep
%Numerically solve ODE
[t,x] = ode45(@(t,x)eom(t, x, Ix, Iy, Iz), interval,IC);
omegaX = x(:,1);
omegaY = x(:,2);
omegaZ = x(:,3);
plot(t,x)
function xdot = eom(~, x, Ix, Iy, Iz)
%define constants
%Assume no external moments
Mx = 0;
My = 0;
Mz = 0;
xdot(1) = (Mx./Ix) - (Iz-Iy)*x(2)*x(3)/Ix;
xdot(2) = (My./Iy) - (Ix-Iz)*x(1)*x(3)/Iy;
xdot(3) = (Mz./Iz) - (Iy-Ix)*x(1)*x(2)/Iz;
xdot = xdot';
end
  4 Comments
Walter Roberson
Walter Roberson on 25 Nov 2020
@(t,x) eom(t, x, Ix, Iy, Iz)
means that you are creating an anonymous function that will accept two positional parameter. At the time the anonymous function is called, it will accept the two parameters and will call the function eom. Whatever was passed in as the first parameter to the anonymous function will be passed as the first parameter to eom -- which is indicated by matching the name of the first parameter to the use of that same variable. Likewise, the second parameter passed to the anonymous function will be passed as the second parameter to eom, indicated by matching the name of the dummy second parameter to that same variable name in the body of the code.
At the time the anonymous function is built, the then-current values of Ix, Iy, and Iz will be recorded and will be copied into the datastructure representing the anonymous function. When the anonymous function is run, the values will be pulled out of the data structure and passed in to appropriate positions to the function eom.
Stephan
Stephan on 25 Nov 2020
Thank you Walter.
off topic BTW today I reported 14 or 15 accounts to the TMW contact you provided. Hope this helps to get rid of this problem.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!