What is the best function of the following shape?

How can I construct this shape? what is the best function of it?
It is more like sigmoid function + straight line
But I cant get how can I represent it by one equation and How to control its limits!
The Y limits should be from 0 to 1
( I want to use it as a fuzzy memebership function)
Is there any recommendation?

2 Comments

Not exactly sure what you want to do because you didn't define the problem in a mathematical manner. The existing functions in the Fuzzy Toolbox library stock do not have cutoff points. Guess you want to create a custom Sigmoidal Membership Function with one-sided cutoff point for your unique Fuzzy System, where it can be rescaled and reflected in anyway you want:
Hi @Sam Chak, How did you get the above plots? Could you please provide me with the code.
I want to use this membership function in a code, not with the fuzzy tool box. Thanks

Sign in to comment.

 Accepted Answer

Hi @M
The original code was accidentally highlighted and deleted due to sensitive touchpad. It was constructed based on @Torsten's idea on the Arctangent function but I replaced it with the Logistic function as shown in this link:
Since you don't want anything to do with the Fuzzy Logic Toolbox, this new solution, a Quintic function (not in the toolbox, so you can probably claim novelty in the Fuzzy MF paper), is tailor-made for you so that you adjust the position of the cutoff point (end point of blue line) easily.
Example #1: The Quintic function, a 5th-order polynomial, is commonly used in the trajectory planning of a mobile robot. So I modified the function for a custom fuzzy membership function.
I have not tested everything. But this is the basic concept for you to develop. Hope it helps.
% Quintic function-based MF (blue line as main)
% Start point (xA, yA); End point (xB, yB);
xf = 1;
x = linspace(0, xf, xf*1e4 + 1);
yA = 0;
yB = 1;
xA = 0.1;
xB = 0.9;
y = (yA + (10*((x - xA)/(xB - xA)).^3 - 15*((x - xA)/(xB - xA)).^4 + 6*((x - xA)/(xB - xA)).^5)*(yB - yA)).*((0 <= (x - xA)) & ((x - xA) < (xB - xA)));
plot(x, y, -(x-xf), y), grid on, ylim([0 1.2])
legend('MF_1', 'MF_2', 'location', 'north')
xlabel('Universe of Discourse, \it{x}'), ylabel('\mu(\it{x})')
Example #2: Using the Logistic function:
xf = 1;
x = linspace(0, xf, xf*1e4 + 1);
xA = 0.1; % Left bound of the blue S-curve
xB = 0.9; % Right bound of the blue S-curve
a = 2*9.2; % (logistic growth rate) adjust the steepness of S-curve
c = 0.5; % shift the center of S-curve to x = c
y = sigmf(x, [a c]).*((0 <= (x - xA)) & ((x - xA) < (xB - xA)));
plot(x, y, -(x-xf), y), grid on, ylim([0 1.2])
legend('MF_1', 'MF_2', 'location', 'north')
xlabel('Universe of Discourse, \it{x}'), ylabel('\mu(\it{x})')

7 Comments

It looks like your polynomial and mine are the same.
I find it on my own.
M
M on 7 Sep 2022
Edited: M on 7 Sep 2022
@Sam Chak It is a really very good Idea. If you can please provide me also with your full first solution. It might help also. Thanks
Hi @M
As mentioned previously, because you asked for a sigmoidal function and something related to fuzzy MF, naturally, the search result directs me to sigmf():
You are encouraged to explore the sigmoidal functions that suit your application.
ub = 1;
lb = -ub;
x = linspace(lb, ub, (ub - lb)*1e3 + 1);
a = 9.2; %
c = 0; % center of function
y = sigmf(x, [a c]);
plot(x, y, 'linewidth', 1.5), grid on,
xlabel('Universe of Discourse'), ylabel('\mu')
@Sam Chak which function do you think allow more adjustment to the S-curve ? is it the sigmoindal or Ploy functions ?
@M, Theoretically, the polynomial function offers more flexibility in adjusting the shape of the S-curve in anyway you like as long as it is monotonically increasing/decreasing, and if you know how to adjust the coefficients of the terms.
For simplicity, you may want to choose the Logistic function (aka sigmf), because you just need adjust one parameter, the logistic growth rate (a) to shape the steepness of the S-curve.
I have added a second example in the edited Answer.
((0 <= (x - xA)) & ((x - xA) < (xB - xA)))
or equivalently
((x >= xA) & (x < xB))

