Search code examples
matlabsymbolic-math

How to have square wave in Matlab symbolic equation


My project require me to use Matlab to create a symbolic equation with square wave inside. I tried to write it like this but to no avail:

syms t;
a=square(t);

Input arguments must be 'double'.

What can i do to solve this problem? Thanks in advance for the helps offered.


Solution

  • here are a couple of general options using floor and sign functions:

    f=@(A,T,x0,x) A*sign(sin((2*pi*(x-x0))/T));
    f=@(A,T,x0,x) A*(-1).^(floor(2*(x-x0)/T));
    

    So for example using the floor function:

     syms x
     sqr=2*floor(x)-floor(2*x)+1;
     ezplot(sqr, [-2, 2])
    

    enter image description here