0% found this document useful (0 votes)
3 views

MATLAB Code Week 4

The document provides MATLAB code for performing integration, including indefinite, definite integrals, and calculating the area between curves. It includes syntax for various MATLAB functions and examples of integration problems using sine, cosine, and exponential functions. Additionally, it outlines a method for finding the area between two curves and visualizing the results.

Uploaded by

sumedhaganguli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MATLAB Code Week 4

The document provides MATLAB code for performing integration, including indefinite, definite integrals, and calculating the area between curves. It includes syntax for various MATLAB functions and examples of integration problems using sine, cosine, and exponential functions. Additionally, it outlines a method for finding the area between two curves and visualizing the results.

Uploaded by

sumedhaganguli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MATLAB code Week 4

Integration: Indefinite, definite and Area between the curves


MATLAB Syntax used:
int(f,v) uses the symbolic object v as the
variable of integration, rather than the
variable determined by symvar
rsums(f, [a, b]) rsums(f, a, b) and rsums(f, [a,
b]) approximates the integral
for x from a to b.
fill(X,Y,C) fill(X,Y,C) creates filled polygons from
the data in X and Y with vertex color
specified by C.
char(X) converts array X of nonnegative integer
codes into a character array.

A. Integration

i) Inbuilt MATLAB function:


syms x
f=input('enter the function f(x):');
a=input('enter lower limit of x ');
b=input('enter the upper limit of x');
n=input('number of intervals');
z=int(f,a,b) % direct evaluation

ii) As a sum of rectangles by using rsums command :

→Initialization:
value = 0;
dx = (b-a)/n;

→ sum of the function values at all the right points


for k=1:n
c = a+k*dx;
d=subs(f,x,c);
value = value + d;
end
→ value of the sum* length of the sub interval is the approx. value of the integral
ivalue = dx*value
ezplot(f,[a b])

→ Taking mid point function values


rsums(f, a, b)

Problems:
1) Sin(x) in [0, 2 pi]

Output
enter the function f(x):sin(x)
enter lower limit of x 0
enter the upper limit of x2*pi
number of intervals10

z =

value =

-1.5389e-016

z =

Figure Window:

2) Cos(x) in [-pi/2, pi/2]

Output

enter the function f(x):


cos(x)
enter lower limit of x
-pi/2
enter the upper limit of x
pi/2
number of intervals
10

z=

2
value =

(pi*((2^(1/2)*(5 - 5^(1/2))^(1/2))/2 + 5^(1/2) + (2^(1/2)*(5^(1/2) + 5)^(1/2))/2 + 1))/10

z=

Figure Window:

3) e^x+tan(x)

Output

enter the function f(x):


exp(x)+tan(x)
enter lower limit of x
0
enter the upper limit of x
pi/4
number of intervals
10

z=

exp(pi/4) + log(2)/2 - 1

value =
(pi*(exp(pi/4) + exp(pi/5) + exp(pi/8) + exp(pi/10) + exp(pi/20) + exp((3*pi)/20) + exp(pi/40) + exp((3*pi)/40) +
exp((7*pi)/40) + exp((9*pi)/40) + tan(pi/20) + tan((3*pi)/20) + tan(pi/40) + tan((3*pi)/40) + tan((7*pi)/40) + tan((9*pi)/40) +
(5^(1/2)*(5 - 2*5^(1/2))^(1/2))/5 + 2^(1/2) + (5 - 2*5^(1/2))^(1/2)))/40

z=

exp(pi/4) + log(2)/2 - 1

B. Area between the curves:

clc
clear all
close all
syms x
y1=input('ENTER THE Y1 REGION VALUE');
y2=input('ENTER THE Y2 REGION VALUE');
t=solve(y1-y2); %(Y1-Y2=0)
po=double(t)
poi=sort(po)
n=length(poi)
m1=min(poi)
m2=max(poi)

ez1=ezplot(y1,[m1-1,m2+1])
hold on
TA=0
ez2=ezplot(y2,[m1-1,m2+1])
if n>2
for i=1:n-1
A=int(y1-y2,poi(i),poi(i+1))
TA= TA+abs(A)
x1 = linspace(poi(i),poi(i+1))
yy1 =subs(y1,x,x1)
yy2 = subs(y2,x,x1)

%iii) Creating a polygon:

xx = [x1,fliplr(x1)]
yy = [yy1,fliplr(yy2)]
fill(xx,yy,'g')
grid on
end
else

A=int(y1-y2,poi(1),poi(2))
TA=abs(A)
x1 = linspace(poi(1),poi(2));
yy1 =subs(y1,x,x1)
yy2 = subs(y2,x,x1)
xx = [x1,fliplr(x1)]
yy = [yy1,fliplr(yy2)]
fill(xx,yy,'g')
end

Problems:
4) Find the area of the regions enclosed by the curves, 𝑦 = 𝑥 2 − 2𝑥, 𝑦 = 𝑥

Output

ENTER THE Y1 REGION VALUE


x
ENTER THE Y2 REGION VALUE
x^2-2*x

f=

9/2

kokler =

0
3

f=
9/2
5) Find the area of the regions enclosed by the curves 𝑦 = 𝑥4 − 4𝑥2 + 4, 𝑦 = 𝑥2

Output:
ASSESSMENT-03

1. Choose any two curves of your choice such that they have multiple intersection. Then find and
visualize the closed area between them.

You might also like