CM

Published on January 2018 | Categories: Documents | Downloads: 82 | Comments: 0 | Views: 677
of 17
Download PDF   Embed   Report

Comments

Content

MATLAB FILE SIDHARTH RAZDAN 04316101410 USCT 3rd YEAR

1

EXPERIMENT NO. 1

OBJECTIVE: SOLVING LINEAR EQUATION IN ONE VARIABLE METHOD USED: FZERO PROCEDURE: The equation to be solved is 4x + 3 = 0 We make a .m file, say linf1.m, defined as function y = linf1(x) y = 4*x + 3 ; In the command window we use function ‘fzero’ as follows, >> fzero ( @linf1, 0 ) where initial approximation of x is 0. RESULT: ans = -0.7500 The answer is returned in four decimal places, as default in matlab. PRECAUTIONS:   

Take care of the syntax of fzero. Defining linf1.m should be very careful. To get more precise result, use format long in the command window.

2

EXPERIMENT NO. 2

OBJECTIVE: SOLVING SYSTEM OF LINEAR EQUATIONS METHOD USED: Matrix method and FSOLVE PROCEDURE

:

The equations to solve are x+y+z=9 2x – 3y + 4z = 13 3x + 4y + 5z = 40 Two methods can be used to find the solution – matrix method, or by using function fsolve. In matrix method, the system is written in the form AX = B where, A= 1

1

1

2 -3

4

3

5

4

X= x y z and, B= 9 13 40 In the command window, we enter the component matrix A and use matrix algebra to calculate vector X as,

3

>> A= [ 1 1 1 ; 2 -3 4 ; 3 4 5 ] ; >> B= [ 9 ; 13 ; 40 ] ; >> X= A \ B If we are to use the fsolve method, we define a .m file, say linf2.m, as follws function m=linf2(p) x = p(1); y = p(2); z = p(3); m= zeros (3,1); m(1)= x + y + z – 9 ; m(2)= 2*x - 3*y + 4*z - 13 ; m(3)=3*x + 4*y + 5*z - 40 ; In the command window we use the function fsolve as follows, >> fsolve ( @linf2, [ 0 0 0 ] ) The vector [ 0 0 0 ] denotes the initial assumptions of x, y, z as 0, 0, 0 respectively. RESULT: For the matrix method, we get X= 1.0000 2.0000 3.0000 For the method using fsolve, we get ans= 1.0000

2.0000

3.0000

The result is returned in 4 decimal places, as default in matlab. PRECAUTIONS:    

Be careful in using ‘/’ instead ‘\’. Take care of the syntax of fsolve. Defining linf2.m should be very careful. To get more precise result, use format long in the command window. 4

EXPERIMENT NO. 3

OBJECTIVE: SOLVING NON-LINEAR EQUATION IN ONE VARIABLE METHOD USED: ROOTS PROCEDURE

:

The equation to be solved is x4 + 2x3 + 3x2 + 2x + 1 = 0 In the command window we use function roots as follows, >> p = [ 1 2 3 2 1 ] ; >> roots (p) ‘p’ here is defined as the vector of coefficients of powers of x in the equation in the decreasing order. RESULT: ans= -0.5000 + 0.8660i -0.5000 - 0.8660i -0.5000 + 0.8660i -0.5000 - 0.8660i As usual, the result is returned in four decimal places, default in matlab.

PRECAUTIONS:  

p can be a column or a row vector. The result would not be affected. Carefully define vector p, in ‘decreasing’ powers of x.

5

EXPERIMENT NO. 4 OBJECTIVE: SOLVING SYSTEM OF NON-LINEAR EQUATIONS METHOD USED: FSOLVE PROCEDURE

:

The system of equations to be solved is 10x + 3y2 = 3 x2 - ey =2 We create a .m file, say nonlinf2.m, as function m = nonlinf2(p) x = p(1); y = p(2); m(1) = 10*x + 3*(y^2) – 3 ; m(2) = (x^2) – exp (y) -2 ; In the command window we use function fsolve as follows, >> fsolve ( @nonlinf2, [ 0 0 ] ) Vector [ 0 0 ] denotes initial assumptions of x and y as 0 and 0 respectively. RESULT: ans= -1.4456 -2.4122 PRECAUTIONS:  

Do not use e, instead of exp. Keep in mind the syntax of function fsolve.

6

EXPERIMENT NO. 5

