Matlab函数中输入参数不足 [英] Not enough input arguments in function Matlab

查看:1576
本文介绍了Matlab函数中输入参数不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发GUI来解决微分方程时遇到问题,我找不到错误.

I'm having a problem developing my GUI to solve a differential equation and I can not find the error.

我要求解的方程式定义为:

The equation I'm trying to solve is defined by:

T*x'+x = kSigma*heaviside(t-t0) + kSin*sin(Omega*t+alpha*pi/180).

我尝试过的方法是:

function lsg = DGLvar(t,T,Omega)
   x = 1;
   kSin = 1;
   kSigma = 5;
   t0 = 0;
   alpha = 0;
   lsg = 1/T * (-x + kSigma*heaviside(t-t0) + kSin*sin(Omega*t+alpha*pi/180) );

在GUI中,代码如下所示:

In the GUI the code looks like this:

function pushbutton1_Callback(hObject, ~, handles)
   t=[0 100];
   periode=get(handles.sliderT,'value');
   Omega=get(handles.slideromega,'value');
   [x,t]=ode45(@DGLvar,t,periode,Omega);
   plot(handles.axes2,x,t,'g')

我遇到以下错误:

Error using DGLvar (line 8)
Not enough input arguments.

Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

Error in PT1>pushbutton1_Callback (line 218)
   [x,t]=ode45(@DGLvar,t,periode,Omega);

Error in gui_mainfcn (line 95)
    feval(varargin{:});

Error in PT1 (line 42)
gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)PT1('pushbutton1_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

如何解决此错误?

推荐答案

求解器ode45希望输入函数f(t,x)作为输入,并将其用于求解方程式x'=f(t,x).

The solver ode45 expects as input a function f(t,x) and uses this to solve the equation x'=f(t,x).

您需要将x用作函数DGLvar的参数.我还建议将其重命名为xprime,因为这更具描述性.

You need to use x as a parameter for your function DGLvar. I would also recommend renaming it to xprime, as this is more descriptive.

function xp = xprime(t,x,T,Omega)
   kSin = 1;
   kSigma = 5;
   t0 = 0;
   alpha = 0;
   xp = 1/T * (-x + kSigma*heaviside(t-t0) + kSin*sin(Omega*t+alpha*pi/180) );

GUI代码如下所示:

The GUI code would look like this:

%% Get the GUI values:
tspan = [0 100];
x0 = 0;
T = get(handles.sliderT, 'value');
Omega = get(handles.slideromega, 'value');
%% Define a function with two parameters @(t,x) for ode45.
xprimefixedTandOmega = @(t,x) xprime(t, x, T, Omega);
%% Solve the equation: x' = xprimefixedTandOmega(t,x), x(0)=0 for t=0...100.
[t,x] = ode45(xprimefixedTandOmega, tspan, x0);
%% Plot the result of solver
plot(handles.axes2, t, x, 'g');

这篇关于Matlab函数中输入参数不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