how to solve error occuring in underlined part?

2 次查看(过去 30 天)
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 个评论
Bjorn Gustavsson
Bjorn Gustavsson 2021-3-25
@Meenakshi Tripathi: Well, then I repeat my answer: Run the code with dbstop if error. That way you can see what your variables are. Here the error-message indicates that observer does not return a column array which is what the odeNN functions are designed to handle. You can also tell the debugger to stop in observer by issuing this directive:
dbstop in observer
ALSO: remove the globals!

请先登录,再进行评论。

回答(1 个)

Bjorn Gustavsson
Bjorn Gustavsson 2021-3-25
编辑:Bjorn Gustavsson 2021-3-25
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 个评论
Bjorn Gustavsson
Bjorn Gustavsson 2021-3-25
Please use the code-markup for your matlab-code, also use indentation for improved readability. Further, remove all global variables. Read up on how to pass additional arguments to the ode-function. In brief:
function dzdt = your_ode(t,z,A,B,C)
dzdt = [z(2)*A;
-z(1)*B+z(2)/C];
end
Then in the ode45-call:
[t_out,z_out] = ode45(@(t,z) your_ode(t,z,12,17,23),t_span,z0 );

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by