OBJECTIVE: SOLVING ORDINARY DIFFERENTIAL EQUATION IN ONE VARIABLE METHOD USED: ODE23 AND ODE45 PROCEDURE: Q. A stream of brine solution ( 0.8 mol/L ) is flowing into a tank at the rate of 10 L/min. At t=0, it is diluted with distilled water flowing in the tank at the rate of 15 L/min. Plot the change in outlet concentration of brine solution from the tank as a function of time, starting from t=0. Above problem is of blending. Applying mass balance, with initial outlet concentration same as that of the stream before dilution, i.e. 0.8 mol/L, we have a differential equation = -4Cr + 1.6, Cr (0) = 0.8 The RHS is denoted as f(Cr).To solve this ODE we define a .m file, say myode1.m, as function m = myode1 ( t, Cr ) m = -4*Cr + 1.6 ;

% we put m = f(Cr)

In the command we can use function ode23 or ode 45. The only difference is in the results, which are more accurate in the latter because in involves more rigorous calculation. The function, say ode45, is used as follows >> [ t Cr ] = ode45 ( @myode1, [ 0 : 60 ], 0.8 ) ; >> c = [ t Cr ] ; >> plot ( t, Cr ) >> xlabel ( ‘ Time in minutes ‘ ) >> ylabel ( ‘ Concentration in mol/L ‘ ) >> grid The vector [ 0 : 60 ] denotes set of time values at which Cr is calculated. 0.8 is the initial value of Cr. The values of t an Cr are returned in vector c.

7

RESULT: c= 0

0.8000

1.0000

0.4073

2.0000

0.4002

3.0000

0.4000

4.0000

0.3998

5.0000

0.4000

6.0000

0.4000

7.0000

0.4000

8.0000

0.3999

9.0000

0.3999

10.0000

0.4000

11.0000

0.4000

12.0000

0.4001

13.0000

0.4001

14.0000

0.4000

15.0000

0.4000

16.0000

0.4001

17.0000

0.4003

18.0000

0.4001

19.0000

0.4000

20.0000

0.4001

21.0000

0.4002

22.0000

0.4004

23.0000

0.4001

24.0000

0.4000 8

25.0000

0.4001

26.0000

0.4003

27.0000

0.4004

28.0000

0.4000

29.0000

0.4000

30.0000

0.4001

31.0000

0.4004

32.0000

0.4002

33.0000

0.4000

34.0000

0.4000

35.0000

0.4001

36.0000

0.4001

37.0000

0.3999

38.0000

0.3997

39.0000

0.4000

40.0000

0.4001

41.0000

0.4000

42.0000

0.4000

43.0000

0.4000

44.0000

0.4000

45.0000

0.4000

46.0000

0.4001

47.0000

0.4002

48.0000

0.4000

49.0000

0.4000

50.0000

0.4001

51.0000

0.4003 9

52.0000

0.4000

53.0000

0.4000

54.0000

0.4000

55.0000

0.4002

56.0000

0.4002

57.0000

0.4000

58.0000

0.3999

59.0000

0.3998

60.0000

0.4001

0.8 0.75

Concentration in mol/L

0.7 0.65 0.6 0.55 0.5 0.45 0.4 0.35

0

10

20

30 Time in minutes

40

50

60

PRECAUTIONS:   

Keep in mind the syntax of ode45. Ode23 can also be used instead of ode45. However the accuracy will be reduced. Semicolons suppress output. Hence they must be carefully used..

10

EXPERIMENT NO. 6

OBJECTIVE : SOLVING SYSTEM OF ORDINARY DIFFERENTIAL EQUATIONS METHOD USED: ODE23 AND ODE45 PROCEDURE: Q. Under appropriate conditions A decomposes as followsA→R→S with, k1 = k2 = 0.1 /min, CA0 = 1 mol/L, and CR0=CS0=0 mol/L. Plot CA, CR, and CS with respect to time in minutes. Solving above problem, we get 3 sets of ODEs

= - k1*CA = - 0.1*CA = f(CA) = k1*CA - k2*CR = 0.1*CA – 0.1*CR = f(CR) = 0.1*CR = f(CS) with CA0 = 1, and CR0=CS0=0. We define a .m file, say myode2.m as, function m = myode2 ( t, y ) m=zeros (3, 1) ; m(1) = -0.1*y(1) ; m(2) = 0.1*y(1) – 0.1*y(2) ; m(3) = 0.1 *y(2) ; In the command we can use function ode23 or ode 45. The only difference is in the results, which are more accurate in the latter because in involves more rigorous calculation. The function, say ode23, is used as follows, >> [ t Cr ] = ode23 ( @myode1, [ 0 : 60 ], [ 1 0 0 ] ) ; >> c = [ t Cr ] ; >> plot ( t, Cr ) >> xlabel ( ‘ Time in minutes ‘ )

11

