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