Louis Marr
Louis Marr

Reputation: 43

Return vector from ODE45 has shorter length than initial conditions vector, while trying to solve coupled diffEQ

I am trying to use ODE45 to find the solution to 2 rotating bars, rotating on vertical plane, that have a torsional spring that creates a moment on the bars only when the angle between them differs from 90 degrees. I am just using a1-b4 as the constants in the diffEQ and are just imputing their values into a matrix before sending it into the function. I keep betting back an error saying that I am sending 6 initial conditions, but only get 5 back from the ODE45 function. Any ideas on how to fix this?

%system1.m
function [dx] = system1(t,x,parameters)
dx = zeros(4,1);

a1 = parameters(1);
a2 = parameters(2);
a3 = parameters(3);
a4 = parameters(4);
b1 = parameters(5);
b2 = parameters(6);
b3 = parameters(7);
b4 = parameters(8);

dx(1) = x(2); %dtheta1 = angular velocity1
dx(2) = x(3); %d(angular velocity1) = angular acceleration1
dx(4) = x(5); %dtheta2 = angular velocity2
dx(5) = x(6); %d(angular velocity2) = angular acceleration2

dx(2) = a1*x(1)+a2*x(4)+a3*x(2)+a4*x(5); %motion equation 1
dx(5) = b1*x(1)+b2*x(4)+b3*x(2)+b4*x(5); %motion equation 2

%CA2Lou.m
%set parameters
clear;

a1 = -12;
a2 = 12;
a3 = 0;
a4 = 0;
b1 = 4;
b2 = -4;
b3 = 0;
b4 = 0;

parameters = [a1 a2 a3 a4 b1 b2 b3 b4];

%set final time
tf = .5;

options = odeset('MaxStep',.05);

%setting initial conditions
InitialConditions = [90 0 0 0 0 0];

[t_sol,x_sol] = ode45(@system1,[0 tf],InitialConditions,[],parameters);

Upvotes: 2

Views: 730

Answers (1)

gnovice
gnovice

Reputation: 125874

Your size and indexing for dx don't match x. You initialize dx to 4 elements, even though x has 6. Then you assign values to 4 indices of dx (specifically, [1 2 4 5]) which results in a new size for dx of 5 elements, still one less than the 6 it expects.

You probably need to initialize dx like so:

dx = zeros(6, 1);

Then, your first and second motion equations should probably (I'm guessing) be placed in indices 3 and 6:

dx(3) = a1*x(1)+a2*x(4)+a3*x(2)+a4*x(5); %motion equation 1
dx(6) = b1*x(1)+b2*x(4)+b3*x(2)+b4*x(5); %motion equation 2

Upvotes: 1

Related Questions