Sign in to comment.

More Answers (4)

x = -2:0.01:2;
plot([x,x(end)],[1/pi*atan(2*x)+1/2,1/pi*atan(2*x(1))+1/2])
xlim ([-3 3])
ylim ([0 1])

14 Comments

M
M on 6 Sep 2022
Edited: M on 6 Sep 2022
@Torsten, Thanks but how to add the left staright line to the shape as I post in the above figure?
If you can please provide me with one general function
@Torsten, How to control the Y axis Limits? I want it from 0 to 1 ?
M
M on 6 Sep 2022
Edited: M on 6 Sep 2022
@Torsten I need to control the X and Y limits before the plot, Because I want to construct multiple functions
@Torsten, Can you please provide me with general function not as plot..Thakns
The function is written in the plot.
x = 0:0.01:1;
plot([x,x(end)],[1/pi*atan(2*x)+1/2,1/pi*atan(2*x(1))+1/2])
xlim ([-3 3])
ylim ([0 1])
M
M on 6 Sep 2022
Edited: M on 6 Sep 2022
@Torsten I adjusted the x limits of the function I obtaind the the above shape! also it gave wronge y axis limits in this case.
My all functions are in right positive side and limited in [0 1] in y axis.
M
M on 6 Sep 2022
Edited: M on 6 Sep 2022
@Torsten, I want to keep the S function shape as before.
If you plot the function in a subinterval, you get the plot of the function in the subinterval. What do you expect without changing the function itself ?
x = 0:0.01:1;
plot([x,x(end)],[1/pi*atan(10*(x-0.5))+0.5,1/pi*atan(10*(x(1)-0.5))+0.5])
xlim ([0 1.1])
ylim ([0 1])
@Torsten, I appreciate your help, Thank you so much
M
M on 7 Sep 2022
Edited: M on 7 Sep 2022
Hi @Torsten, I am not able to adjust the y limits as I want, I want to it to strart exactly from zero and end to 1. Could you please help in this? And If you could please tell me based on what you do changes for the equation to include the desired limits? Is it random change or else? Thanks
Not possible with this function - it goes asymtotically against 0 and 1.
Use Sam Chak's solution instead.
@Sam Chak Please provide me with your full solution

Sign in to comment.

Try this:
xmin = 1;
xmax = 3;
pp = struct('form','pp',...
'breaks',[-1e-100 0 1 inf],...
'coefs',[0 0 0 0 0 0;6,-15,10,0,0,0;0 0 0 0 0 1],...
'pieces',3,...
'order',6,...
'dim',1);
sfun = @(x) ppval(pp,(x-xmin)/(xmax-xmin));
ezplot(sfun,xmin-1,xmax+1)

13 Comments

