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

Table of Contents

ME 303 Homework 6 .......................................................................................................... 1


Problem 2a, 2b ................................................................................................................... 1
Function 5 ......................................................................................................................... 7

ME 303 Homework 6
By: Joseph Apollo

clc
close all
clear all

Problem 2a, 2b
tspan = [0 20]; %interval of t = 0 to t = 20
x0 = [0 0 0 1 0 0]; %initial values of x

[t,x] = ode45(@prob2fun, tspan, x0) %solves ODE from function


@prob2fun

figure(1) %plots and labels displacement graphs


subplot(2,1,1)
plot(t,x(:,1),t,x(:,2),t,x(:,3))
grid on
legend('x1','x2','x3')
title('Displacements of x1, x2, and x3')
xlabel('Time (s)')
ylabel('Displacement in meters (m)')
subplot(2,1,2) %plots and labels velocity graphs
plot(t,x(:,4),t,x(:,5),t,x(:,6))
grid on
legend('x1','x2','x3')
title('Velocities of x1, x2, and x3')
xlabel('Time (s)')
ylabel('Velocity in meters per second (m/s)')

t =

0
0.0001
0.0001
0.0002
0.0002
0.0005
0.0007
0.0010
0.0012
0.0025

1
0.0037
0.0050
0.0062
0.0125
0.0188
0.0251
0.0313
0.0627
0.0941
0.1255
0.1569
0.2286
0.3004
0.3721
0.4438
0.5357
0.6276
0.7195
0.8115
0.9063
1.0011
1.0959
1.1907
1.2904
1.3901
1.4898
1.5895
1.7360
1.8826
2.0292
2.1758
2.3692
2.5627
2.7562
2.9497
3.1769
3.4040
3.6312
3.8584
4.1249
4.3913
4.6577
4.9241
5.1465
5.3688
5.5912
5.8135
6.0358
6.2582
6.4805
6.7029
6.9519
7.2010
7.4500

2
7.6991
7.9286
8.1580
8.3875
8.6170
8.8401
9.0633
9.2865
9.5096
9.7164
9.9231
10.1298
10.3365
10.5728
10.8090
11.0453
11.2815
11.4807
11.6799
11.8791
12.0782
12.3005
12.5228
12.7451
12.9674
13.1267
13.2861
13.4454
13.6048
13.7217
13.8386
13.9555
14.0724
14.1893
14.3063
14.4232
14.5401
14.7010
14.8619
15.0228
15.1837
15.4099
15.6360
15.8622
16.0883
16.3269
16.5654
16.8039
17.0425
17.2990
17.5555
17.8120
18.0685
18.2963

3
18.5242
18.7520
18.9799
19.2349
19.4899
19.7450
20.0000

x =

0 0 0 1.0000 0 0
0.0001 0.0000 0.0000 1.0000 0.0000 0.0000
0.0001 0.0000 0.0000 1.0000 0.0000 0.0000
0.0002 0.0000 0.0000 1.0000 0.0000 0.0000
0.0002 0.0000 0.0000 1.0000 0.0000 0.0000
0.0005 0.0000 0.0000 1.0000 0.0000 0.0000
0.0007 0.0000 0.0000 1.0000 0.0000 0.0000
0.0010 0.0000 0.0000 1.0000 0.0000 0.0000
0.0012 0.0000 0.0000 1.0000 0.0000 0.0000
0.0025 0.0000 0.0000 1.0000 0.0000 0.0000
0.0037 0.0000 0.0000 1.0000 0.0000 0.0000
0.0050 0.0000 0.0000 1.0000 0.0000 0.0000
0.0062 0.0000 0.0000 1.0000 0.0000 0.0000
0.0125 0.0000 0.0000 1.0000 0.0000 0.0000
0.0188 0.0000 0.0000 0.9999 0.0000 0.0000
0.0251 0.0000 0.0000 0.9999 0.0001 0.0000
0.0313 0.0000 0.0000 0.9998 0.0001 0.0000
0.0627 0.0000 0.0000 0.9991 0.0005 0.0000
0.0941 0.0000 0.0000 0.9980 0.0011 0.0000
0.1254 0.0001 0.0000 0.9965 0.0019 0.0000
0.1567 0.0002 0.0000 0.9945 0.0030 0.0000
0.2278 0.0005 0.0000 0.9883 0.0062 0.0000
0.2983 0.0011 0.0000 0.9798 0.0108 0.0000
0.3682 0.0020 0.0000 0.9691 0.0164 0.0000
0.4372 0.0035 0.0000 0.9561 0.0233 0.0001
0.5242 0.0061 0.0000 0.9363 0.0337 0.0002
0.6093 0.0097 0.0000 0.9130 0.0459 0.0003
0.6920 0.0146 0.0001 0.8863 0.0598 0.0006
0.7721 0.0208 0.0002 0.8563 0.0753 0.0010
0.8517 0.0287 0.0003 0.8221 0.0928 0.0015
0.9279 0.0384 0.0004 0.7848 0.1117 0.0022
1.0004 0.0500 0.0007 0.7445 0.1319 0.0031
1.0690 0.0635 0.0010 0.7014 0.1533 0.0043
1.1365 0.0799 0.0015 0.6533 0.1767 0.0059
1.1992 0.0987 0.0022 0.6027 0.2009 0.0078
1.2566 0.1200 0.0031 0.5498 0.2258 0.0102
1.3087 0.1438 0.0043 0.4948 0.2511 0.0131
1.3751 0.1833 0.0066 0.4110 0.2884 0.0183
1.4291 0.2283 0.0097 0.3245 0.3253 0.0248
1.4702 0.2786 0.0139 0.2362 0.3611 0.0327
1.4983 0.3340 0.0194 0.1472 0.3950 0.0423
1.5154 0.4145 0.0290 0.0303 0.4355 0.0575
1.5102 0.5021 0.0418 -0.0839 0.4699 0.0759

