Matlab Pesentation2 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Matlab

A L I G H A MA RTA L A

Engineering Analysis
Instructor: Dr. Jagath Nikapitiya

Faculty of Engineering and Applied Science


Memorial University of Newfoundland
November 2019
Some notes about solving numerical problem:
1-Write script instead of coding in command window.
2-Enter all available data first, Note: if you write any parameter
in the following equation without defining it before, you will get
an error.
3- Wirte equations orderly. It means if the result of equation 1
will be used in equaiton 2, the eqaution 1 one should come first.
4- If a specific parameter define more than one time, the value
will update each time.
5- Matlab is sensitive to capital and small letter

2
Modified Bessel functions
• Modified Bessel function K ( Z ) besselk(nu,Z)
x=0:0.01:5;
y0=besselk(0,x);
y1=besselk(1,x);
y2=besselk(2,x);
plot(x,y0)
hold on
plot(x,y1)
hold on
plot(x,y2)
axis([0 5 0 8])
legend('k_0', 'k_1', 'k_2')
xlabel('X')
ylabel('Bessel function')
title('Plot of Comparing different Modified Bessel function')

3
Modified Bessel functions
• Modified Bessel function I ( Z ) besseli(nu,Z)
x=0:0.01:5;
y0=besseli(0,x);
y1=besseli(1,x);
y2=besseli(2,x);
plot(x,y0)
hold on
plot(x,y1)
hold on
plot(x,y2)
axis([0 5 0 8])
legend('I_0', 'I_1', 'I_2')
xlabel('X')
ylabel('Bessel function')
title('Plot of Comparing different Modified Bessel function')

4
Elliptic Integral
Find Complete Elliptic Integrals of First kind
M = 0.5;
ellipticK(M)
---Result
1.8541 => the first kind
Find Complete Elliptic Integrals of second kind
M = 0.5;
ellipticE(M)
---Result
1.3506 => the second kind
Find Complete Elliptic Integrals of First and Second Kind
M = 0.5;
[K,E] = ellipke(M)
--- Result
K = 1.8541 => the first kind
E = 1.3506 => the second kind

5
Analytical Calculation

6
Analytical Calculation

7
Analytical Calculation
Function Example
𝑑𝑓
diff(function, variable) 𝑑𝑥
⇒ diff(f, x)
+𝑏
int(function, variable, lower bound, upper bound) ‫׬‬−𝑏 𝑓 𝑑𝑥 ⇒ int(f, x, -b, +b)
symsum(function, variable, lower bond, upper bond ) σ3𝑛=0 𝑓(𝑛, 𝑥, … ) = symsum(f, n, 0, 3)
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(function) • simplify(exp(c*log(sqrt(alpha+beta)))) ⇒ (alpha + beta)^(c/2).
• simplify(sqrt(x^2)) ⇒ sqrt(x^2),
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)

