Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Registration Number : 21BCI0085

Name : Vashishta Kothamasu


Date : 3/10/2021

Experiment 1–A
Evaluating extremum of a single variable function

EXERCISE
1. Find the local and global maxima and minima for the function x^3-12x-5 , interval (-4,4)

Code :

clear
clc
syms x
f(x)=x^3-12*x-5;
I=[-4,4];
f1(x)=-f(x);
a=I(1);b=I(2);
t=linspace(a,b,10000); %Discretizing the interval I
g=double(f(t)); %Finding the values of f(x) at t values
[lmax_f,loc]=findpeaks(g);
lmax_x=round(t(loc),2);
h=double(f1(t));
[lmin_f,loc]=findpeaks(h);
lmin_x=round(t(loc),2);
disp('Local maximum occur at x=')
disp(lmax_x)
disp('The Local Maximum value(s) of the function are ')
disp(double(f(lmax_x)))
disp('Local minimum occur 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; %Plotting the function
plot(lmax_x,double(f(lmax_x)),'or');%Pointing the local
plot(lmin_x,double(f(lmin_x)),'*g');%Pointing the local
hold off

Output :
Local maximum occur at x=
-2

The Local Maximum value(s) of the function are


11

Local minimum occur at x=


2

The Local Minimum value(s) of the function are


-21
Exercise 2 :
Find the local and global maxima and minima for the function x+sin(2*x), interval (-5,5)

Code :

clear
clc
syms x
f(x)=x+sin(2*x);
I=[-5,5];
f1(x)=-f(x);
a=I(1);b=I(2);
t=linspace(a,b,10000); %Discretizing the interval I
g=double(f(t)); %Finding the values of f(x) at t values
[lmax_f,loc]=findpeaks(g);
lmax_x=round(t(loc),9);
h=double(f1(t));
[lmin_f,loc]=findpeaks(h);
lmin_x=round(t(loc),9);
disp('Local maximum occur at x=')
disp(lmax_x)
disp('The Local Maximum value(s) of the function are ')
disp(double(f(lmax_x)))
disp('Local minimum occur 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; %Plotting the function
plot(lmax_x,double(f(lmax_x)),'or');%Pointing the local
plot(lmin_x,double(f(lmin_x)),'*g');%Pointing the local
hold off

Output :

Local maximum occur at x=


-2.0947 1.0476 4.1889

The Local Maximum value(s) of the function are


-1.2284 1.9132 5.0548

Local minimum occur at x=


-4.1889 -1.0476 2.0947

The Local Minimum value(s) of the function are


-5.0548 -1.9132 1.2284

You might also like