Date: Feb 16, 2013 11:03 PM
Author: Phil Goddard
Subject: Re: Error in (embedded) MATLAB function
It's a bit unrealistic to ask for comments on the format of code that doesn't execute as it will obviously have to change for it to actually run.
In general you don't need to check whether all of your persistent variables are empty -- if one is empty they all will be, so
> if isempty(m)
> m = 0;
> persistent x1 % x1 is first state variable
> if isempty(x1)
> x1 = 0;
> persistent x2 % x2 is second state variable
> if isempty(x2)
> x2 = 0;
m = 0;
x1 = 0;
x2 = 0;
But in your latest version x1 and x2 aren't defined as being persistent so this will error out saying they are undefined.
Also in your latest version you are (trying) to pass a string input to the 'test' function, but 'test' hasn't been written to accept any inputs.
If you do (in your final version which you haven't told us anything about) want/need to call an external function then you have used extrinsic definition correctly.
For what you have shown, I'd go back to your original version and have some persistent variables -- something like y1p and y2p -- then have
if floor(u3/Ts) >= l
l = l+1;
y1p = .3067*x1+3.5924*x2+.6933*u1-3.5994*u2;
y2p = -.2515*x1+.3042*x2+.2515*u1+.6933*u2;
x1 = y1p;
x2 = y2p;
y1 = y1p;
y2 = y2p;
In that way y1 and y2 are defined every time the function is called, but their value only changes when the sample time is hit.