Plotting heaviside unit step functions
8 views (last 30 days)
Show older comments
I'm struggling to plot Z(t) (a function with respect to t) from a differential equation in terms of heaviside step functions. My code is as below:
%This function is used to integrate using "ode45", (beta, gamma, A = constants) i.e find "Z(t)" from the differential equation
function [ dydt ] = IntegratingFunction2(t,y,beta,gamma,A)
z = y(1);
zdot = y(2);
dydt = zeros(size(y));
dydt(1) = zdot;
dydt(2) = heaviside(A*t) + 4*heaviside(A*(t-1)) - 5*heaviside(A*(t-3)) - beta*gamma*zdot - beta*z;
return
Then in my main script I called the function:
[tLinear,y] = ode45(@(t,y)IntegratingFunction(t,y,beta,gamma,A(k)), tspan, xinit,options);
I then made:
%This statement means that the first column from "y" are all the values for "z" (a function in terms of t(time)
z = y(:,1);
% the output "y" is technically dydt, as written in my function earlier % I now plot "z" in terms of "t" (time)
plot(tLinear , z , 'r--');
Now here is the problem, nothing shows/plots. Why?
0 Comments
Accepted Answer
Richard Brown
on 19 Apr 2012
If you replace the line with the heavisides in it with the one from my previous answer
dydt(2) = (A*t >= 0) + 4*(A*(t-1) >= 0) - 5*(A*(t-3) >= 0) - beta*gamma*zdot - beta*z;
your code works perfectly. The reason you can't see the dashed lines is because they are all stacked on top of each other - if you put a pause statement inside your loop, you can see the solutions build up.
The reason for this is because for A > 0, Heaviside(A*(t-1)) is exactly equal to Heaviside(t - 1), so the different values of A have no effect. With the tanh function the sigmoid gets closer and closer to the Heaviside function as A increases.
6 Comments
Andy Zelenak
on 7 Dec 2014
Edited: Andy Zelenak
on 7 Dec 2014
Thanks Richard. This was useful to me, too. I didn't know you could put inequalities in an equation like that.
More Answers (2)
Richard Brown
on 19 Apr 2012
heaviside is a function from the symbolic toolbox - I'd be surprised your code doesn't complain when you try to call it.
Why not instead use
dydt(2) = (A*t >= 0) + 4*(A*(t-1) >= 0) - 5*(A*(t-3) >= 0) - beta*gamma*zdot - beta*z;
And see how that goes ...
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!