Notes
![]() ![]() Notes - notes.io |
increment 0.01
PROGRAM
clc
close all
clear all
t = 0:0.01:10;
a = sin(t);
b = cos(t);
c = sinh(t);
d = cosh(t);
subplot(2,2,1)
plot(t,a)
title('sin t')
subplot(2,2,2)
plot(t,b)
title('cos t')
subplot(2,2,3)
plot(t,c)
title('sinh t')
subplot(2,2,4)
plot(t,d)
title('cosh t')
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
OUTPUT
2. Compute the first and second derivatives of sin t, cos t, sinh t, cosh t functions using built in
tools such as grad and plot the derivatives over the respective functions for the vector
t = [-5, 5] with increment 0:01
PROGRAM
clc
close all
clear all
t=-5:0.01:5;
a=sin (t);
subplot(3,1,1)
plot(t,a)
title('sin t')
b=cos (t);
c= sinh (t);
d=cosh (t);
da=gradient(a);
subplot(3,1,2)
plot(t,da)
title('first derivative of sin t')
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
d2a=gradient(da);
subplot(3,1,3)
plot(t,d2a)
title('second derivative of sin t')
figure
subplot(3,1,1)
plot(t,b)
title('cos t')
db=gradient(b);
subplot(3,1,2)
plot(t,db)
title('first derivative of cos t')
d2b=gradient(db);
subplot(3,1,3)
plot(t,d2b)
title('second derivative of cos t')
figure
subplot(3,1,1)
plot(t,c)
title('sinh t')
dc=gradient(c);
subplot(3,1,2)
plot(t,dc)
title('first derivative of sinh t')
d2c=gradient(dc);
subplot(3,1,3)
plot(t,d2c)
title('second derivative of sinh t')
figure
subplot(3,1,1)
plot(t,d)
title('cosh t')
dd=gradient(d);
subplot(3,1,2)
plot(t,dd)
title('first derivative of cosh t')
d2d=gradient(dd);
subplot(3,1,3)
plot(t,d2d)
title('second derivative of cosh t')
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
OUTPUT
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
3. Find the derivative of following functions.
(a) f = sin (5 ∗ x)
(b) g = exp(x) ∗ cos (x)
PROGRAM
(a)
clc
close all
clear all
syms x
f = sin(5*x);
y = diff(f) %to take the first derivative of f
%can also use gradient(f) instead of diff(f)
vpa(subs(f,x,2)) %find the value of f at x = 2
vpa(subs(y,x,2)) %find the derivative of f at x = 2
(b)
clc
close all
clear all
syms x
g = exp(x)*cos(x);
y = diff(g) %take the first derivative of g
vpa(subs(y,x,4)) %derivative of g for a given value of x=4
z = diff(g,2) %take the second derivative of g
%You can get the same result by taking the derivative twice:
diff(diff(g))
vpa(subs(z,x,1)) %second derivative of g for a value of x=1
OUTPUT
(a)
ans = 5*cos(5*x)
ans = -0.54402111088936981340474766185138
ans = -4.1953576453822622612943197391203
(b)
y = exp(x)*cos(x) - exp(x)*sin(x)
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
ans = 5.6322837041610723980357343265599
z = -2*exp(x)*sin(x)
ans = -4.574710574357684782416343813401
4. Compute d
dt sin(st) ,
d
ds sin(st) and d
2
dt2
sin (st)
PROGRAM
%Derivatives of expressions with several variables
clc
close all
clear all
syms s t
f = sin(s*t);
y = diff(f,t) %calculates the partial derivative ∂f/∂t
diff(f,s) %%calculates the partial derivative ∂f/∂s
diff(f, t, 2) %calculate the partial derivative ∂2f/∂t2
vpa(subs(y,[s,t],[2,1])) %find the ∂f/∂t for s=2 and t=1
OUTPUT
ans = s*cos(s*t)
ans = t*cos(s*t)
ans = -s^2*sin(s*t)
ans = -0.83229367309428477399513645900152
5. Realize the function f(t) = 4t
2 + 3 and plot it for the vector [-5,5] with increment 0.01
PROGRAM
clc
close all
clear all
t=-5:0.01:5;
y=4*(t.^2)+3;
plot(t,y,'k','linewidth',2)
title('Realization of f(t)')
xlabel('t')
ylabel('f(t)')
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
OUTPUT
6. Familiarize the general integration tools such as int and integral.
Compute following integrations.
(a) ∫
−2x
(1+x
2)
2
dx (b) ∫
1
1+u2
du (c) ∫ e
(−x
2) + log(x
2
) dx
(d) ∫
x
1+z
2
dx (e) ∫ sin (aθ + b)
(f) ∫ exp(−x
2
) ∗ log (x
2
)
∞
0
dx (g) ∫
1
x
3−2x−c
2
0
dx given c = 5
(h) ∫ log(x) dx 1
0
(i) ∫ e
−x
2
dx 1
0
(j) ∫ x
n dx given n = 2
1
0
(k) ∫ sin (aθ + b)
π
2
0
da given θ = 90 and b = 5
(l) ∫ sin (aθ + b)
π
0
dθ given a = 1 and b = 2
PROGRAM
%Indefinite Integral of Univariate Expression
(a)
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
clc
close all
clear all
syms x
f = -2*x/(1+x^2)^2;
F = int(f)
vpa(subs(F,x,1)) %to get function value for x=1
(b)
syms u
f = 1/(1+u^2);
int(f)
(c)
syms x
f = exp(-x^2)*log(x)^2;
int(f)
%Indefinite Integrals of Multivariate Function
(d)
clc
close all
clear all
syms x z
f = x/(1+z^2);
Fx = int(f,x) %integral with respect to the variable x
Fz = int(f,z) %integral with respect to the variable z
(e)
syms a b theta
f = sin(a*theta+b);
Fa = int(f,a) %integral with respect to the variable a
Fb = int(f,b) %integral with respect to the variable b
Ft = int(f,theta) %integral with respect to the variable theta
%Definite Integral
(f)
clc
close all
clear all
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
f = @(x) exp(-x.^2).*log(x.^2);
q = integral(f,0,Inf)
(g)
f = @(x,c) 1./(x.^3-2*x-c);
f(4,6) % to get function value for given x=4 & c=6
q = integral(@(x) f(x,5),0,2)
(h)
f = @(x)log(x);
q = integral(f,0,1)
(i)
f = @(x)exp(-x.^2);
q = integral(f,0,1)
(j)
f = @(x,n)x.^n;
q = integral(@(x)f(x,2),0,1)
(k)
f = @(a,theta,b)sin(a*theta+b);
q = integral(@(a) f(a,90,45),0,pi/2)
(l)
f = @(a,theta,b)sin(a*theta+b);
q = integral(@(theta) f(1,theta,2),0,pi)
OUTPUT
(a)
F = 1/(x^2 + 1)
ans = 0.5
(b)
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
ans = atan(u)
(c)
ans = int(exp(-x^2)*log(x)^2, x)
(d)
Fx = x^2/(2*(z^2 + 1))
Fz = x*atan(z)
(e)
Fa = -cos(b + a*theta)/theta
Fb = -cos(b + a*theta)
Ft = -cos(b + a*theta)/a
(f)
q = -1.7401
(g)
ans = 0.0200
q = -0.4605
(h)
q = -1.0000
(i)
q = 0.7468
(j)
q = 0.3333
(k)
q = 0.0117
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
(l)
q = -0.8323
7. Use general integration tool to compute ∫ (4t
2 + 3)
2
−2
dt
Repeat the above steps with trapezoidal and Simpson method and compare the results.
PROGRAM
%General integration tool
clc;
clear all;
close all;
f = @(t)4*t.^2+2;
integral(f,-2,2)
%Numerical integration tool: Trapezoidal Rule
f=@(t)4*t^2+2;
a = input('Enter lower limit a: ');
b = input('Enter upper limit b: ');
n = input('Enter the no. of sub interval: ');
h = (b-a)/n;
sum = 0;
for k = 1:1:n-1
t = a+k*h;
y = f(t);
sum = sum+y;
end
result = h/2*(f(a)+f(b)+2*sum);
fprintf('n The value of integration is %f',result);
%Trapezoidal Rule using function
t = -2:0.4:2;
f = 4*t.^2+2;
trapz(t,f)
%Numerical integration tool: Simpson’s 1/3 Rule
syms x
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
f = @(t)4*t^2+2;
a = input('Enter lower limit a: ');
b = input('Enter upper limit b: ');
n = input('Enter the no. of sub interval: ');
h = (b-a)/n;
odd = 0;
even = 0;
for k = 1:1:n-1
t = a+k*h;
y(k) = f(t);
end
for k = 1:1:n-1
if rem(k,2) == 1
odd = odd + y(k);%sum of odd terms
else
even = even + y(k); %sum of even terms
end
end
result = (h/3)*(f(a)+f(b)+4*odd+2*even);
fprintf('n The value of integration is %f',result);
%Numerical integration tool: Simpson’s 3/8 Rule
syms x
f = @(t)4*t^2+2;
a = input('Enter lower limit a: ');
b = input('Enter upper limit b: ');
n = input('Enter the no. of sub interval: ');
h = (b-a)/n;
sum1 = 0;
sum2 = 0;
for k = 1:1:n-1
t = a+k*h;
y(k) = f(t);
end
for k = 1:1:n-1
if rem(k,3) == 0
sum1 = sum1 + y(k);%sum of f(a+3h), f(a+6h),....
else
sum2 = sum2 + y(k); %sum of f(a+h),f(a+2h),f(a+4h),.....
end
end
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
result = (3*h/8)*(f(a)+f(b)+3*sum2+2*sum1);
fprintf('n The value of integration is %f',result);
OUTPUT
%General integration tool
ans = 29.3333
%Numerical integration tool: Trapezoidal Rule
Enter lower limit a: -2
Enter upper limit b: 2
Enter the no. of sub interval: 10
The value of integration is 29.760000
%Trapezoidal Rule using function
ans = 29.7600
%Numerical integration tool: Simpson’s 1/3 Rule
Enter lower limit a: -2
Enter upper limit b: 2
Enter the no. of sub interval: 10
The value of integration is 29.333333
%Numerical integration tool: Simpson’s 3/8 Rule
Enter lower limit a: -2
Enter upper limit b: 2
Enter the no. of sub interval: 10
The value of integration is 27.864000
8. Use general integration tool to compute 1
√2π
∫ e
−x
2
2 ∞
0
dx
Repeat the above steps with trapezoidal and Simpson method and compare the results.
PROGRAM
%General integration tool
clc;
clear all;
close all;
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
f = @(x)1/sqrt(2*pi)*exp(-x.^2/2)
integral(f,0,inf)
%Numerical integration tool: Trapezoidal Rule
f = @(x)1/sqrt(2*pi)*exp(-x.^2/2)
a = input('Enter lower limit a: ');
b = input('Enter upper limit b: ');
n = input('Enter the no. of sub interval: ');
h = (b-a)/n;
sum = 0;
for k = 1:1:n-1
x = a+k*h;
y = f(x);
sum = sum+y;
end
result = h/2*(f(a)+f(b)+2*sum);
fprintf('n The value of integration is %f',result);
%Trapezoidal Rule using function
x = 0:0.5:9999;
f = 1/sqrt(2*pi)*exp(-x.^2/2);
q = trapz(x,f)
%Numerical integration tool: Simpson’s 1/3 Rule
syms x
f = @(x)1/sqrt(2*pi)*exp(-x.^2/2)
a = input('Enter lower limit a: ');
b = input('Enter upper limit b: ');
n = input('Enter the no. of sub interval: ');
h = (b-a)/n;
odd = 0;
even = 0;
for k = 1:1:n-1
x = a+k*h;
y(k) = f(x);
end
for k = 1:1:n-1
if rem(k,2) == 1
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
odd = odd + y(k);%sum of odd terms
else
even = even + y(k); %sum of even terms
end
end
result = (h/3)*(f(a)+f(b)+4*odd+2*even);
fprintf('n The value of integration is %f',result);
%Numerical integration tool: Simpson’s 3/8 Rule
syms x
f = @(x)1/sqrt(2*pi)*exp(-x.^2/2)
a = input('Enter lower limit a: ');
b = input('Enter upper limit b: ');
n = input('Enter the no. of sub interval: ');
h = (b-a)/n;
sum1 = 0;
sum2 = 0;
for k = 1:1:n-1
x = a+k*h;
y(k) = f(x);
end
for k = 1:1:n-1
if rem(k,3) == 0
sum1 = sum1 + y(k);%sum of f(a+3h), f(a+6h),....
else
sum2 = sum2 + y(k); %sum of f(a+h),f(a+2h),f(a+4h),.....
end
end
result = (3*h/8)*(f(a)+f(b)+3*sum2+2*sum1);
fprintf('n The value of integration is %f',result);
OUTPUT
%General integration tool
ans = 0.5000
%Numerical integration tool: Trapezoidal Rule
Enter lower limit a: 0
Enter upper limit b: 10000
Enter the no. of sub interval: 20000
ECL 201 Scientific Computing Laboratory 2021-2022
Dept. Of Electronics and Communication Engineering
The value of integration is 0.500000
%Trapezoidal Rule using function
ans = 0.5000
%Numerical integration tool: Simpson’s 1/3 Rule
Enter lower limit a: 0
Enter upper limit b: 10000
Enter the no. of sub interval: 20000
The value of integration is 0.500000
%Numerical integration tool: Simpson’s 3/8 Rule
Enter lower limit a: 0
Enter upper limit b: 10000
Enter the no. of sub interval: 20000
The value of integration is 0.499981
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team