>> ylabel ( ‘ Concentration in mol/L ‘ ) >> legend ( ‘CA’, ‘CR’, ‘CS’ ) >> grid The vector [ 0 : 60 ] denotes set of time values at which Cr is calculated. Vector [ 1 0 0 ] denotes initial values of CA, CR, and CS as 1, 0, and 0 mol/L respectively. The values of t, CA, CR, and CS are returned in vector c. RESULT: c= 0

1.0000

0

0

1.0000

0.9048

0.0905

0.0047

2.0000

0.8187

0.1637

0.0175

3.0000

0.7408

0.2223

0.0369

4.0000

0.6703

0.2681

0.0615

5.0000

0.6065

0.3033

0.0902

6.0000

0.5488

0.3293

0.1219

7.0000

0.4966

0.3477

0.1558

8.0000

0.4493

0.3596

0.1911

9.0000

0.4065

0.3661

0.2274

10.0000

0.3678

0.3681

0.2641

11.0000

0.3328

0.3664

0.3008

12.0000

0.3011

0.3617

0.3372

13.0000

0.2724

0.3546

0.3730

14.0000

0.2465

0.3456

0.4080

15.0000

0.2230

0.3351

0.4419

16.0000

0.2018

0.3234

0.4749

17.0000

0.1825

0.3110

0.5065

18.0000

0.1651

0.2980

0.5369

19.0000

0.1494

0.2846

0.5660 12

20.0000

0.1351

0.2711

0.5938

21.0000

0.1222

0.2576

0.6202

22.0000

0.1106

0.2442

0.6452

23.0000

0.1001

0.2310

0.6690

24.0000

0.0905

0.2181

0.6914

25.0000

0.0819

0.2056

0.7126

26.0000

0.0741

0.1934

0.7325

27.0000

0.0670

0.1817

0.7512

28.0000

0.0606

0.1705

0.7688

29.0000

0.0548

0.1598

0.7853

30.0000

0.0496

0.1496

0.8008

31.0000

0.0449

0.1398

0.8153

32.0000

0.0406

0.1306

0.8288

33.0000

0.0367

0.1219

0.8414

34.0000

0.0332

0.1136

0.8532

35.0000 0.0301

0.1058

0.8641

36.0000

0.0272

0.0984

0.8744

37.0000

0.0246

0.0915

0.8839

38.0000

0.0223

0.0851

0.8927

39.0000

0.0201

0.0790

0.9009

40.0000

0.0182

0.0733

0.9085

41.0000

0.0165 0.0680

0.9156

42.0000

0.0149

0.0630

0.9221

43.0000

0.0135

0.0583

0.9282

44.0000

0.0122

0.0540

0.9338

45.0000

0.0110

0.0500

0.9390

46.0000

0.0100

0.0462

0.9438 13

47.0000

0.0090

0.0427

0.9482

48.0000

0.0082

0.0395

0.9524

49.0000

0.0074

0.0365

0.9562

50.0000

0.0067

0.0337

0.9597

51.0000

0.0060

0.0311

0.9629

52.0000

0.0055

0.0286

0.9659

53.0000

0.0050

0.0264

0.9686

54.0000

0.0045

0.0243

0.9712

55.0000

0.0041

0.0224

0.9735

56.0000

0.0037

0.0207

0.9757

57.0000

0.0033

0.0190

0.9777

58.0000

0.0030

0.0175

0.9795

59.0000

0.0027

0.0161

0.9812

60.0000

0.0025

0.0148

0.9827

1 CA CR CS

0.9

Concentration in mol/L

0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0

0

10

20

30 Time in minutes

40

50

60

PRECAUTIONS:   

Keep in mind the syntax of ode23. Ode45 can also be used instead of ode23. The accuracy will be increased. Semicolons suppress output. Hence they must be carefully used. 14

EXPERIMENT NO. 7 OBJECTIVE : SOLVING PARTIAL DIFFERENTIAL EQUATION METHOD USED: PDEPE PROCEDURE: Q. Solve the unsteady state heat equation in one dimension

with α = 0.1 m2/s, initial conditions T( x, 0 ) = 20K and boundary conditions T( 0, t ) = 300K & T( 0.8, t ) = 300K. PDEs like above can be solved using function pdepe. In standard form

When the given equation is written, c = 1, f =

, m = 0, and s = 0. x lies between 0 and 0.8,

and say time t lies between 0 and 3.1 seconds. Taking 3 .m files pdex1pde.m, pdex1ic.m and pdex1bc.m as,

m and

s, we make a

function [ c , f , s ] = pdex1pde (x , t , T , DTDx ) c = 10 ; f = DTDx ; s=0; function T0 = pdex1ic (x) T0 = 20 ; function [ pl , ql , pr , qr ] = pdex1bc ( xl , Tl , xr , Tr , t ) pl = Tl – 300 ; ql = 0 ; pr = Tr – 300 ; qr = 0 ; Now, in the command window we use the function pdepe as follows,

