Error while solving State space equations using ode45 - matlab

My code should output me a column vector of X at the end of the program.I'm getting a lot of errors.
Please help!
clc;
clear;
t0=0;
tend=.001;
T=[t0 .00005];
T1=[.00005 tend];
temp=1;
X(:,1) = [0;0;0;0];
for k=1:2
for i= temp:50*(k)
[T,X]=ode45(#(T,X)sys(T,X,A1,B1),T,X(:,1));
else
[T1,X1]=ode45(#(T1,X1)sys1(T1,X1,A0,B0),T1,X(:,end));
end
end
temp=50;
end
function Xdot = sys(T,X,A1,B1,U)
Xdot= A1*X + B1*U;
end
function Xdot = sys1(T1,X,A0,B0,U)
Xdot= A0*X + B0*U;
end
The errors are as follows:
Not enough input arguments.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options,
varargin);

It looks like you are not setting the argument U to the functions sys and sys1.
I have highlighted the corresponding places below with <U missing>:
if sw(i)==0 && X(2,i)> vdon || sw(i)==1 && X(1,i)>0
sw(i+1)=1;
[T,X] = ode45( #(T,X) sys(T,X,A1,B1, <U missing> ), T, X(:,1) );
else
sw(i+1)=0;
[T1,X1] = ode45( #(T1,X1) sys1(T1,X1,A0,B0, <U missing> ), T1, X(:,end) );
end

Related

Why this function not getting called?

%% ----------Chua.m----------
function out = chua(t,in)
x = in(1);
y = in(2);
z = in(3);
alpha = 15.6;
beta = 28;
m0 = -1.143;
m1 = -0.714;
h = m1*x+0.5*(m0-m1)*(abs(x+1)-abs(x-1));
xdot = alpha*(y-x-h);
ydot = x - y+ z;
zdot = -beta*y;
out = [xdot ydot zdot]';
end
% ----------StartChua.m----------
% [t,y] = ode45(#Chua,[0 100],[0.7 0 0]);
% plot3(y(:,1),y(:,2),y(:,3))
% grid
Everytime when I use [t,y] = ode45(#Chua,[0 100],[0.7 0 0]); I get an error:
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Also in the code it tells me, the function might be unused. I dont get it, this code should work.
I am on the right path, that I know, also I am not using anthing that can't be compiled.

I am trying to write this function for ode. But it always gives me an error as not enough input arguments. Here is my code associated: [duplicate]

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

Too Many Input Arguments Matlab ode45

I am trying to write a simple Matlab code to model a projectile. Whenever I try to run the code I get an error saying there are too many input arguments. I am running the code with
model1(44.7, 45)
function[] = model1(vel, angle)
close all;
tspan = [0 3];
x0 = [0; 0.915; vel*cos(angle); vel*sin(angle)];
[x] = ode45(#ball, tspan, x0);
function xdot = ball(x)
g = 9.81;
xdot = [x(3); x(4); 0; -g];
end
end
Error using model1/ball
Too many input arguments.
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options,
varargin);
Error in model1 (line 9)
[x] = ode45(#ball, tspan, x0);
I'd appreciate any advice!
The error was (what I also committed many times in the past) that you have to pass the independent variable (time, in this case) too.
function [t, x] = model1(vel, angle)
tspan = [0 3];
x0 = [0; 0.915; vel*cos(angle); vel*sin(angle)];
[t, x] = ode45(#ball, tspan, x0);
end
function xdot = ball(t,x)
g = 9.81;
xdot = [x(3); x(4); 0; -g];
end
I modified your code to return the solution and the corresponding time steps. Moreover, I removed ball to be a nested function.

MATLAB - How to read a single value from a vector? (ode45 involved)

I'm trying to work with a code that involves ode45, in the one in the equation that will be solved I have to introduce a single value from a vector that will change depending on the time the equation is solved, I mean if it is x=x(0) then u1(1) and u2(t), when x=x(1) then u1(2), u2(2)... This is my code:
function [sal] = scobe1
clear
clc
global a;
a=1;
assignin('base','a',a)
tspan = [0; 180];
x0 = [80; 0];
[t,x] = ode45(#f,tspan,x0);
sal=[t,x]
figure
subplot(2,1,1)
plot(t,x(:,1),'k-');
subplot(2,1,2)
plot(t,x(:,2),'b-');
function dxdt = f(t,x)
global a s1 u1 u2 newData;
newData=importdata('datosnoembarazo.mat');
assignin('base','newData',newData);
u1=getfield(newData,'glucosa4dias');
assignin('base','u1',u1);
u2=getfield(newData,'insulina4dias');
assignin('base','u2',u2);
u1=getfield(newData,'glucosa4dias');
assignin('base','u1',u1);
u2=getfield(newData,'insulina4dias');
assignin('base','u2',u2);
if (a<=s1)
dxdt = [
(-4.9e-2-x(2))*x(1) + (4.42 + u1(a)) %(1) IN HERE u1 IS THE VECTOR'S NAME
-9.1e-2*x(2) + u2(a) %(2) IN HERE u2 IS THE VECTOR'S NAME
];
a=a+1
assignin('base','a',a);
else return
end
The problem is that it sends me this error: And I don't know what is wrong with the code, or what else can I do in order for it to read it, can you please help me? Thanks
Error in scobe1>f (line 36)
global a s1 s2 u1 u2 newData;
Error using feval
Output argument "dxdt" (and maybe others) not assigned during call to
"C:\Users\AnnieA\Dropbox\Tesis (A. Olay)\GUI Examples\Resumen de Modelos\scobe1.m>f".
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 scobe1 (line 25)
[t,x] = ode45(#f,tspan,x0);
Your function function dxdt = f(t,x) has two branches in the if statement: If a<=s1, then dxdt gets assigned correctly. If a>s1, dxdt is not assigned at all. You can fix the error by assigning a value to dxdt in the else branch of your if statment before you return. I am not sure what an appropriate value would be in this case. Perhaps 0?

Not enough input arguments in function Matlab

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