0

I'm new to Simulink but I need to use a matlab function.

I created a "MATLAB Function1" block with one input (a time signal that comes from another block) and one output (three signals wrapped in a matrix shown in a Scope block).

Here the code inside the matlab function block:

function outputSignal  = myFunction(input_signal)
coder.extrinsic('lsim');

time = [1:1:length(input_signal)];

k_dia = [19.5 13 9.9];
k_dia = k_dia*10^-3;

outputSignal = zeros(length(time), length(k_dia));

for j = 1:length(k_dia)
    A = [-k_dia(j) 0; k_dia(j) -k_dia(j)];
    B = [1 0]';
    C = [1 1];
    D = 0;
    sistem = feval('ss', A, B, C, D);
    outputSignal(:,j) = lsim(sistem, input_signal, time);
end
end

Previously I had problems using the functions "ss" and "lsim" because of code generation problems, but I should have solved them using feval and coder.extrinsic. Now I have the following error:

When simulating the response to a specific input signal, the input data U must be a matrix
of numeric values with at least two rows (samples) and without any NaN or Inf.  

and I can't understand if the problem is still with these functions or if I made a mistake in how to use matlab functions in simulink.

EDIT: I understood that the problem was because lsim needs an input_signal of at least length 2, while my system is giving an input to the function of one single value at every time step. So if the time is of 10 steps and the serie of values generated from the previous block is [1 2 3 4 5 6 7 8 9 10], I would like to have as input to my function:

  • [1] at step 1,
  • [1 2] at step 2,
  • [1 2 3] at step 3, ....
  • [1 2 3 4 5 6 7 8 9 10] at step 10.

Of course since lsim doesn't work with one value, at step 1 I could use a default output value.

I think I need a block that memorizes what happens in the time steps before, something like the Scope block but with an output.

2
  • It would be good if you could provide the input signal you are using to test your model. Commented Aug 8, 2016 at 11:18
  • 1
    You have 3 systems as evidenced by your loop with 3 independent calls to ss and lsim. Instead use the provided Simulink State-Space block. Your question does not address why you have to the use the Matlab function block. Commented Aug 8, 2016 at 18:24

1 Answer 1

1

Since your systems are time invariant you can create three separate State space systems each with different parameter valued matrices and supply a vector of three components as input signals which I have left empty because I don't know where you would like to send from.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but I can't understant what you are saying. Please take a look at the edited question
@leonardovet You don't need to lsim in Simulink. The whole idea of simulink is to let matlab do the simulation for you

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.