how to solve error occuring in underlined part?

1 view (last 30 days)
function food2
clc
clear all
global E A B C RB k L N M tspan x0 z0 u
E = [1 0 0 0;0 1 0 0;0 0 0 0;0 0 0 0]
A = [0 0 1 0;1 0 0 0;-1 0 0 1;0 1 1 1]
B = [0;0;0;-1]
C = [-1 0 0 1;0 1 1 1; 0 1 0 0]
RB = [0;0;0;0]
M = [0 0 -0.6265;-0 0 1.8440;-1 1 -1.9598;1 0 -0.3333]
k = [-2.7031 2.0302 -2.2363;0.6138 -0.7490 4.4640;-1.5056 1.5606 -2.0025;0.2045 1.4432 -1.9992]
L = [-1 1 1.2394;0 0 -3.7553;1 -1 4.1909;-1 1 0.1070]
N = [-2.0767 0.2061 -1.0302 0.6729;-0.2302 -3.7150 0.7490 0.1352;-0.5458 0.4419 -2.5606 -0.0550;0.5378 0.5560 -0.4432 -1.6477]
tspan = [0:.1:20]
x0 = [1 0 -1 1]
[x,y] = solbasic
function dzdt = observer(t,z)
dzdt=N*z+L*y'+RB*u;
end
z0 = [10 11 6 8]
[~,z]=ode45(@observer,tspan,z0); % <---- Error here
z;
x;
x_cap=z+y*M';
e=x-x_cap;
end
function [x,y] = solbasic()
global E C x0 tspan
opt = odeset('RelTol', 1e-6, 'Stats', 'off', 'Mass', E);
[~, x] = ode15s(@basic, tspan, x0, opt)
y=x*C'
end
function dxdt = basic(t,x)
global A B u
u= [t^2];
dxdt=A*x+B*u;
end
Errors are -
Error using odearguments (line 93)
FOOD2/OBSERVER must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in food2 (line 22)
[~,z]=ode45(@observer,tspan,z0);
  5 Comments

Sign in to comment.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 25 Mar 2021
Edited: Bjorn Gustavsson on 25 Mar 2021
Run this code, but set the debug condition to stop at errors:
dbstop if error
Then you'll get a debug-propmt at the line where the error occurs and you can inspect all variables in that function and step up the function call stack and see where things go wrong.
One possible error I see is that you use global variables to define u. Using global variables for (possibly varying) parameters to ODEs are a dubious practice - at best. The ODE-integrating function evaluates the ode-function at a cunning set of points - but not necessarily in a predictable neat order. You as a user should look at the ode-integrating function as something that calls the ode-function at random points in (t,z) - and design the ode-function to handle that. Therefore: remove the globals! Then check that the sizes of the variables are correct.
HTH
  4 Comments

Sign in to comment.

Categories

Find more on QSP, PKPD, and Systems Biology in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!