21bec2166 Matlab Experiment Da1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

MATLAB EXPERIMENT

NAME: Anand Kholwadiya

REG.NO.: 21BEC2166

SLOT: L9+L10

Experiment – 1A
1.
AIM:
Find the local and global maxima and minima for
the function x3-12x-5 on x  (-4,4).

MATLAB 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);
g=double(f(t));
[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)));
maxi=max(g);
mini=min(g);
disp('The global maxima of the function is')
disp(maxi);
disp('The global minima of the function is')
disp(mini);
plot(t,f(t));
hold on;
plot(lmax_x,double(f(lmax_x)),'--^b');
plot(lmin_x,double(f(lmin_x)),'--vg');
hold off

INTPUT:
f(x) = x^3 - 12*x - 5

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
"The global maximum is:" "11"
"The global minima is:" "-21"
VISUAL OUTPUT:
2.
AIM: Find the local and global maxima and
minima for the function x+sin2x on x  (-5,5).
MATLAB 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 tvalues
[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)));
maxi=max(g);
mini=min(g);
disp('The global maxima of the function is')
disp(maxi);
disp('The global minima of the function is')
disp(mini);
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
INTPUT:
f(x) = x + sin(2*x)

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

The global maxima of the function is


5.0548

The global minima of the function is


-5.0548
VISUAL OUTPUT:
Experiment – 1B
1.
AIM: To find the area of the region bounded
by the curve 𝑦 = 𝑥 2 − 2𝑥 and the line 𝑦 = 𝑥.

MATLAB CODE:
Clear all
clc
syms x
f=input('Enter the upper curve f(x): ');
g=input('Enter the lower curve g(x): ');
L=input('Enter the limits of integration for x [a,b]:');
a=L(1);
b=L(2);
Area=int(f-g,x,a,b);
disp(['Area bounded by the curves f(x) and g(x) is: ',char(Area)]);
x1=linspace(a,b,20);
y1=subs(f,x,x1);
x2=x1;y2=subs(g,x,x1);
plot(x1,y1);
hold on;
plot(x2,y2);
hold off;
xlabel('x-axis');
ylabel('y-axis');
legend('f(x)','g(x)');grid on;
INTPUT:
Enter the upper curve f(x):
x
Enter the lower curve g(x):
x^2 - 2*x
Enter the limits of integration for x
[a,b]:[0,3]

OUTPUT:
Area bounded by the curves f(x) and g(x) is:
9/2

VISUAL OUTPUT:
2.
AIM: To find the area of the region bounded
by the curves 𝑥 = 𝑦 3 and 𝑥 = 𝑦 2.

MATLAB CODE:
clear
clc
syms y
f=input('Enter the right curve f(y): ');
g=input('Enter the left curve g(y): ');
L=input('Enter the limits of integration for y [c,d]: ');
c=L(1); d=L(2);
Area=int(f-g,y,c,d);
disp(['Area bounded by the curves f(y) and g(y) is: ',char(Area)]);
y1=linspace(c,d,20);
x1=subs(f,y,y1);
y2=y1;
x2=subs(g,y,y1);
plot(x1,y1);
hold on;
plot(x2,y2);
hold off;
xlabel('x-axis');
ylabel('y-axis');
legend('f(y)','g(y)');
grid on;

INTPUT:
Enter the right curve f(y):
y^2
Enter the left curve g(y):
y^3
Enter the limits of integration for y
[c,d]:
[0,1]
OUTPUT:
Area bounded by the curves f(y) and g(y)
is: 1/12

VISUAL OUTPUT:

3.
AIM: To find the area of the region bounded
by the curves 𝑥 = 𝑦 2, 𝑦 = 𝑥 − 2 in the first
quadrant.
MATLAB CODE:
clear all
clc
syms y
f=input('Enter the right curve f(y): ');
g=input('Enter the left curve g(y): ');
L=input('Enter the limits of integration for y [c,d]:');
c=L(1); d=L(2);
Area=int(f-g,y,c,d);
disp(['Area bounded by the curves f(y) and g(y) is:',char(Area)]);
y1=linspace(c,d,20);
x1=subs(f,y,y1);
y2=y1;
x2=subs(g,y,y1);
plot(x1,y1);
hold on;
plot(x2,y2);
hold off;
xlabel('x-axis');
ylabel('y-axis');
legend('f(y)','g(y)');
grid on;

INTPUT:
Enter the right curve f(y):
2+y
Enter the left curve g(y):
y^2
Enter the limits of integration for y [c,d]:
[0,2]

OUTPUT:
Area bounded by the curves f(y) and g(y) is:
10/3

VISUAL OUTPUT:

4.
AIM: To evaluate the area enclosed between
the curve 𝑦 = 𝑥3 − 2𝑥 + 5 and the ordinates 𝑥
= 1 and 𝑥 = 2.

MATLAB CODE:
clear all
clc
syms x
f=input('Enter the upper curve f(x): ');
L=input('Enter the limits of integration for x [a,b]:');
a=L(1);
b=L(2);
Area=int(f,x,a,b);
disp(['Area bounded by the curves f(x) and g(x) is: ',char(Area)]);
x1=linspace(a,b,20);
y1=subs(f,x,x1);
x2=x1;
y2=subs(x,x1);
plot(x1,y1);
xlabel('x-axis');
ylabel('y-axis');
legend('f(x)');
grid on;

INTPUT:
Enter the upper curve f(x):
x^3-2*x+5
Enter the limits of integration for x
[a,b]:
[1,2]

OUTPUT:
Area bounded by the curves f(x) and
g(x) is: 23/4

VISUAL OUTPUT:

You might also like