Calc. Exp 1a and 1b
Calc. Exp 1a and 1b
Calc. Exp 1a and 1b
1. Using MATLAB find the tangent to the curves y=√ x at x=4 at show graphically.
Solution:
The tangent to the line of the curve y=f(x) at the point P (a, f(a)) is the line
f ( a+h ) −f ( a)
through P with the slope m=f’(a)=lim
h→ 0 h
The following code illustrate the plotting of the tangent to the curve y= √ x at the
point x=4.
clear all
clc
syms x y
f = input (‘Enter the given function in variable x:’);
x0 = input (‘Enter the x-coordinate of the point:’);
y0 = subs (f, x, x0);
fx = diff (f, x);
m = subs (fx, x, x0);
tangent = y0+m*(x-x0);
t_line = y-tangent;
plotrange = [x0-3, x0+3];
ezplot (f, plotrange);
hold on;
ezplot (tangent, plotrange)
title (‘Tangent line plot’)
t = sprintf (‘The tangent to the curve y=%s at (%d, %d) is y=%s’, f, x0, y0, tangent’);
disp (t)
3 4
3. Verify Rolle’s theorem for the function (x +2) (x −3) in the interval [2,3]. Plot
the curve along with the secant joining the end points and the tangents at points
which satisfy Rolle’s theorem.
Solution:
Rolle’s theorem: Suppose that the function y f (x) is continuous at every
point of the closed interval [a, b] and differentiable at every point in (a, b) and if
f (a) f (b), then there is at least one number c in (a, b) at which f (c) 0.
The below code illustrates the verification of Rolle’s theorem for the function
3 4
f(x)=(x +2) (x −3) on the interval [-2, 3].
clear all;
clc;
syms x c;
f=input ('Enter the function: ');
I=input ('Enter the interval [a, b]: ');
a=I (1); b=I (2);
fa=subs (f, x, a); fb=subs (f, x, b);
df=diff (f, x); dfc=subs (df, x, c);
if fa==fb
c=solve(dfc);
count=0;
for i=1: length(c)
if c(i)>a && c(i)
count=count+1;
r(count)=c(i);
end
end
values=sprintf ('The values of c between %d and %d which satisfy Rolle’s theorem are x=', a, b);
disp(values)
disp(r)
else
disp('f(a) and f(b) are not equal, function does not satisfy conditions for Rolle’s theorem');
end
tval=subs (f, x, r);
xval=linspace (a, b, 100);
yval=subs (f, x, xval);
plot (xval, yval);
[p, q] =size(xval);
for i=1: length(tval)
hold on;
plot (xval, tval(i)*ones (p, q),'r');
hold on;
plot(r(i), tval(i),'ok');
end
hold off;
legend ('Function', 'Tangent');
title ('Demonstration of Rolles theorem');
EXPERIMENT 1-B
1. Find the local and global maxima and minima for the function x 3-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')
x
2. Find the global extrema of the function f(x)= x 2 e sinx - 3 on the interval [0,5].
x +1
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));
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')