error in fblinct where matrix dimensions do not agree??

1 visualizzazione (ultimi 30 giorni)
ive been working on simulated system for control and i encounter that there is an error as
"Error using /
Matrix dimensions must agree.
Error in fblinct (line 4)
yD = sin(2*pi*t/T);
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode23 (line 112)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in hw6_Q4 (line 7)
[t,x]=ode23('fblinct',[t0 tf],x0);
"the function is:
function xdot=fblinct(t,x)
global T
% computation of the desired trajectory
yD = sin(2*pi*t/T);
yDdot=(2*pi/T) * cos(2*pi*t/T);
yDddot= -(2*pi/T)^2 * sin(2*pi*t/T);
% computation of the control input
kp=100;
kd=14.14;
f = sin(x(1)) + x(2)*x(3) + x(1)+x(2)^2;
g = 1 + x(1)^2;
y = x(1);
ydot= x(1)*x(2) + x(3);
e = yD - y;
edot= yDdot - ydot;
u = (-f + yDddot + kd*edot + kp*e) / g;
%Plant dynamics
xdot(1) = x(1)*x(2) + x(3);
xdot(2) = -2*x(2) + x(1)*u;
xdot(3) = sin(x(1)) + 2*x(1)*x(2) + u;
the m-file to run is:
clc;
clear all;
close all;
T=10
t0=0; tf=50;
x0=[1 1 1]';
[t,x]=ode23('fblinct',[t0 tf],x0);
yd= sin(2*pi*t/T);
e= yd - y;
plot(t,[yd,x(:,1)])
plot(t,e)
plot(t,x(:,2))

Risposta accettata

Geoff Hayes
Geoff Hayes il 28 Ott 2014
Try putting a breakpoint at the line
yD = sin(2*pi*t/T);
of your fblinct function and re-run your code (you may want to remove the clear all statement from your main script so that the breakpoint doesn't get cleared). What you should see is that the global variable T is empty, and so 2*pi*t is being divided by an empty matrix, and so the error makes sense.
The problem is that T hasn't been declared as a global variable in the script where it is initialized. So in that script, you would have to do something like
clc;
close all;
global T;
T=10;
% etc.
An alternative approach, is to avoid the global variable altogether and make fblinct a nested function within your main script. Create a main function, and then "nest" fblinct within it
function main
clc;
close all;
T=10;
t0=0; tf=50;
x0=[1 1 1]';
[t,x]=ode23(@fblinct,[t0 tf],x0);
% etc.
function xdot=fblinct(t,x)
% computation of the desired trajectory
yD = sin(2*pi*t/T);
% etc.
end
end
Copy the above into an m file named main.m. Note how we have removed the need for global variables as the nested function fblinct has access to the local variables of the main function. As well, note the ampersand in front of the function name input of ode23 - we are passing @fblinct rather than the string 'fblinct'. It may be a good idea to delete fblinct.m so as not to confuse you about whether the nested function or the m file is being used.
When you run the above you will observe an error with fblinct
Error using odearguments (line 90)
GG/FBLINCT must return a column vector.
Rather, I observed the above (R2014a). This is because your output vector xdot whereas the input vector x is a column. Just add the following as the first line in your fblinct function so that xdot is sized appropriately
function xdot=fblinct(t,x)
xdot = zeros(size(x));
% etc.
end
The above should now work, but you may observe a problem with the line
e= yd - y;
from your main.m function because you haven't defined y. Though it seems to pick up the value of y in your nested function which may or may not be correct. You will have to determine this.

Più risposte (1)

tilak tenneti
tilak tenneti il 5 Nov 2014
Thanks geoff, just declaring T=10 worked

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by