Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Why is this function returning a vector of length 1?

Subject: Why is this function returning a vector of length 1?

From: Bravus

Date: 15 Jan, 2013 22:09:17

Message: 1 of 7

This is a function m-file:

function dx = MathieuDamped(x,p,a,b,q);
dx = [p(2);b*p(2)+(((2*q)*cos(2*x))-a)*p(1)]

a, b and q are constants that are fed in.

The function is called using ode45 as follows:

[x,p]=ode45('MathieuDamped',[0 1000],[1,0],a,b,q);

I'm getting the error message:

Error using odearguments (line 93)
MATHIEUDAMPED returns a vector of length 1, but the length of initial conditions
vector is 2. The vector returned by MATHIEUDAMPED and the initial conditions
vector must have the same number of elements.

And sure enough, when I do 'length(dx)' I get an answer of 1.

With the semicolon in the initial function expression (second line) though, I'm not sure why the function is not returning a vector of length 2, with the first line being p(2) and the second being b*p(2)+(((2*q)*cos(2*x))-a)*p(1).

The value of b I'm working with initially (I want to change it later, which is why the term is included) is 0, if that's relevant.

Thanks for any help you can offer to get me going again!

Subject: Why is this function returning a vector of length 1?

From: Bravus

Date: 15 Jan, 2013 22:21:07

Message: 2 of 7

Additional information: the function is called by this script:

b=0;
num=0;
for a=1:200 % Indices need to be positive integers, so...
    for q=1:200 % adjusted values in 'MathieuDamped'

        [x,p]=ode45('MathieuDamped',[0 1000],[1,0],a,b,q);
        num=num+1;
        
        if p(num,2) >= 100
            stab(a,q)=0;
        elseif p(num,2) < 100
            stab(a,q)=1;
        end
        num=0;
    end
end

I've tried echoing a, b and q to the command window, and am getting

a = ' '
b = 0
q = 1

so it's possible the problem is in how the value of a is being handed to the function. That's breaking the second line of the dx vector... maybe.

Subject: Why is this function returning a vector of length 1?

From: Bravus

Date: 15 Jan, 2013 23:13:18

Message: 3 of 7

More additional information: it's definitely a that's broken: deleting it in the function makes everything run happily.

That probably wraps up the question to some extent, at least in terms of vector length.

The question of why q is passing through fine and a is breaking is still vexing me. Making it a global variable doesn't work (and is probably inelegant anyway).

Subject: Why is this function returning a vector of length 1?

From: Bravus

Date: 16 Jan, 2013 04:14:12

Message: 4 of 7

Talking to myself is fun! Actually, I'm kinda relieved no-one else has wasted their time replying, since it's made me think and talk through things with myself and my supervisor and solve a bunch of silly little problems myself.

The main one still applies, though. Not sure whether to start a new thread or continue in this one with the history, but I'll do the latter. I think the title still mostly works.

One of the things my supervisor suggested as an explanation for why 'b' and 'q' were passing through fine but not 'a' was that 'a' might be a reserved variable name. Pretty sure it's not, but he also suggested moving to more self-explanatory variable names, which should get rid of that possibility and also be better coding practice.

So, there are my two m-files, the first a script and the second the function it calls:

******************

% Uses ode45 to solve undamped and damped Matheiu Equation
%
% David Geelan, 2013
%
% Calls function 'MathieuDamped' for x=0-100
% with initial conditions p(1)=1, p(2)=0
%
clear;
dampingb=0;
for trapa=1:20 % Indices need to be positive integers, so...
    for trapq=1:20 % adjusted values in 'MathieuDamped'
        [timex,odep]=ode45('MathieuDamped',[0 100],[1,0],trapa,dampingb,trapq);
        if odep(end,2) >= 100
            stab(trapa,trapq)=0;
        elseif odep(end,2) < 100
            stab(trapa,trapq)=1;
        end
    end
end

******************