4
1.4833 0.5958 0.0586 -0.1931 0.4966 0.0976
1.4359 0.6937 0.0798 -0.2954 0.5145 0.1227
1.3562 0.8119 0.1114 -0.4041 0.5228 0.1563
1.2534 0.9302 0.1511 -0.4982 0.5163 0.1941
1.1311 1.0452 0.1999 -0.5753 0.4942 0.2357
0.9934 1.1535 0.2585 -0.6340 0.4565 0.2803
0.8179 1.2673 0.3404 -0.6782 0.3932 0.3350
0.6343 1.3615 0.4370 -0.6952 0.3111 0.3907
0.4499 1.4316 0.5485 -0.6856 0.2127 0.4451
0.2713 1.4737 0.6740 -0.6511 0.1021 0.4958
0.1313 1.4855 0.7884 -0.6053 0.0037 0.5334
0.0031 1.4752 0.9107 -0.5460 -0.0972 0.5650
-0.1106 1.4423 1.0392 -0.4755 -0.1976 0.5894
-0.2076 1.3875 1.1721 -0.3965 -0.2942 0.6049
-0.2865 1.3120 1.3074 -0.3115 -0.3841 0.6105
-0.3460 1.2175 1.4428 -0.2233 -0.4643 0.6051
-0.3858 1.1065 1.5756 -0.1349 -0.5321 0.5878
-0.4061 0.9819 1.7032 -0.0486 -0.5852 0.5580
-0.4067 0.8307 1.8365 0.0424 -0.6252 0.5096
-0.3857 0.6724 1.9558 0.1245 -0.6426 0.4454
-0.3457 0.5126 2.0571 0.1951 -0.6365 0.3661
-0.2896 0.3573 2.1369 0.2524 -0.6068 0.2728
-0.2268 0.2231 2.1887 0.2924 -0.5595 0.1760
-0.1564 0.1018 2.2171 0.3197 -0.4946 0.0704
-0.0811 -0.0027 2.2205 0.3342 -0.4140 -0.0417
-0.0040 -0.0872 2.1975 0.3364 -0.3205 -0.1583
0.0704 -0.1477 2.1494 0.3277 -0.2201 -0.2736
0.1416 -0.1851 2.0755 0.3095 -0.1137 -0.3882
0.2079 -0.1983 1.9763 0.2833 -0.0047 -0.4998
0.2675 -0.1872 1.8528 0.2507 0.1033 -0.6058
0.3159 -0.1558 1.7180 0.2163 0.1993 -0.6970
0.3568 -0.1053 1.5653 0.1795 0.2887 -0.7795
0.3900 -0.0371 1.3965 0.1417 0.3686 -0.8518
0.4154 0.0464 1.2139 0.1043 0.4369 -0.9124
0.4352 0.1572 0.9917 0.0637 0.4981 -0.9658
0.4459 0.2801 0.7590 0.0271 0.5388 -1.0008
0.4484 0.4100 0.5203 -0.0042 0.5572 -1.0165
0.4443 0.5416 0.2803 -0.0292 0.5522 -1.0124
0.4369 0.6496 0.0802 -0.0450 0.5299 -0.9938
0.4267 0.7516 -0.1147 -0.0558 0.4913 -0.9615
0.4150 0.8443 -0.3019 -0.0617 0.4371 -0.9164
0.4025 0.9247 -0.4789 -0.0630 0.3685 -0.8591
0.3887 0.9968 -0.6616 -0.0598 0.2768 -0.7824
0.3762 1.0469 -0.8259 -0.0525 0.1719 -0.6939
0.3656 1.0724 -0.9694 -0.0424 0.0565 -0.5958
0.3575 1.0714 -1.0902 -0.0308 -0.0658 -0.4905
0.3533 1.0538 -1.1622 -0.0223 -0.1557 -0.4119
0.3503 1.0218 -1.2214 -0.0144 -0.2461 -0.3319
0.3486 0.9754 -1.2679 -0.0076 -0.3353 -0.2514
0.3478 0.9150 -1.3016 -0.0024 -0.4222 -0.1714
0.3477 0.8621 -1.3182 0.0001 -0.4837 -0.1135
0.3478 0.8020 -1.3282 0.0012 -0.5427 -0.0568
0.3479 0.7353 -1.3316 0.0009 -0.5987 -0.0015
0.3479 0.6622 -1.3286 -0.0010 -0.6513 0.0518

