2

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).

The approach I have tried is:

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) );

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')

I'm getting the following error:

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

How can I resolve this error?

0

1 Answer 1

3

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

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) );

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the patience knedlsepp. It worked, but can you please explain me why did you do this: xprimefixedTandOmega = @(t,x) xprime(t, x, T, Omega);
This line fixes the values T and Omega. Think if You are the solver: "I want to get a function f(t,x). I know the first value is time and the second value is the value at this time. I will then do my magic and return the solution to x' = f(t,x)". When you pass in a function with more then 4 parameters the solver ode45 will think: "Well, I got passed a function with 4 parameters. I know the first is time, the second is the value at this time, but I have no idea what the other two parameters are." Then it tries to use only the first two => Error: "Not enough input arguments."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.