Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Finding Extrema of Functions using MATLAB, Lab Reports of Mathematics

The solution to finding the local and global extrema of two given functions using matlab. The matlab code to evaluate and visualize the functions and their critical points, as well as the output of the code for the critical points and extrema. The first function is x^3-12x-5 in the interval [-4,4], and the second function is x^2*exp(sin(x))-x/x^3+1 in the interval [0,5]

Typology: Lab Reports

2020/2021

Uploaded on 03/21/2022

scarletboo
scarletboo 🇮🇳

5 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EXPERIMENT 1-B
1. Find the local and global maxima and minima for the function
x3
-12x-5, x
(-4,4).
Solution:
The following MATLAB code illustrates the evaluation and visualization of global and local extrema of a
function f (x) in an interval (a, b).
clear all
clc
syms x
f = input ('Enter the function f(x):');
I = input ('Enter the interval: ');
a=I (1); b=I (2);
df = diff (f, x);
ddf = diff (df, x); f = inline(vectorize(f));
df = inline(vectorize(df));
ddf = inline(vectorize(ddf));
range = linspace (a, b,100);
plot (range, f(range),'-b','LineWidth',2);
legstr = {'Function Plot'}; % Legend String
hold on;
guesses = linspace (a, b,5);
root = zeros(size(guesses));
for i=1: numel(guesses)
root(i) = fzero (df, guesses(i));
end
root = root (a <= root & root <=b);
root = unique(round(root,4));
plot (root, f(root),'ro','MarkerSize',10);
legstr = [legstr, {'Critical Points'}];
disp (['Critical Points of f(x) are: ', num2str(root)])
maxp = root(ddf(root) < 0);
if(numel(maxp) ~= 0)
disp (['Local maximum of f(x) occurs at: ', num2str(maxp)])
end
minp = root(ddf(root) > 0);
if(numel(minp) ~= 0)
disp (['Local minimum of f(x) occurs at: ', num2str(minp)])
end
fval = f(root);
if(numel(maxp) ~= 0)
gmax = root (fval == max(fval));
disp (['Global maximum of f(x) occurs at: ', num2str(gmax),' and its value is:',
num2str(max(fval))]) plot (gmax, f(gmax),'m+','MarkerSize',10);
legstr = [legstr, {'Global Maximum'}];
end
if(numel(minp) ~= 0)
gmin = root (fval == min(fval));
disp (['Global minimum of f(x) occurs at: ', num2str(gmin),' and its value is: ',
num2str(min(fval))]) plot (gmin, f(gmin),'m*','MarkerSize',10);
legstr = [legstr, {'Global Minimum'}];
end
legend (legstr, 'Location', 'Best')
Input : Enter the function f(x): x^3-12x-5
pf3

Partial preview of the text

Download Finding Extrema of Functions using MATLAB and more Lab Reports Mathematics in PDF only on Docsity!

EXPERIMENT 1-B

1. Find the local and global maxima and minima for the function x^3 -12x-5, x ∈

Solution:

The following MATLAB code illustrates the evaluation and visualization of global and local extrema of a

function f (x) in an interval (a, b).

clear all clc syms x f = input ('Enter the function f(x):'); I = input ('Enter the interval: '); a=I (1); b=I (2); df = diff (f, x); ddf = diff (df, x); f = inline(vectorize(f)); df = inline(vectorize(df)); ddf = inline(vectorize(ddf)); range = linspace (a, b,100); plot (range, f(range),'-b','LineWidth',2); legstr = {'Function Plot'}; % Legend String hold on; guesses = linspace (a, b,5); root = zeros(size(guesses)); for i=1: numel(guesses) root(i) = fzero (df, guesses(i)); end root = root (a <= root & root <=b); root = unique(round(root,4)); plot (root, f(root),'ro','MarkerSize',10); legstr = [legstr, {'Critical Points'}]; disp (['Critical Points of f(x) are: ', num2str(root)]) maxp = root(ddf(root) < 0); if(numel(maxp) ~= 0) disp (['Local maximum of f(x) occurs at: ', num2str(maxp)]) end minp = root(ddf(root) > 0); if(numel(minp) ~= 0) disp (['Local minimum of f(x) occurs at: ', num2str(minp)]) end fval = f(root); if(numel(maxp) ~= 0) gmax = root (fval == max(fval)); disp (['Global maximum of f(x) occurs at: ', num2str(gmax),' and its value is:', num2str(max(fval))]) plot (gmax, f(gmax),'m+','MarkerSize',10); legstr = [legstr, {'Global Maximum'}]; end if(numel(minp) ~= 0) gmin = root (fval == min(fval)); disp (['Global minimum of f(x) occurs at: ', num2str(gmin),' and its value is: ', num2str(min(fval))]) plot (gmin, f(gmin),'m*','MarkerSize',10); legstr = [legstr, {'Global Minimum'}]; end legend (legstr, 'Location', 'Best')

Input : Enter the function f(x): x^3-12x-

Enter the interval: [-4,4]

Output : Critical points of f(x) are: -2,

Local maximum of f(x) occurs at: -

Local minimum of f(x) occurs at: 2

Global maximum of f(x) occurs at: -2 and its value is: 11

Global minimum of f(x) occurs at: 2 and its value is: -

2. Find the global extrema of the function f(x)=x^2 esinx- x x 3

  • 1

on the interval [0,5].

Solution:

The following MATLAB code illustrates the evaluation and visualization of global extrema of a function f (x) in

an interval (a, b).

clear all clc syms x f = input ('Enter the function f(x):'); I = input ('Enter the interval: '); a=I (1); b=I (2); df = diff (f, x); ddf = diff (df, x); f = inline(vectorize(f)); df = inline(vectorize(df)); ddf = inline(vectorize(ddf)); range = linspace (a, b,100); plot (range, f(range),'-b','LineWidth',2); legstr = {'Function Plot'}; % Legend String hold on; guesses = linspace (a, b,5); root = zeros(size(guesses)); for i=1: numel(guesses) root(i) = fzero (df, guesses(i)); end root = root (a <= root & root <=b); root = unique(round(root,4));