5
0.3476 0.5831 -1.3195 -0.0048 -0.7000 0.1031
0.3467 0.4987 -1.3046 -0.0104 -0.7445 0.1519
0.3451 0.4092 -1.2841 -0.0180 -0.7843 0.1979
0.3425 0.3155 -1.2584 -0.0276 -0.8192 0.2409
0.3367 0.1803 -1.2153 -0.0442 -0.8587 0.2947
0.3280 0.0396 -1.1640 -0.0647 -0.8878 0.3418
0.3157 -0.1048 -1.1057 -0.0891 -0.9061 0.3817
0.2992 -0.2513 -1.0415 -0.1171 -0.9133 0.4141
0.2677 -0.4574 -0.9439 -0.1621 -0.9046 0.4466
0.2255 -0.6590 -0.8407 -0.2125 -0.8745 0.4637
0.1713 -0.8513 -0.7354 -0.2670 -0.8239 0.4653
0.1045 -1.0301 -0.6314 -0.3238 -0.7547 0.4521
0.0201 -1.1997 -0.5266 -0.3839 -0.6640 0.4232
-0.0784 -1.3458 -0.4305 -0.4417 -0.5584 0.3804
-0.1902 -1.4652 -0.3461 -0.4946 -0.4416 0.3256
-0.3138 -1.5558 -0.2760 -0.5398 -0.3177 0.2610
-0.4573 -1.6199 -0.2188 -0.5770 -0.1813 0.1834
-0.6085 -1.6491 -0.1823 -0.5996 -0.0468 0.1003
-0.7634 -1.6445 -0.1675 -0.6050 0.0807 0.0152
-0.9173 -1.6086 -0.1746 -0.5915 0.1965 -0.0691
-1.0492 -1.5533 -0.1985 -0.5628 0.2867 -0.1407
-1.1726 -1.4791 -0.2383 -0.5180 0.3622 -0.2074
-1.2840 -1.3895 -0.2925 -0.4573 0.4215 -0.2674
-1.3798 -1.2883 -0.3596 -0.3816 0.4634 -0.3194
-1.4647 -1.1663 -0.4474 -0.2808 0.4893 -0.3668
-1.5220 -1.0405 -0.5457 -0.1655 0.4932 -0.4017
-1.5482 -0.9165 -0.6511 -0.0389 0.4764 -0.4235
-1.5410 -0.7992 -0.7605 0.0951 0.4410 -0.4322

6
Function 5
clc
clear all
close all

x0=0; xL=10; % define x domain


T0=240; TL=150; % given temperature BCs
DT1=-75; DT2=-100; % guesses for the 1st derivative at x=x0

[x1,T1]=ode45(@shootlin,[x0 xL],[T0, DT1]); %solves for first guess


TL1=T1(length(x1),1);

[x2,T2]=ode45(@shootlin,[x0 xL],[T0, DT2]); %solves for second guesss


TL2=T2(length(x2),1);

% Use linear interpolation to find exact value of IC for dT/dx


DT=DT1+(DT2-DT1)/(TL2-TL1)*(TL-TL1); %finds exact DT

[x,T]=ode45(@shootlin,[x0 xL],[T0, DT]); %finds exact solution

fprintf('1st shot: dT/dx = %8.3g, TL1 = %8.3g \n',DT1,TL1) %displays


both guesses and solution for DT and TL
fprintf('2nd shot: dT/dx = %8.3g, TL2 = %8.3g \n',DT2,TL2)
fprintf('Solution: dT/dx = %8.3g, TL = %8.3g \n',DT,T(length(x),1))

7
figure(2)
plot(x,T(:,1),x1,T1(:,1),x2,T2(:,1)) %plots and labels two guesses and
solution
xlabel('x'); ylabel('Temperature')
title('Solution of Linear BVP by the Shooting Method')
legend('T Solution','T 1st guess','T 2nd
guess','Location','NorthWest')
grid on

function dTdx=shootlin(x,T)

dTdx=[T(2); 0.15*T(1)]; %function into 1st order ODE for problem 5

end

1st shot: dT/dx = -75, TL1 = 1.12e+03


2nd shot: dT/dx = -100, TL2 = -432
Solution: dT/dx = -90.6, TL = 150

Published with MATLAB® R2021a

You might also like