ODE45 & heaviside- [Inputs must be floats, namely single or double.]

2 views (last 30 days)
Hi!
I'm having problems with the code when solving differential equations by ode45.
The point is that I must enter singular functions from GUIDE:
u(t) -> Step. " ONLY THE HEAVISIDE FUNCTION ALLOWS ME"
Code:
vin=get(handles.editE,'String');
vin=strrep(vin,'u','heaviside');
t=(str2double(get(handles.editI,'String')):0.001:str2double(get(handles.editF,'String')));
tspan = [0 9];
vs=(str2double(get(handles.editPVI,'String')));
R=(str2double(get(handles.editR,'String')));
C=(str2double(get(handles.editC,'String')));
tau = R*C;
syms t;
NOW HERE I TRIED:
vin = eval (vin) -> not working.
vin = symfun (vin, t) -> not working.
syms vin -> not working.
.
f =@(t,v) vin/tau-v/tau; v0=0;
% works like this (literally the function):
% f =@(t,v) heaviside(t)/tau-v/tau; v0=0;
% [t,v] = ode45(f,tspan,v0)
But I can not put this function as such, since vin can be a step, pulse, ramp, an exponential function, etc ...
[t,v] = ode45(f,tspan,v0);
axes(handles.axesVc);
grid on
plot(t,v);
Somebody help me :(

Accepted Answer

Steven Lord
Steven Lord on 9 May 2018
The ode45 function cannot accept functions that return a sym object as the first input to ode45. You could use symbolic calculations inside the ODE function so long as what it returns is a column vector of data type single or double.
For two other approaches, you could use dsolve to try to solve the system of ODEs symbolically or you could use the odeFunction function to try to convert a symbolic expression into a function handle that is compatible with the numeric ODE solvers like ode45. See this page in the documentation for more information.
  1 Comment
baney acosta
baney acosta on 10 May 2018
Edited: baney acosta on 10 May 2018
Hi.
In fact, I chose ode45 since the dsolve function gives me a strange result.
This is the result that should give me this equation:
And this is the result that dsolve gives me:
sign(t)/2 - exp(-t)/2 + 1/2
I really do not know why this is.
vin=get(handles.editE,'String');
vin=strrep(vin,'u','heaviside');
ti=str2double(get(handles.editI,'String'));
tf=str2double(get(handles.editF,'String'));
tspan =[ti:0.01:tf];
vs=(str2double(get(handles.editPVI,'String')));
R=(str2double(get(handles.editR,'String')));
C=(str2double(get(handles.editC,'String')));
tau = num2str(R*C);
syms t;
vc = dsolve(['Dv+v/',tau,'=',vin,'/',tau],'v(0)=0');
disp(vc)
  • tau=1
  • vin=heaviside(t)

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!