@Bruno Luong Thank you so Much, your idea might work also
It's exact same Sam Chack's when you look carefull the coefficients, and extended also on the left/right sides of the x boundaries.
@Bruno Luong Thats correct. Also Sam's answer contains the part of the straight line. Thanks
Yes, @Bruno Luong, the methods are the same. In fact, you posted the Answer 5 min earlier than me. Thanks as I learned something new about ppval() from you.
However, the fixed 5th-order polynomial trajectory generation approach does not allow the S-curve to be adjusted. An option for @M to choose between the 5th-, 7th,- and 9th-order would probably allow some flexiness.
@Bruno Luong @Sam Chak Thank you both for your useful information.
@Bruno Luong Can you please suggest a way about how to add straight line to your solution? Thanks
My function sfun has constant value of 0 for x <= xmin and 1 for x >= xmax.
The function graph are therefore straignt lines in those domain.
@Bruno Luong I meant in the end of the curve to close it, like is shown in the question figure.
I tried to plot your code in xlim 0 6 , the straight line didnt appear. Thanks
xmin = 1;
xmax = 3;
pp = struct('form','pp',...
'breaks',[-1e-100 0 1 inf],...
'coefs',[0 0 0 0 0 0;6,-15,10,0,0,0;0 0 0 0 0 1],...
'pieces',3,...
'order',6,...
'dim',1);
sfun = @(x) ppval(pp,(x-xmin)/(xmax-xmin));
ezplot(sfun,xmin-1,xmax+1)
grid on , xlim ([0 6])
If you want the function to be 0 for x >= xmax, set the last value of 'coefs' to 0.
@Bruno Luong Still it doesnt work
xmin = 1;
xmax = 3;
pp = struct('form','pp',...
'breaks',[-1e-100 0 1 inf],...
'coefs',[0 0 0 0 0 0;6,-15,10,0,0,0;0 0 0 0 0 0],...
'pieces',3,...
'order',6,...
'dim',1);
sfun = @(x) ppval(pp,(x-xmin)/(xmax-xmin));
ezplot(sfun,xmin-1,xmax+1)
grid on , xlim ([0 6])
"In mathematics, a function from a set X to a set Y assigns to each element of X exactly one element of Y."
You ask for function. I give you a code that provide a function with real input x that drops to 0 at xmax.
If you want to do something else with the function, you are free to do it on top of my function sfun.
If you don't want the tail just plot in the ROI
xmin = 1;
xmax = 3;
pp = struct('form','pp',...
'breaks',[-1e-100 0 1 inf],...
'coefs',[0 0 0 0 0 0;6,-15,10,0,0,0;0 0 0 0 0 0],...
'pieces',3,...
'order',6,...
'dim',1);
sfun = @(x) ppval(pp,(x-xmin)/(xmax-xmin));
close all
x = linspace(xmin,xmax,2^12+1);
plot(x,sfun(x),'linewidth',2)
xlim([0 4])
In analysis, a "function" is any mapping of values to other values. It might or might not be "one to one" or "onto". There is no requirement that the function maps all possible inputs in the domain.
In analysis, functions over infinite sets are treated like the limit of finite mappings. For example, x->x^2, x in Z, is treated as-if it were the set of mappings {..., -2 -> 4, -1 -> 1, 0 -> 0, 1 -> 1, 2 -> 2, ...} rather than as being something fundamentally different. There does not have to be a formula for a function: the complete list of mappings defines the function; formulae can make it significantly more compact to express though. The mappings are the fundamental property of the function; formulae are conveniences to express mappings.
"There is no requirement that the function maps all possible inputs in the domain."
Wrong to my book, all elements of input set (domain) must be mapped (defined) by the function.

Sign in to comment.

Looks like tansig

2 Comments

M
M on 6 Sep 2022
Edited: M on 6 Sep 2022
@Walter Roberson, I need a general eqaution for this shape so I can adjust the hight and width of it depending on my application ( I want to use it as a fuzzy memebership function). Note that there is a left straight line to the curve, what could be the best eqaution that represents shape?
@M there are a variety of equations that can do that. See
and just pick one.

Sign in to comment.

James Tursa
James Tursa on 6 Sep 2022
Edited: James Tursa on 6 Sep 2022
There are lots of functions that could fit this general shape. E.g., sin(x), or more generally A*sin(B*x+C) depending on width and height of the graph which you don't show. Maybe you could share some more details about where this shape comes from and the height and width sizes.

3 Comments

M
M on 6 Sep 2022
Edited: M on 6 Sep 2022
@James Tursa, I need a general eqaution for this shape so I can adjust the hight and width of it depending on my application ( I want to use it as a fuzzy memebership function). Note that there is a left straight line to the curve, what could be the best eqaution that represents shape?
M
M on 6 Sep 2022
Edited: M on 6 Sep 2022
@James Tursa, I think it could be sigmoid function + straight line
But cant get how can I represent it by one equation and How to control its limits!
@M there are a variety of equations that can do that. See
and just pick one.

Sign in to comment.

Asked:

M
M
on 6 Sep 2022

Edited:

on 12 Sep 2022

Community Treasure Hunt

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

Start Hunting!