# how to solve error occuring in underlined part?

조회 수: 5 (최근 30일)
Meenakshi Tripathi 2021년 3월 25일
댓글: Meenakshi Tripathi 2021년 3월 25일
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이전 댓글 3개 표시이전 댓글 3개 숨기기
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!
Meenakshi Tripathi 2021년 3월 25일
Thanks, i will try that.

댓글을 달려면 로그인하십시오.

### 답변 (1개)

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이전 댓글 2개 표시이전 댓글 2개 숨기기
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 );
Meenakshi Tripathi 2021년 3월 25일
ok Thanks

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by