Matlab里出现此上下文中不允许函数定义的错误怎么解决
function[sys,x0,str,ts]=spacemodel(t,x,u,flag)switchflag,case0,[sys,x0,str,ts]=mdlIni...
function [sys,x0,str,ts] = spacemodel(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case {2,4,9}
sys=[];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 6;
sizes.NumInputs = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
function sys=mdlOutputs(t,x,u)
qd1=0.1*sin(t);
d_qd1=0.1*cos(t);
dd_qd1=-0.1*sin(t);
qd2=0.1*sin(t);
d_qd2=0.1*cos(t);
dd_qd2=-0.1*sin(t);
sys(1)=qd1;
sys(2)=d_qd1;
sys(3)=dd_qd1;
sys(4)=qd2;
sys(5)=d_qd2;
sys(6)=dd_qd2;
figure(1);
subplot(211);
plot(t,x1(:,1),'r',t,x1(:,2),'b');
xlabel('time(s)');ylabel('position tracking for link 1');
subplot(212);
plot(t,x2(:,1),'r',t,x2(:,2),'b');
xlabel('time(s)');ylabel('position tracking for link 2');
function [sys,x0,str,ts] = spacemodel(t,x,u,flag)
|
错误: 此上下文中不允许函数定义。 展开
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case {2,4,9}
sys=[];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 6;
sizes.NumInputs = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
function sys=mdlOutputs(t,x,u)
qd1=0.1*sin(t);
d_qd1=0.1*cos(t);
dd_qd1=-0.1*sin(t);
qd2=0.1*sin(t);
d_qd2=0.1*cos(t);
dd_qd2=-0.1*sin(t);
sys(1)=qd1;
sys(2)=d_qd1;
sys(3)=dd_qd1;
sys(4)=qd2;
sys(5)=d_qd2;
sys(6)=dd_qd2;
figure(1);
subplot(211);
plot(t,x1(:,1),'r',t,x1(:,2),'b');
xlabel('time(s)');ylabel('position tracking for link 1');
subplot(212);
plot(t,x2(:,1),'r',t,x2(:,2),'b');
xlabel('time(s)');ylabel('position tracking for link 2');
function [sys,x0,str,ts] = spacemodel(t,x,u,flag)
|
错误: 此上下文中不允许函数定义。 展开
2个回答
展开全部
可能你的程序格式有问题。如把代码集成在一起,可以来写
function ode45_main( )
clear all;%清除内存中的变量和和函数
tspan=[0,0.245]; %定义变量的求解区间
y0=[0.449/0.09846,0.1021/0.09846,0.1057/0.09846,0.019/0.09846,0.2327/0.09846,0.085/0.09846,1.01325,1,1273];%定义初始值-v=0.669;
[z,y]=ode45(@redu_model_fun,tspan,y0);
A=[z,y];
disp(' z, y ')
disp(A)
end
function dy=redu_model_fun(z,y)
n=y(1)+y(2)+y(3)+y(4)+y(5)+y(6);
。。。。。。
dy(9)=1/(y(8)*SigmaNC)*(-SigmarH-y(8)*dy(7)-y(7)*dy(8)-SigmaRC*y(9));
end
然后,保存为 ode45_main.m 文件
运行可得
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询