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)
Show older comments
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;
0 Comments
Answers (1)
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=')
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;
See Also
Categories
Find more on Large Files and Big Data 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!