Find the local and global maxima and minima for the function (x+1)^2/(1+x^2) x belongs to [-5,5].Showing local min value something different than zero and how get local values

20 views (last 30 days)
clear
clc
syms x
f(x)=(x+1)^2/(1+x^2);
I=[-5,5];
fl(x)=-f(x);
a=I(1);b=I(2);
t=linspace(a,b,10000);
g=double(f(t));
[lmax_f,loc]=findpeaks(g);
lmax_x=round(t(loc),4);
h=double(fl(t));
[lmin_f,loc]=findpeaks(h);
lmin_x=round(t(loc),4);
disp('global maximum occurs at x=')
disp(lmax_x)
disp('the local maximum value(s) of the function are')
disp(double(f(lmax_x)))
disp('global minimum occurs at x=')
disp(lmin_x)
disp('the local minimum value(s) of the function are')
disp(double(f(lmin_x)))
plot(t,f(t));hold on;
plot(lmax_x,double(f(lmax_x)),'or');
plot(lmin_x,double(f(lmin_x)),'*g');
hold off;

Answers (1)

Sam Chak
Sam Chak on 29 Oct 2022
@Aashay, your code works. So, what exactly is the question for the math problem?
syms x
f(x)=(x+1)^2/(1+x^2);
I=[-5,5];
fl(x)=-f(x);
a=I(1);b=I(2);
t=linspace(a,b,10000);
g=double(f(t));
[lmax_f,loc]=findpeaks(g);
lmax_x=round(t(loc),4);
h=double(fl(t));
[lmin_f,loc]=findpeaks(h);
lmin_x=round(t(loc),4);
disp('global maximum occurs at x=')
global maximum occurs at x=
disp(lmax_x)
0.9996
disp('the local maximum value(s) of the function are')
the local maximum value(s) of the function are
disp(double(f(lmax_x)))
2.0000
disp('global minimum occurs at x=')
global minimum occurs at x=
disp(lmin_x)
-0.9996
disp('the local minimum value(s) of the function are')
the local minimum value(s) of the function are
disp(double(f(lmin_x)))
8.0032e-08
plot(t,f(t));hold on;
plot(lmax_x,double(f(lmax_x)),'or');
plot(lmin_x,double(f(lmin_x)),'*g');
hold off;

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!