
Trying to write a function using step uni function and ramp function (elementary functions)
5 views (last 30 days)
Show older comments
As seen, I’m trying to express this graph in elementary functions, I found the corner points of every line, and I used if statement, to find out if it’s a ramp function with a slop or if it’s a unit function (just a vertical line), or if it’s a horizontal line due to a ramp function of the opposite sign, however I got the equations for the first 3 lines (a1 & a2 & a3) correct but the rest aren’t and matlab is miscalculating the slope I don’t know why, please help, here’s my code
clc
clear
syms t r u f t0 t1
x = [-1;
0;
0;
1;
2;
2.5;
2.5;
4;
4];
y = [0, 0.5, 1, 1, 2, 1, 0.5, 0.5, 0];
figure
plot(x,y)
p1=(y(2)-y(1))/(x(2)-x(1));
p2=(y(3)-y(2))/1/x(2)-x(2);
p3=(y(4)-y(3))/1/x(4)-x(3);
p4=(y(5)-y(4))/x(5)-x(4);
p5=(y(6)-y(5))/x(6)-x(5);
p6=(y(7)-y(6))/1/x(7)-x(6);
p7=(y(8)-y(7))/1/x(8)-x(7);
p8=(y(9)-y(8))/1/x(9)-x(8);
p = [p1,p2,p3,p4,p5,p6,p7,p8];
n1= (y(2)-y(1));
n2= (y(3)-y(2));
n3= (y(4)-y(3));
n4 =(y(5)-y(4));
n5=(y(6)-y(5));
n6=(y(7)-y(6));
n7= (y(8)-y(7));
n8=(y(8)-y(7));
n=[n1,n2,n3,n4,n5,n6,n7,n8];
if p(1)==inf & p(1)==-1*inf
t1=x(1);
a1= n(1)*u*(t-t1);
else p(1)>0 & p(1)<0;
t0=x(1);
a1=p(1)*r*(t-t0);
end
if p(2)==inf & p(2)==-1*inf
t1=x(2);
a2= n(2)*u*(t-t1);
else
if p(2)>0 & p(2)<0
t0=x(2);
a2=p(2)*r*(t-t0);
else
a2= -1*p(1)*r*(t-t0);
end
end
if p(3)==inf & p(3)==-1*inf
t1=x(3);
a3= n(3)*u*(t-t1);
elseif p(3)>0 & p(3)<0
t0=x(3);
a3=p(3)*r*(t-t0);
elseif p(2)~= 0 && p(2)~= -1*inf && p(2)~=inf
a3= -1*p(2)*r*(t-t0);
else
a3= -1*p(1)*r*(t-t0);
end
if p(4)==inf & p(4)==-1*inf
t1=x(4);
a4= n(4)*u*(t-t1);
elseif p(4)>0 & p(4)<0
t0=x(4);
a4=p(4)*r*(t-t0);
elseif p(3)~= 0 && p(3)~= -1*inf && p(3)~=inf
a4= -1*p(3)*r*(t-t0);
elseif p(2)~= 0 && p(2)~= -1*inf && p(2)~=inf
a4= -1*p(2)*r*(t-t0);
else
a4= -1*p(1)*r*(t-t0);
end
if p(5)==inf & p(5)==-1*inf
t1=x(5);
a5= n(5)*u*(t-t1);
elseif p(5)>0 & p(5)<0
t0=x(5);
a5=p(5)*r*(t-t0);
elseif p(4)~= 0 && p(4)~= -1*inf && p(4)~=inf
a5= -1*p(4)*r*(t-t0);
elseif p(3)~= 0 && p(3)~= -1*inf && p(3)~=inf
a5= -1*p(3)*r*(t-t0);
elseif p(2)~= 0 && p(2)~= -1*inf && p(2)~=inf
a5= -1*p(2)*r*(t-t0);
else
a5= -1*p(1)*r*(t-t0);
end
if p(6)==inf & p(6)==-1*inf
t1=x(6);
a6= n(6)*u*(t-t1);
elseif p(6)>0 & p(6)<0
t0=x(6);
a6=p(6)*r*(t-t0);
elseif p(5)~= 0 && p(5)~= -1*inf && p(5)~=inf
a6= -1*p(5)*r*(t-t0);
elseif p(4)~= 0 && p(4)~= -1*inf && p(4)~=inf
a6= -1*p(4)*r*(t-t0);
elseif p(3)~= 0 && p(3)~= -1*inf && p(3)~=inf
a6= -1*p(3)*r*(t-t0);
elseif p(2)~= 0 && p(2)~= -1*inf && p(2)~=inf
a6= -1*p(2)*r*(t-t0);
else
a6= -1*p(1)*r*(t-t0);
end
if p(7)==inf & p(7)==-1*inf
t1=x(7);
a7= n(7)*u*(t-t1);
elseif p(7)>0 & p(7)<0
t0=x(7);
a7=p(7)*r*(t-t0);
elseif p(6)~= 0 && p(6)~= -1*inf && p(6)~=inf
a7= -1*p(6)*r*(t-t0);
elseif p(5)~= 0 && p(5)~= -1*inf && p(5)~=inf
a7= -1*p(5)*r*(t-t0);
elseif p(4)~= 0 && p(4)~= -1*inf && p(4)~=inf
a7= -1*p(4)*r*(t-t0);
elseif p(3)~= 0 && p(3)~= -1*inf && p(3)~=inf
a7= -1*p(3)*r*(t-t0);
elseif p(2)~= 0 && p(2)~= -1*inf && p(2)~=inf
a7= -1*p(2)*r*(t-t0);
else
a7= -1*p(1)*r*(t-t0);
end
if p(8)==inf & p(8)==-1*inf
t1=x(8);
a8= n(8)*u*(t-t1);
elseif p(8)>0 & p(8)<0
t0=x(8);
a8=p(8)*r*(t-t0);
elseif p(7)~= 0 && p(7)~= -1*inf && p(7)~=inf
a8= -1*p(7)*r*(t-t0);
elseif p(6)~= 0 && p(6)~= -1*inf && p(6)~=inf
a8= -1*p(6)*r*(t-t0);
elseif p(5)~= 0 && p(5)~= -1*inf && p(5)~=inf
a8= -1*p(5)*r*(t-t0);
elseif p(4)~= 0 && p(4)~= -1*inf && p(4)~=inf
a8= -1*p(4)*r*(t-t0);
elseif p(3)~= 0 && p(3)~= -1*inf && p(3)~=inf
a8= -1*p(3)*r*(t-t0);
elseif p(2)~= 0 && p(2)~= -1*inf && p(2)~=inf
a8= -1*p(2)*r*(t-t0);
else
a8= -1*p(1)*r*(t-t0);
end
f=a1+a2+a3+a4+a5+a6+a7+a8;
0 Comments
Answers (1)
Aashray
on 19 Jun 2025
From the code, I understand that you are trying to build a piecewise function using ramp and unit step functions (r(t) and u(t)), based on the slopes between corner points. You're getting correct equations for the first few segments, but incorrect slopes later.
The trick here lies in the operator precedence in MATLAB. The division operator takes precedence over subtraction. So, while calculating p2, the following line:
p2 = (y(3)-y(2))/1/x(2)-x(2);
is interpreted by MATLAB as:
temp = (y(3)-y(2))/1;
temp2 = temp / x(2);
p2 = temp2 - x(2);
which is not the slope between two points.
To compute slope between point (x(i), y(i)) and (x(i+1), y(i+1)), you should write it as:
p2 = (y(3)-y(2)) / (x(3)-x(2));
So, just fixing all the slope assignments should work.
I have also attached the output plot that I got after fixing the slope assignments for reference.

Documentation link for operator precedence: https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms 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!