It's interesting that every continuous piecewise linear function can be specified by one explicit equation with absolute values​​. The procedure JoggedLine carries out such conversion.

Formal arguments of the procedure: 

A - a list of the coordinates of the vertices of the polyline or the continuous piecewise linear expression defined on the entire real axis.

B (optional) - a point on the left "tail" of the broken line or the slope of the left "tail".

C (optional) - a point on the right "tail" of the broken line or the slope of the right "tail". By default, the angular coefficients of the "tails" are 0.

The global variable  P  is building a graph of this line.

The code of the procedure:

JoggedLine:=proc(A::{listlist,piecewise}, B::{numeric,list}:=[A[1,1]-1, A[1,2]],C::{numeric,list}:=[A[nops(A),1]+1, A[nops(A),2]])

local T,l,r,n,F,M,k,N,K,ll,rl,L,S,U,V,f;

global P;

if type(A, listlist) then T:=[B, A, C] else

T:=A fi;

if type(A, listlist) then

l:=T[1]; L:=T[2]; r:=T[3];

n:=nops(L);

if l::numeric then ll:=L[1][2]+l*(x-L[1][1]);

              else ll:=L[1][2]+(L[1][2]-l[2])/(L[1][1]-l[1])*(x-L[1][1]);

fi;

if r::numeric then rl:=L[n][2]+r*(x-L[n][1]);

              else rl:=L[n][2]+(L[n][2]-r[2])/(L[n][1]-r[1])*(x-L[n][1]);

fi else

S:=sort(convert(discont(T, x), list)); n:=nops(S); f:=unapply(T, x);

L:=[seq([S[i],f(S[i])], i=1..n)];

U:=[L[1,1]-1, f(L[1,1]-1)];

V:=[L[n,1]+1, f(L[n,1]+1)];

ll:=U[2]+(L[1,2]-U[2])/(L[1,1]-U[1])*(x-U[1]);

rl:=L[n,2]+(V[2]-L[n,2])/(V[1]-L[n,1])*(x-L[n,1]) fi;

F[0]:=sum(-a[i]*(x-L[i][1]),i=1..n)+a*x+b;

F[n+1]:=sum(a[i]*(x-L[i][1]),i=1..n)+a*x+b;

M:=[];

if n>=3 then

        for k from 1 to n-2 do

            F[k]:=add(a[i]*(x-L[i][1]),i=1..k)+add(-a[i]*(x-L[i][1]),i=k+1..n)+a*x+b;

            M:=[op(M),F[k]];

                            od;

fi;

N:=solve({coeff(F[0],x)=coeff(ll,x),coeff(F[0],x,0)=coeff(ll,x,0),seq(subs(x=L[i+1][1],M[i])=L[i+1][2],i=1..n-2),coeff(F[n+1],x)=coeff(rl,x),coeff(F[n+1],x,0)=coeff(rl,x,0)},[seq(a[i],i=1..n),a,b]);

K:=[seq(rhs(op(N)[i]),i=1..n)];

P:=plot(sum(K[i]*abs(x-L[i][1]),i=1..n)+rhs(op(N)[n+1])*x+rhs(op(N)[n+2]),x=L[1][1]-1..L[n][1]+1,y=min(-1,subs(x=L[1][1]-1,ll)-1,seq(L[i][2]-1,i=1..n),subs(x=L[n][1]+1,rl)-1)..max(1,subs(x=L[1][1],ll)+1,seq(L[i][2]+1,i=1..n),subs(x=L[n][1]+1,rl)+1),thickness=2,scaling=constrained, numpoints=1000);

sum(K[i]*abs(x-L[i][1]),i=1..n)+rhs(op(N)[n+1])*x+rhs(op(N)[n+2]); 

end proc:

 

 

Example 1. The equation of a single triangular pulse

JoggedLine(piecewise(x>0 and x<=1, 2*x, x>1 and x<2, 4-2*x, 0));

P;

 


 

Example 2.

JoggedLine([seq([n, (-1)^n], n=1..5)]);

P;

 

Example 3. A random broken line.

roll:=rand(0..7):

A:=[seq([n, roll()], n=-1..7)];

JoggedLine(A, roll(), roll());

P;

Equation_of_broken_l.mws

 

 

 

 

 


Please Wait...