Matlab Coder Error: The output of the ODE function must be a column vector
Show older comments
I tried to generate C code from the following m-file:
function [t1,y1,t2,y2,t3,y3] = FMD(m, d, c)
%definition of the transfer function of a spring mass damper system
num = 1;
denum = [m d c];
%tranformation to a state space system
[A,B,C,D] = tf2ss(num, denum);
%definition of three input signals (sine, exponential and step)
ts = 0:0.01:8;
u1 = sin(10*ts);
u2 = exp(ts);
u3 = zeros(1,length(ts));
u3(ts>=1 & ts<max(ts)) = 1;
%time span for the numerical solver
tspan = [2 5];
%initial condition of the state space system
iniCon = [0;0];
%numerical solving of the ODEs for the three different input signals
[t1, x1] = ode45(@(t,x) sys(t,x,u1), tspan, iniCon);
[t2, x2] = ode45(@(t,x) sys(t,x,u2), tspan, iniCon);
[t3, x3] = ode45(@(t,x) sys(t,x,u3), tspan, iniCon);
%preallocation of output variables
y1 = zeros(length(x1),1);
y2 = zeros(length(x2),1);
y3 = zeros(length(x3),1);
%calculation of the output values for every time step (element-wise
%output function of the state space system)
for i=1:1:length(x1)
y1(i,:) = C*transpose(x1(i,:));
end
for i=1:1:length(x2)
y2(i,:)=C*transpose(x2(i,:));
end
for i=1:1:length(x3)
y3(i,:)=C*transpose(x3(i,:));
end
%nested function for the definition of the state space system
function dx = sys(t, x, u)
%interpolate the data set (ts,u3) at time t
u3I = interp1(ts, u, t);
dx = A*x + B*u3I;
end
end
When I try to run the code in Matlab by calling
clear;
[t1,y1,t2,y2,t3,y3]=FMD(2,5,10);
plot(t1,y1);
everything works as expected. However when I want to generate C code out the function FMD, the first error that I get is the following one:
As the program works fine in Matlab without having trouble with the resulting vector, I do not get the problem and I would be very glad if someone could help me to understand and solve the problem.
Accepted Answer
More Answers (0)
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!