Reputation: 2302
I have a small Simulink model as follows:
and the resepctive code:
function [xdot,y] = fcn(x,u)
% define your constants
g = 9.81;
m = 0.05;
R = 1;
L = 0.01;
C = 0.0001;
x1 = 0.012;
x2 = 0;
x3 = 0.84;
% nonlinear set of equations
xdot = [x2; g-((C/m)*(x3/x1)^2); -((R/L) +(((2*C)/L)*(((x2*x3)/((x1)^2)))))] + [0;0;1/L]*u;
y = x';
However when I try to run, Simulink generates the following errors:
Inferred size ('[1 3]') for data 'y' does not match specified size ('scalar'). Component:MATLAB Function | Category:Coder error Simulink cannot determine sizes and/or types of the outputs for block 'MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
I searched through some documentation for variable size inputs and outputs, selected the variable size check box and also entered the upper bound as [1 3].
When I try to run again I get:
Expression '[1 3]' for maximum of data 'y' must evaluate to a scalar.
I'm not sure how to solve this error. I also looked here, but still couldn't get it to work. Any help would be appreciated.
Upvotes: 2
Views: 9488
Reputation: 10762
You'll probably find that your code will work by make the following changes,
You don't have variable sized data, and can set all of those options back to their default values.
(As indicated in one of the other answers, ) you need to change the way that the input x
enters your equations because at the moment it does not get used to calculate xdot
.
Move the x1
, x2
and x3
to be the 3-by-1 vector of initial conditions for the Integrator
block. (Assuming this is what they really are.)
With those changes the block should detect that the x
signal is a 3-by-1 (since the Integrator
block has 3 initial values), and hence your xdot
output is 3-by-1 and your y
output is 1-by-3.
To be on the safe side, you might also consider putting the following 2 lines at the top of your function.
xdot = zeros(3,1);
y = zeros(1,3);
Those lines will get used during block initialization to tell the compiler what the size of the output signals will be.
NOTE: why are you making y
a 1-by-3 vector? That's very unusual and I suspect you really want it to be a 3-by-1 vector (if you want to output the states) or you are supposed to sum up the values of x
to get y
in which case it is just a scalar.
Not related to the above, but you might also consider making your constants parameters of the block so that you can change them without editing the function.
Upvotes: 4
Reputation: 6863
I think you will have to set the set the sizes for all the inputs and outputs of the function block in the Ports and Data manager, like you tried.
Set the size for y
to [1 3]
, x
to [3 1]
and xdot
to [3 1]
.
Furthermore, I think that there is a mistake in your nonlinear state space, since your 'A' matrix is now constant. So to make them dependent on the current state, replace the declaration of x1
etc to:
x1 = x(1);
x2 = x(2);
x3 = x(3);
I assume that the values you have there right now are your initial conditions for the differential equations, which you will have to set in the integrator block .
Upvotes: 3