8
Example
Q = ( x 2 − a 2 )  ( y 2 − b 2 )  (a1 + a2  ( x 2 + y 2 ) + a3  x 2 y 2
Q  Q   Q 
2 2

f =  +  + 2cQ
x  x   y 
syms x y a b a1 a2 a3 c X Y n
Q = (x^2-a^2)*(y^2-b^2)*(a1+a2*(x^2+y^2)+a3*x^2*y^2);
Diff_x = diff(Q,x);
(a^2 - x^2)*(b^2 - y^2)*(2*a3*x*y^2 + 2*a2*x) - 2*x*(b^2 - y^2)*(a1 + a2*(x^2 + y^2) ….
f= (Diff_x)^2 + (diff(Q,y))^2 + 2*c*Q;
(2*y*(a^2 - x^2)*(a1 + a2*(x^2 + y^2) + a3*x^2*y^2) - (a^2 - x^2)*(b^2 - y^2)*(2*a3*y*x^2 +…
a b
h= 
− a −b
f yx

h=int(int(f,y,-b,b),x,-a,a);
(128*a^9*a2^2*b^3)/945 + (256*a^9*a2*a3*b^5)/4725 + (1408*a^9*a3^2*b^7)/33075 +...

9
Example
Q = ( x 2 − a 2 )  ( y 2 − b 2 )  (a1 + a 2  ( x 2 + y 2 ) + a3  x 2 y 2
h
S= → solve for a1
a1
k=diff(h,a1);
S= solve(k,a1);
-(8*a3*a^4*b^2 + 40*a2*a^4 + 8*a3*a^2*b^4 + 112*a2*a^2*b^2 + 40*a2*b^4 ....
h
T=
a2 a1 = S

Subs_a1_2 = subs(diff(h,a2),a1,S);
(256*a^3*a2*b^9)/945 + (3328*a^5*a2*b^7)/1575 + (3328*a^7*a2*b^5)/1575 + ...

10
Example
Q = ( x 2 − a 2 )  ( y 2 − b 2 )  (a1 + a 2  ( x 2 + y 2 ) + a3  x 2 y 2

3
−1n  cosh( x)  cos( y ) (2n + 1)
 =
( 2n + 1)  3 cosh( a)
3
n =0 2b

Landa = (2*n+1)*pi/(2*b);
Part = symsum(((-1)^n*cosh(Landa*x)*cos(Landa*y))/((2*n+1)^3*pi^3*cosh(Landa*a)) , n , 0 , 3);

(281474976710656*cosh((pi*x)/(2*b))*cos((pi*y)/(2*b)))/(8727491006471547*cosh((pi*a)/(2*b)))
-…

11
Example
QQ3 =(5540625*(- 4*X^2*b^2 + 4*b^2)*(- Y^2*b^2 + b^2)*((44003*c)/(394000*b^2) +
(1281*c*(4*X^2*b^2 + Y^2*b^2))/(78800*b^4) +
(189*X^2*Y^2*c)/(24625*b^2)))/(1263224*b^2*c)

A= simplify(QQ3)
(225*(X^2 - 1)*(Y^2 - 1)*(3024*X^2*Y^2 + 25620*X^2 + 6405*Y^2 + 44003))/5052896

B=expand(A)
(42525*X^4*Y^4)/315806 + (4725*X^4*Y^2)/4696 - (1441125*X^4)/1263224 +
(760725*X^2*Y^4)/5052896 + (1687725*X^2*Y^2)/2526448 - (4136175*X^2)/5052896 -
(1441125*Y^4)/5052896 - (4229775*Y^2)/2526448 + 9900675/5052896

12
Solve Equations

13
Solve by “fsolve”
• 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=

2.4048 5.5201 8.6537 11.7915 14.9309

14
Solve by code

Q:Calculate epsilon if B/A=44999.58. … *fsolve cannot solve this


if B_A>=B_A_assume problem!!!
Solution: fsolve stopped because the
B_A=input('please input B/A='); m = (2*E/pi/e^2)^(1/3); relative size of the current step
B_A=round(B_A,3); n = (2/pi*e*E)^(1/3); is less than the value of the
%Evaluate Equation for 0<e<1 step size tolerance squared
e=0; a = m*2.65*10^-4; and the vector of function
while e<1 b = n*2.65*10^-4; values is near zero as
e=e+.00001; measured by the value of the
e_prime = (1-e^2)^0.5; A = pi*a*b; function tolerance.
[K,E] = ellipke(e_prime); break
B_A_assume=round((1/e^2*E-K)/(K-E),3); end
… end
disp('e=');disp(e)
15
Solve two equality simultaneously
• J ( Z ) = besselj(nu,Z)
• Y ( Z ) = bessely(nu,Z)

Example: Find c1 and c2: Matlab format:


c1*besselj(1, 1) + c2*bessely(1, 1) = 1
c1 J (1) + c2Y (1) = 1 c1*besselj(1, 2) + c2*bessely(1, 2) = 2
c1 J (2) + c2Y (2) = 2
Use Matlab :
besselj(1, 1) = 0.4401, bessely(1, 1) = -0.7812
besselj(1, 2) = 0.5767, bessely(1, 2) = -0.1070
Solve manually:
c1*0.4401 + c2*(-0.7812)= 1

c1*0.5767 + c2*(-0.1070)= 2 OR
16
Solve two equality simultaneously
Question: Calculate ‘u’ and ‘v’ Question: Calculate ‘c1’ and ‘c2’
2*u + v = 0
u-v=1 funcs=[c1*besselj(1, 1) + c2*bessely(1, 1) == 1,
syms u v c1*besselj(1, 2) + c2*bessely(1, 2) == 2];

funcs = [2*u + v == 0, u - v == 1]; S = solve(funcs,[c1 c2])


S = solve(funcs,[u v]) c1= double(c1)
------- c2= double(c2)
>>S.u => 1/3
>>S.v => -2/3

17
18

You might also like