15

>> m = 0 ; >> x = 0 : 0.1 : 0.8 ; >> t = 0 : 0.1 : 3.1 ; >> sol = pdepe ( m , @pdex1pde , @pdex1ic , @pdex1bc , x , t ) ; >> T = sol ( : , : , 1 ) >> surf ( x , t , T ) ; >> title ( ' Temperature variation across the slab ' ) >> xlabel ( ' Distance in metres ' ) >> ylabel ( ' Time in seconds ' ) >> zlabel ( ' Temperature in Kelvin ' ) The value of temperatures are returned to the vector T, which is then plotted against distance and time using the function surf, that generates 3 dimensional plots. RESULT: T= 300.0000 20.0000 20.0000 20.0000 20.0000 20.0000 20.0000 20.0000 300.0000 300.0000 153.3150 67.0590 33.3088 25.6091 33.3088 67.0590 153.3150 300.0000 300.0000 192.2620 110.3364 63.6943 49.1658 63.6943 110.3364 192.2620 300.0000 300.0000 212.2822 140.5572 95.0830 79.7108 95.0830 140.5572 212.2822 300.0000 300.0000 226.0163 164.0938 123.4612 109.3755 123.4612 164.0938 226.0163 300.0000 300.0000 236.8782 183.5989 148.2195 135.8507 148.2195 183.5989 236.8782 300.0000 300.0000 245.9018 200.1181 169.6028 158.9065 169.6028 200.1181 245.9018 300.0000 300.0000 253.5611 214.2351 187.9987 178.7958 187.9987 214.2351 253.5611 300.0000 300.0000 260.1325 226.3538 203.8010 195.8857 203.8010 226.3538 260.1325 300.0000 300.0000 265.7741 236.7605 217.3755 210.5687 217.3755 236.7605 265.7741 300.0000 300.0000 270.6107 245.6919 229.0384 223.1897 229.0384 245.6919 270.6107 300.0000 300.0000 274.7518 253.3447 239.0386 234.0145 239.0386 253.3447 274.7518 300.0000 300.0000 278.2993 259.9014 247.6077 243.2906 247.6077 259.9014 278.2993 300.0000 300.0000 281.3501 265.5407 254.9784 251.2697 254.9784 265.5407 281.3501 300.0000 300.0000 283.9619 270.3680 261.2871 258.0989 261.2871 270.3680 283.9619 300.0000

16

300.0000 286.2210 274.5415 266.7390 263.9996 266.7390 274.5415 286.2210 300.0000 300.0000 288.1520 278.1090 271.3997 269.0439 271.3997 278.1090 288.1520 300.0000 300.0000 289.8255 281.2002 275.4370 273.4133 275.4370 281.2002 289.8255 300.0000 300.0000 291.2544 283.8397 278.8850 277.1450 278.8850 283.8397 291.2544 300.0000 300.0000 292.4912 286.1250 281.8708 280.3768 281.8708 286.1250 292.4912 300.0000 300.0000 293.5468 288.0754 284.4191 283.1351 284.4191 288.0754 293.5468 300.0000 300.0000 294.4590 289.7613 286.6222 285.5199 286.6222 289.7613 294.4590 300.0000 300.0000 295.2288 291.1839 288.4812 287.5321 288.4812 291.1839 295.2288 300.0000 300.0000 295.8901 292.4060 290.0780 289.2606 290.0780 292.4060 295.8901 300.0000 300.0000 296.4679 293.4737 291.4731 290.7707 291.4731 293.4737 296.4679 300.0000 300.0000 296.9547 294.3732 292.6485 292.0429 292.6485 294.3732 296.9547 300.0000 300.0000 297.3731 295.1464 293.6587 293.1363 293.6587 295.1464 297.3731 300.0000 300.0000 297.7426 295.8289 294.5503 294.1014 294.5503 295.8289 297.7426 300.0000 300.0000 298.0559 296.4078 295.3067 294.9200 295.3067 296.4078 298.0559 300.0000 300.0000 298.3241 296.9034 295.9541 295.6207 295.9541 296.9034 298.3241 300.0000 300.0000 298.5610 297.3410 296.5258 296.2396 296.5258 297.3410 298.5610 300.0000 300.0000 298.7690 297.7254 297.0281 296.7832 297.0281 297.7254 298.7690 300.0000 Temperature variation across the slab

300

Temperature in Kelvin

250 200 150 100 50 0 4

3

2

Time in seconds

1

0

0

0.2

0.4

0.6

0.8

Distance in metres

PRECAUTIONS:  

Be careful in defining values of parameters and boundary conditions. Use of ‘;’ to suppress outputs should be carefully done. 17

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close