0
$\begingroup$

I am simulating a mass - damper - system in Matlab and I have the following vector (1x100) as input to my system:

ut = linspace(0, 10);
u = 5 * sin(2 * ut) + 10.5; % input of our system - external force

Now, I want to take the value of the output for each value of the input vector using this differential equation:

function dx = odefun_4(t,x)
    m = 15;
    b = 0.2;
    k = 2;   
    dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];    
end

which is called from my main .m file like this:

[t,X] = ode45(@odefun_4, [0 10], [0;0])

I tried to pass the u vector by making it global variable but I get an error stating: Dimensions of arrays being concatenated not consistent. I also tried to set the Refine parameter using odeset and the options argument of ode45 solver but still don't get the desired results. I could really use some help.

$\endgroup$
2
  • $\begingroup$ What do you want $u$ actually to be, a smooth continuous function or a discretized function (zero or first order hold) at a fixed interval? $\endgroup$
    – fibonatic
    Commented Nov 11, 2018 at 13:50
  • $\begingroup$ A smooth continuous function. $\endgroup$ Commented Nov 11, 2018 at 13:55

1 Answer 1

1
$\begingroup$

If you want $u$ to be considered as a smooth continuous function, then you should define it as such since you also have $t$ available as well. So you could use:

function dx = odefun_4(t,x)
    m = 15;
    b = 0.2;
    k = 2;
    u = 5 * sin(2 * t) + 10.5;
    dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];
end

I often also find it more convenient to use anonymous functions in Matlab, for example:

m = 15;
b = 0.2;
k = 2;
u = @(t) 5 * sin(2 * t) + 10.5;
odefun_4 = @(t,x) [x(2); u(t)/m - (b/m)*x(2) - (k/m)*x(1)];
$\endgroup$
2
  • $\begingroup$ Well, what you suggest works pretty fine. My issue is that for every value of u (which has 100 values) I want to get one value as output. I have a system and I want one output value for each input value. So , I need 100 output values as I have 100 input values. $\endgroup$ Commented Nov 11, 2018 at 14:11
  • $\begingroup$ @TeoProtoulis ode45 does not allow this. It is a variable step ode solver. You can specify that the time vector you input into ode45 is linspace(0, 10) and filter out any potentially additional time steps afterwards. Since your dynamics is linear you could also use lsim, which only works with constant time steps sampling. $\endgroup$
    – fibonatic
    Commented Nov 11, 2018 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.