Introduction On Matlab: Engineering Analysis Instructor: Dr. Jagath Nikapitiya
Introduction On Matlab: Engineering Analysis Instructor: Dr. Jagath Nikapitiya
A L I G H A MA RTA L A
Engineering Analysis
Instructor: Dr. Jagath Nikapitiya
2
Numerical Calculation
3
Numerical Calculation
4
Numerical Calculation
5
Numerical Calculation
6
Numerical Calculation
7
Numerical Calculation
8
Numerical Calculation
9
Numerical Calculation
10
Gamma function
• 1. fplot ⇒ fplot(@gamma)
• 2. script ⇒ x=-5:0.1:5; y=gamma(x); plot(x,y)
11
Solve equation that has gamma function
gamma(1/3)/3
Solution:
ans =
3*sqrt(pi)*gamma(2/3)/gamma(1/6)
0.892979511569249
ans =
1.2936
format long
ans =
1.293554779614895
12
Format of Numbers
13
Bessel functions
• Bessel function J ( Z ) (besselj(nu,Z))
x=0:0.1:9;
y0=besselj(0,x);
y1=besselj(1,x);
y2=besselj(2,x);
y(length(x))=0;
plot(x,y,'-.')
hold on
plot(x,y0)
hold on
plot(x,y1)
hold on
plot(x,y2)
legend('y=0','J_0','J_1','J_2')
14
Bessel functions
• Bessel function Y ( Z ) (bessely(nu, Z))
x=0:0.1:20;
y(length(x))=0;
y0=bessely(0,x);
y1=bessely(1,x);
y2=bessely(2,x);
plot(x,y,'-.’);
hold on;
plot(x,y0);
hold on;
plot(x,y1);
hold on;
plot(x,y2);
legend('y=0','Y_0','Y_1','Y_2')
axis([-0.1 20.2 -2 0.6])
15
Example
Solution:
x=0:0.1:10;y1=x.*besselj(2/3, x.^(3/2)); y2=x.*besselj(-2/3,x.^(3/2));
ploy(x,y1);
hold on
plot(x,y2);
16
Bessel function root
• fsolve:
eqn = @(x) besselj(nu, x) ;
x0 = [first guess] ;
x = fsolve(eqn,x0)
-------------------------------------------------------
eqn = @(x) besselj(0, x) ;
x0 = [2, 5, 8, 11, 14] ; OR x0=0:20
x = fsolve(eqn,x0)
Result:
x=
17
Bessel function root
eqn = @(x) besselj(1, x) ; eqn = @(x) bessely(1, x) ;
x0 = [3, 7, 10, 13, 16] x0 = [2, 5, 8, 11, 14]
x = fsolve(eqn,x0)
x = fsolve(eqn,x0) Result:
Result: x=
x= 2.1971 5.4297 8.5960 11.7492 14.8974
18
Example
eq=@(x) besselj(-1/3,x);
x0=1:10;
x=fsolve(eq,x0)
Solution:
x=
Columns 1 through 5
Columns 6 through 10
19
Loops
• while, for, if, switch and case:
20
Analytical Calculation
21
Analytical Calculation
22
Analytical Calculation
Function Example
𝑑𝑓
diff(function, variable) 𝑑𝑥
⇒ diff(f, x)
+𝑏
int(function, variable, lower bound, upper bound) −𝑏 𝑓 𝑑𝑥 ⇒ int(f, x, -b, +b)
f = a*x^2+b*x+c ⇒
solve(f, x)⇒
solve(function, variable) x1 = -(b + (b^2 - 4*a*c)^(1/2))/(2*a)
x2 = -(b - (b^2 - 4*a*c)^(1/2))/(2*a)
subs(function, variable) subs(f, x, 2) ⇒ 4*a + 2*b + c
• expand((x+1)^3) ⇒ x^3+3*x^2+3*x+1
expand(function) • expand(sin(x+y)) ⇒ sin(x)*cos(y)+cos(x)*sin(y)
• expand(exp(x+y)) => exp(x)*exp(y)
• simplify(sin(x)^2 + cos(x)^2) ⇒ 1.
• simplify(exp(c*log(sqrt(alpha+beta)))) ⇒ (alpha +
simplify(function) beta)^(c/2).
• simplify(sqrt(x^2)) ⇒ sqrt(x^2),
23
Plot
>>x=0:pi/100:2*pi;
>>y1=sin(x);
>>y2=sin(x-0.25);
>>y3=sin(x-0.5);
>>plot(x,y1,x,y2,x,y3)
>>legend(‘sin(x)’,’sin(x-0.25)’,’sin(x-0.5)’)
xlabel (‘’)
ylabel (‘’)
title(‘’)
24
Plot
>>syms x y
>>ezplot(sin(x^2)-cos(y))
25
Analytical Calculation
26
Plot
>>x=-2:0.2:2;
>>y=-2:0.2:2;
>>[x,y]=meshgrid(x,y);
>>z=x.*exp(-x.^2-y.^2);
>>mesh(z)
Or
>>surf(x,y,z)
27