function dx = MathieuDamped(timex,odep,trapa,dampingb,trapq);
trapa=(trapa-10)/10;
trapq=trapq/10;
trapa, trapq, timex, dampingb
dx = [odep(2);dampingb*odep(2)+(((2*trapq)*cos(2*timex)))*odep(1)];
%
% The main script needs to scan 'a' from -1 to 1 in steps of 0.1
% but the indices need to be positive integers so I've scanned it
% from 1 to 20 and then gone (a-10)/10 here. Similarly for q
% but it's just division by 10

***********************

We're modelliing a 'dust trap' and 'a' and 'q' were parameters of the trap, hence 'trapa' and 'trapq'. 'b' was the damping effect, so 'dampingb'. 'x' related to the time the trap would run, so 'timex', and the solving is for a system of ODEs, p(1) and p(2), so 'odep'. In a latter iteration I'll probably add some underscores...

In the calling line, the [0 100] is the value of the 'timex' variable, and the [1,0] is the initial conditions vector for p(1),p(2).

Running the script as is, echoing the variables to the command window, yields iterations of:

trapa =

     []


trapq =

    0.5000


timex =

   55.4590


dampingb =

     0

in which dampingb stays at 0 as expected, trapa has the empty square brackets and the other two variables work normally.

Switching the order so dampingb appears before trapa leads to dampingb getting the [ ], so it's obviously something to do with what is located immediately after the ODE in the list of variables.

Kind of curly and complicated, but hopefully someone knows what's going on and can solve it.

Subject: Why is this function returning a vector of length 1?

From: Roger Stafford

Date: 16 Jan, 2013 04:44:10

Message: 5 of 7

"Bravus " <bravus@gmail.com> wrote in message <kd4k2d$nsl$1@newscl01ah.mathworks.com>...
> .......
> [x,p]=ode45('MathieuDamped',[0 1000],[1,0],a,b,q);
> ......
- - - - - - - - - -
  The fourth input argument to 'ode45' is their 'options' input that can be provided in conjunction with the 'odeset' function. Perhaps there is some error in the way you have set things up in this respect. The documentation states: "Options: Structure of optional parameters that change the default integration properties. This is the fourth input argument" and "You can create options using the odeset function. See odeset for details."

Roger Stafford

Subject: Why is this function returning a vector of length 1?

From: Bravus

Date: 16 Jan, 2013 04:54:13

Message: 6 of 7

"Roger Stafford" wrote in message <kd5b6q$att$1@newscl01ah.mathworks.com>...
> "Bravus " <bravus@gmail.com> wrote in message <kd4k2d$nsl$1@newscl01ah.mathworks.com>...
> > .......
> > [x,p]=ode45('MathieuDamped',[0 1000],[1,0],a,b,q);
> > ......
> - - - - - - - - - -
> The fourth input argument to 'ode45' is their 'options' input that can be provided in conjunction with the 'odeset' function. Perhaps there is some error in the way you have set things up in this respect. The documentation states: "Options: Structure of optional parameters that change the default integration properties. This is the fourth input argument" and "You can create options using the odeset function. See odeset for details."
>
> Roger Stafford

Thanks, Roger. Yes, one of the other things my supervisor suggested was that there might be a problem defining the other variables within the ode45 statement as I did, and as you say, reading up further on ode45 and odeset shows that it's looking for parameters for numerically solving the ODE in this position, not other variables relating to a different function.

I'll restructure the approach, and report back here with the revised version and (hopefully) reports of success, in the hope of helping someone else in future.

Subject: Why is this function returning a vector of length 1?

From: Phil Goddard

Date: 16 Jan, 2013 05:39:14

Message: 7 of 7


Assuming you want to use the default options then just specify it as an empty matrix,
>> [x,p]=ode45('MathieuDamped',[0 1000],[1,0],[],a,b,q);

Also, I'd highly recommend you read up on function handles and why the cost function should be specified using them.
At a minimum you want
>> [x,p]=ode45(@MathieuDamped,[0 1000],[1,0],[],a,b,q);

Phil.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us