0% found this document useful (0 votes)
109 views6 pages

Experiment-1-B E-Example - 1 Date:13-10-2020: %discretizing The Interval I %finding The Values of F (X) at T Values

The document contains code that uses MATLAB to find the local and global maxima and minima for three different functions over given intervals. For each function f(x), the code discretizes the interval, calculates f(x) at the discrete points, identifies the local maxima and minima using findpeaks, and plots the function along with indicators of the extrema values.

Uploaded by

Akshat Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views6 pages

Experiment-1-B E-Example - 1 Date:13-10-2020: %discretizing The Interval I %finding The Values of F (X) at T Values

The document contains code that uses MATLAB to find the local and global maxima and minima for three different functions over given intervals. For each function f(x), the code discretizes the interval, calculates f(x) at the discrete points, identifies the local maxima and minima using findpeaks, and plots the function along with indicators of the extrema values.

Uploaded by

Akshat Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT-1-B

E-Example – 1
Date:13-10-2020

Question(Aim): ): Using matlab find the local and global maxima and minima for the
function sin|x| on x(2 π ,2 π ).
MATLAB command:
clear
clc
syms x
f(x)=sin(abs(x));
I=[-2*pi,2*pi];
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),4);
h=double(f1(t));
[lmin_f,loc]=findpeaks(h);
lmin_x=round(t(loc),4);
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 maxima on the curve of
f(x)
plot(lmin_x,double(f(lmin_x)),'*g');%Pointing the local minima on the curve of
f(x)
hold off

Input: Code itself


Output:
Local maximum occur at x=
-1.5703 1.5703

The Local Maximum value(s) of the function are


1.0000 1.0000

Local minimum occur at x=


-4.7122 -0.0006 4.7122

The Local Minimum value(s) of the function are


-1.0000 0.0006 -1.0000
E-Record – 1
Date:20-10-2020

Question(Aim): Using matlab find the local and global maxima and minima for the function
x3-12x-5 on x(4,4).
MATLAB command:
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),4);
h=double(f1(t));
[lmin_f,loc]=findpeaks(h);
lmin_x=round(t(loc),4);
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 maxima on the curve of
f(x)
plot(lmin_x,double(f(lmin_x)),'*g');%Pointing the local minima on the curve of
f(x)
hold off

Input: Code itself


Output:
Local maximum occur at x=
-1.9998

The Local Maximum value(s) of the function are


11.0000

Local minimum occur at x=


1.9998

The Local Minimum value(s) of the function are


-21.0000

E-Record – 2
Date:20-10-2020

Question(Aim):Using matlab find the local and global maxima and minima for the function
xsin 2x on x(5,5)
MATLAB command:
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),4);
h=double(f1(t));
[lmin_f,loc]=findpeaks(h);
lmin_x=round(t(loc),4);
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 maxima on the curve of
f(x)
plot(lmin_x,double(f(lmin_x)),'*g');%Pointing the local minima on the curve of
f(x)
hold off

Input: Code itself


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