@(T,X)SSMODEL must return a column vector ERROR

2 views (last 30 days)
function dx = ssmodel(t,x)
syms alpha1 alpha2 alpha3 alpha4 x
A = [0 1 0 0; 0 0 1 0; 0 0 0 1; -alpha4 -alpha3 -alpha2 -alpha1];
B = [0; 0; 0; 1];
K = [16-alpha4 32-alpha3 24-alpha2 8-alpha1];
u = -K*x;
dx = A*x + B*u;
end
x0 = [-1; -1; -2; -2];
tspan = [0, 10, 100];
[t,x] = ode45(@(t,x) ssmodel,tspan,x0);
plot(t,x)
Error using odearguments (line 93)
@(T,X)SSMODEL must return a column vector.
Error in ode45 (line 106)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in multiodev (line 23)
[t,x] = ode45(@(t,x) ssmodel,tspan,x0);
How can i solve this error ? Please help me.

Accepted Answer

Star Strider
Star Strider on 28 Dec 2022
I have no idea what ‘alpha’ is, however it must be numeric and not symbolic. It needs to be passed as an extra argument to ‘ssmodel’ in any event.
This works, however it will be necessary to understand what you want to do in order to provide a complete answer —
x0 = [-1; -1; -2; -2];
tspan = [0, 10, 100];
a = randn(1,4);
[t,x] = ode45(@(t,x)ssmodel(t,x,a),tspan,x0);
plot(t,x)
function dx = ssmodel(t,x,alpha)
A = [0 1 0 0; 0 0 1 0; 0 0 0 1; -alpha(4) -alpha(3) -alpha(2) -alpha(1)];
B = [0; 0; 0; 1];
K = [16-alpha(4) 32-alpha(3) 24-alpha(2) 8-alpha(1)];
u = -K*x;
dx = A*x + B*u;
end
.

More Answers (1)

Jan Studnicka
Jan Studnicka on 28 Dec 2022
The ssmodel function must be defined as described in the documentation:
You cannot use symbolic expressions with ode45:
and I believe that you want to create a model with parameters alpha1 alpha2 alpha3 alpha4. I suggest you do that this way:
x0 = [-1; -1; -2; -2];
tspan = [0, 10, 100];
alpha = [0 0 0 0]; % You need to set alpha before applying ode45
[t,x] = ode45(@(t,x) ssmodel(t,x,alpha),tspan,x0);
plot(t,x)
function dx = ssmodel(t,x, alpha)
% alpha is vector of length 4
A = [0 1 0 0; 0 0 1 0; 0 0 0 1; -alpha(4) -alpha(3) -alpha(2) -alpha(1)];
B = [0; 0; 0; 1];
K = [16-alpha(4) 32-alpha(3) 24-alpha(2) 8-alpha(1)];
u = -K*x;
dx = A*x + B*u;
end

Tags

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!