How to find the maximum and minimum values of a piecewise function?
9 views (last 30 days)
Show older comments
Edgar Dominguez Ugalde
on 6 Dec 2017
Commented: Shivam Kaushik
on 7 Nov 2019
I have defined the following piecewise function:
s = piecewise(0<theta<50*pi/180, 0, 50*pi/180<theta<100*pi/180, curv_arm, 100*pi/180<theta<2*pi, curv_par);
where `curv_arm` and `curv_par` are functions that I wrote at the beginning. And I used the command fplot to display the plot of this function. Now I want to obtain the maximum and the minimum value of this function in the interval [0,pi] but I'm getting the following error:
"s" was previously used as a variable,
conflicting with its use here as the name of a function
or command
I'm trying with the following line:
[x,fval] = fminbnd(@s,0,pi)
The plot is the following:

1 Comment
Shivam Kaushik
on 7 Nov 2019
sigmoid_exact = @(x)(((-4<x) & (x<4))*1/(1+exp(-x)));
sigmoid_approx = @(x) (((0<x) & (x<4))*(1-0.5*((1-0.25*x)^(2)))+((-4<x) & (x<0))*(0.5*((1+0.25*x)^(2)))+(x>4)*1+(x<-4)*0);
error = @(x) (sigmoid_exact-sigmoid_approx) ;
how to obtain plot
piecewise non linear approximation of sigmoid function
i have to minimze error
Accepted Answer
Star Strider
on 6 Dec 2017
I’m not quite sure what you want to do. This will get you started:
curv_arm = 3;
curv_par = 5;
s = @(theta, curv_arm, curv_par) [0*((0<theta) & (theta<50*pi)) + curv_arm.*((50*pi/180<theta) & (theta<100*pi/180)) + curv_par.*((100*pi/180<theta) & (theta<2*pi))];
[x,fval] = fminbnd(@(theta)s(theta, curv_arm, curv_par),0,pi)
You cannot use a symbolic expression with fminbnd, and ‘matlabFunction’ will not create a function from your piecewise function. You can easily create your own function for numeric applications using essentially the same code, and ‘logical indexing’, as I did here.
Make any necessary changes to get the result you want.
6 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!