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

3 次查看(过去 30 天)
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 :(

采纳的回答

Steven Lord
Steven Lord 2018-5-9
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 个评论
baney acosta
baney acosta 2018-5-10
编辑:baney acosta 2018-5-10
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by