부록E실습문제해답
부록E실습문제해답
실습문제 해답
부록E 실습문제 해답
실습문제 해답 2.1
1. 7
2. 10
3. 2.5000
4. 17
5. 7.8154
6. 4.1955
7. 12.9600
8. 5
9. 2.2361
10. -1
실습문제 해답 2.2
1. test is a valid name.
2. Test is a valid name, but is a different variable from test.
3. if is not allowed. It is a reserved keyword.
4. my-book is not allowed because it contains a hyphen.
5. my_book is a valid name
6. Thisisoneverylongnamebutisitstillallowed
? is not allowed because it
includes a question mark. Even without the question mark, it is not a
good idea.
7. 1stgroup is not allowed because it starts with a number.
부록E 실습문제 해답 3
실습문제 해답 2.3
1. 6
2. 72
3. 16
4. 13
5. 48
6. 38.5
7. 4096
8. 2.4179×1024
9. 245
10. 2187
11. 1
12. 7.5
13. 41.6667
14. 25.5
15. 135
실습문제 해답 2.4
1. a = [2.3 5.8 9]
2. sin(a)
ans =
0.7457 -0.4646 0.4121
3. a + 3
ans =
5.3000 8.8000 12.0000
4. b = [5.2 3.14 2]
5. a + b
4 공학도를 위한 매트랩
ans =
7.5000 8.9400 11.0000
6. a .* b
ans =
11.9600 18.2120 18.0000
7. a.^2
ans =
5.2900 33.6400 81.0000
8. c = 0:10 or
c = [0:10]
9. d = 0:2:10 or
d = [0:2:10]
10. linspace(10,20, 6)
ans =
10 12 14 16 18 20
11. logspace(1, 2, 5)
ans =
10.0000 17.7828 31.6228 56.2341 100.0000
실습문제 해답 3.1
1. In the command window, type
help cos
help sqrt
help exp
2. Select Help MATLAB Help from the menu bar.
Use the left-hand pane to navigate to either Functions - Categorical
List or Functions -Alphabetical List
3. Select Help Web Resources The Mathworks Web Site
실습문제 해답 3.2
1. x = -2:1:2
x=
-2 -1 0 1 2
abs(x)
ans =
부록E 실습문제 해답 5
21012
sqrt(x)
ans =
0 + 1.4142i 0 + 1.0000i 0 1.0000 1.4142
2. a. sqrt(-3)
ans =
0 + 1.7321i
sqrt(3)
ans =
1.7321
b. nthroot(-3,2)
?
??Error using ==> nthroot at 33
If X is negative, N must be an odd integer.
nthroot(3,2)
ans =
1.7321
c. -3^(1/2)
ans =
-1.7321
3^(1/2)
ans =
1.7321
3. x = -9:3:12
x=
-9 -6 -3 0 3 6 9 12
rem(x,2)
ans =
-1 0 -1 0 1 0 1 0
4. exp(x)
ans =
1.0e+005 *
0.0000 0.0000 0.0000 0.0000 0.0002 0.0040 0.0810 1.6275
5. log(x)
ans =
Columns 1 through 4
2.1972 + 3.1416i 1.7918 + 3.1416i 1.0986 + 3.1416i -Inf Columns 5
through 8
1.0986 1.7918 2.1972 2.4849
6 공학도를 위한 매트랩
log10(x)
ans =
Columns 1 through 4
0.9542 + 1.3644i 0.7782 + 1.3644i 0.4771 + 1.3644i -Inf
Columns 5 through 8
0.4771 0.7782 0.9542 1.0792
6. sign(x)
ans =
-1 -1 -1 0 1 1 1 1
7. format rat
x/2
ans =
-9/2 -3 -3/2 0 3/2 3 9/2 6
실습문제 해답 3.3
1. factor(322)
ans =
2 7 23
2. gcd(322,6)
ans =
2
3. isprime(322)
ans =
0 Because the result of isprime is the number 0, 322 is not a prime
number.
4. length(primes(322))
ans =
66
5. rats(pi)
ans =
355/113
6. factorial(10)
ans =
3628800
7. nchoosek(20,3)
ans =
1140
부록E 실습문제 해답 7
실습문제 해답 3.4
1. theta = 3*pi;
sin(2*theta)
ans =
-7.3479e-016
2. theta = 0:0.2*pi:2*pi;
cos(theta)
ans =
Columns 1 through 7
1.0000 0.8090 0.3090 -0.3090 -0.8090 -1.0000 -0.8090
Columns 8 through 11
-0.3090 0.3090 0.8090 1.0000
3. asin(1)
ans =
1.5708 This answer is in radians.
4. x = -1:0.2:1;
acos(x)
ans =
Columns 1 through 7
3.1416 2.4981 2.2143 1.9823 1.7722 1.5708 1.3694
Columns 8 through 11
1.1593 0.9273 0.6435 0
5. cos(45*pi/180)
ans =
0.7071
cosd(45)
ans =
0.7071
6. asin(0.5)
ans =
0.5236 This answer is in radians. You could also find the result in
degrees.
asind(0.5)
ans =
30.0000
7. csc(60*pi/180)
ans =
1.1547
8 공학도를 위한 매트랩
or
cscd(60)
ans =
1.1547
실습문제 해답 3.5
x = [4 90 85 75; 2 55 65 75; 3 78 82 79;1 84 92 93];
1. max(x)
ans =
4 90 92 93
2. [maximum, row]=max(x)
maximum =
4 90 92 93
row =
1144
3. max(x')
ans =
90 75 82 93
4. [maximum, column]=max(x')
maximum =
90 75 82 93
column =
2434
5. max(max(x))
ans =
10
93
실습문제 해답 3.6
x = [4 90 85 75; 2 55 65 75; 3 78 82 79;1 84 92 93];
1. mean(x)
ans =
2.5000 76.7500 81.0000 80.5000
2. median(x)
ans =
부록E 실습문제 해답 9
실습문제 해답 3.7
x = [4 90 85 75; 2 55 65 75; 3 78 82 79;1 84 92 93];
1. size(x)
ans =
44
2. sort(x)
ans =
1 55 65 75
2 78 82 75
3 84 85 79
4 90 92 93
3. sort(x,'descend')
ans =
4 90 92 93
3 84 85 79
2 78 82 75
1 55 65 75
4. sortrows(x)
ans =
10 공학도를 위한 매트랩
1 84 92 93
2 55 65 75
3 78 82 79
4 90 85 75
5. sortrows(x,-3)
ans =
12
1 84 92 93
4 90 85 75
3 78 82 79
2 55 65 75
실습문제 해답 3.8
x = [4 90 85 75; 2 55 65 75; 3 78 82 79;1 84 92 93];
1. std(x)
ans =
1.2910 15.3052 11.4601 8.5440
2. var(x)
ans =
1.6667 234.2500 131.3333 73.0000
3. sqrt(var(x))
ans =
1.2910 15.3052 11.4601 8.5440
4. The square root of the variance is equal to the standard deviation.
실습문제 해답 3.9
1. rand(3)
ans =
0.9501 0.4860 0.4565
0.2311 0.8913 0.0185
0.6068 0.7621 0.8214
2. randn(3)
ans =
-0.4326 0.2877 1.1892
-1.6656 -1.1465 -0.0376
부록E 실습문제 해답 11
실습문제 해답 3.10
1. A = 1+i
A=
1.0000 + 1.0000i
B = 2-3i
B=
2.0000 - 3.0000i
C = 8+2i
C=
12 공학도를 위한 매트랩
8.0000 + 2.0000i
2. imagD = [-3,8,-16];
realD = [2,4,6];
D = complex(realD,imagD)
ans =
2.0000 - 3.0000i 4.0000 + 8.0000i 6.0000 -16.0000i
3. abs(A)
ans =
1.4142
abs(B)
ans =
3.6056
abs(C)
ans =
8.2462
abs(D)
ans =
3.6056 8.9443 17.0880
4. angle(A)
ans =
0.7854
angle(B)
ans =
-0.9828
angle(C)
ans =
0.2450
angle(D)
ans =
-0.9828 1.1071 -1.2120
5. conj(D)
ans =
2.0000 + 3.0000i 4.0000 - 8.0000i 6.0000 +16.0000i
6. D'
ans =
2.0000 + 3.0000i
4.0000 - 8.0000i
6.0000 +16.0000i
부록E 실습문제 해답 13
7. sqrt(A.*A')
ans =
1.4142
실습문제 해답 3.11
1. clock
ans =
1.0e+003 *
2.0080 0.0050 0.0270 0.0160 0.0010 0.0220
2. date
ans =
27-May-2008
3. a. factorial(322)
ans =
Inf
b. 5*10^500
ans =
Inf
c. 1/5*10^500
ans =
Inf
d. 0/0
Warning: Divide by zero.
ans =
NaN
실습문제 해답 4.1
a = [12 17 3 6]
a=
12 17 3 6
b = [5 8 3; 1 2 3; 2 4 6]
b=
583
123
246
14 공학도를 위한 매트랩
c = [22;17;4]
c=
22
17
4
1. x1 = a(1,2)
x1 =
17
2. x2 = b(:,3)
x2 =
3
3
6
3. x3 = b(3,:)
x3 =
246
4. x4 = [b(1,1), b(2,2), b(3,3)]
x4 =
526
5. x5 = [a(1:3);b]
x5 =
12 17 3
583
123
246
6. x6 = [c,b;a]
x6 =
22 5 8 3
17 1 2 3
4246
12 17 3 6
7. x7 = b(8)
x7 =
3
8. x8 = b(:)
x8 =
5
1
부록E 실습문제 해답 15
2
8
2
4
19
3
3
6
실습문제 해답 4.2
1. length = [1, 3, 5];
width = [2,4,6,8];
[L,W] = meshgrid(length,width);
area = L.*W
area =
2 6 10
4 12 20
6 18 30
8 24 40
2. radius = 0:3:12;
height = 10:2:20;
[R,H] = meshgrid(radius,height);
volume = pi*R.^2.*H
volume =
1.0e+003 *
0 0.2827 1.1310 2.5447 4.5239
0 0.3393 1.3572 3.0536 5.4287
0 0.3958 1.5834 3.5626 6.3335
0 0.4524 1.8096 4.0715 7.2382
0 0.5089 2.0358 4.5804 8.1430
0 0.5655 2.2619 5.0894 9.0478
16 공학도를 위한 매트랩
실습문제 해답 4.3
1. zeros(3)
ans =
000
000
000
2. zeros(3,4)
ans =
0000
0000
0000
3. ones(3)
ans =
111
111
111
4. ones(5,3)
ans =
111
111
111
111
111
5. ones(4,6)*pi
ans =
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416
6. x = [1,2,3];
diag(x)
ans =
100
020
003
7. x = magic(10)
x=
92 99 1 8 15 67 74 51 58 40
부록E 실습문제 해답 17
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 29 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
a. diag(x)
ans =
92 80 88 21 9 42 30 38 46 59
b. diag(fliplr(x))
ans =
40 64 63 62 61 90 89 13 12 11
c. sum(x)
ans =
505 505 505 505 505 505 505 505 505 505
sum(x')
ans =
505 505 505 505 505 505 505 505 505 505
sum(diag(x))
ans =
505
sum(diag(fliplr(x)))
ans =
실습문제 해답 5.1
1. clear,clc
x = 0:0.1*pi:2*pi;
y = sin(x);
plot(x,y)
2. title('Sinusoidal Curve')
xlabel('x values')
ylabel('sin(x)')
3. figure(2)
18 공학도를 위한 매트랩
3. figure(2)
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)
title('Sine and
Cosine Plots')
xlabel('x values')
ylabel('y values')
4. figure(3)
plot(x,y1,'-- r',
x,y2,': g')
title('Sine and Cosine
Plots')
xlabel('x values')
ylabel('y values')
5. legend('sin(x)','cos(x)')
6. axis([-1,2*pi+1,
-1.5,1.5])
7. figure(4)
a = cos(x);
plot(a)
A line graph is created, with a plotted against the vector index number.
실습문제 해답 5.2
1. subplot(2,1,1)
2. x = -1.5:0.1:1.5;
y = tan(x);
plot(x,y)
3. title('Tangent(x)')
xlabel('x value')
ylabel('y value')
4. subplot(2,1,2)
y = sinh(x);
plot(x,y)
5. title('Hyperbolic
sine of x')
xlabel('x value')
부록E 실습문제 해답 19
ylabel('y value')
6. figure(2)
subplot(1,2,1)
plot(x,y)
title('Tangent(x)')
xlabel('x value')
ylabel('y value')
subplot(1,2,2)
y = sinh(x);
plot(x,y)
title('Hyperbolic
sine of x')
xlabel('x value')
ylabel('y value')
실습문제 해답 5.3
1. theta = 0:0.01*pi:2*pi;
r = 5*cos(4*theta);
polar(theta,r)
2. hold on
r = 4*cos(6*theta);
polar(theta,r)
title('Flower Power')
3. figure(2)
r = 5-5*sin(theta);
polar(theta,r)
4. figure(3)
r = sqrt(5^2*cos(2*theta));
polar(theta3,r)
5. figure(4)
theta = pi/2:4/5*pi:4.8*pi;
r = ones(1,6);
polar(theta,r)
20 공학도를 위한 매트랩
실습문제 해답 5.4
1. figure(1)
x = -1:0.1:1;
y = 5*x+3;
subplot(2,2,1)
plot(x,y)
title('Rectangular Coordinates')
ylabel('y-axis')
grid on
subplot(2,2,2)
semilogx(x,y)
title('Semilog x Coordinate System')
grid on
subplot(2,2,3)
semilogy(x,y)
title('Semilog y Coordinate System')
ylabel('y-axis')
xlabel('x-axis')
grid on
subplot(2,2,4)
loglog(x,y)
title('Log Plot')
xlabel('x-axis')
grid on
2. figure(2)
x = -1:0.1:1;
y = 3*x.^2;
subplot(2,2,1)
plot(x,y)
title('Rectangular Coordinates')
ylabel('y-axis')
grid on
subplot(2,2,2)
semilogx(x,y)
title('Semilog x Coordinate System')
grid on
subplot(2,2,3)
semilogy(x,y)
부록E 실습문제 해답 21
ylabel('y-axis')
grid on
subplot(2,2,2)
semilogx(x,y)
title('Semilog x Coordinate System')
grid on
subplot(2,2,3)
semilogy(x,y)
title('Semilog y Coordinate System')
ylabel('y-axis')
xlabel('x-axis')
grid on
subplot(2,2,4)
loglog(x,y)
title('Log Plot')
xlabel('x-axis')
grid on
실습문제 해답 5.5
1. fplot('5*t^2',[-3,+3])
title('5*t^2')
xlabel('x-axis')
ylabel('y-axis')
2. fplot('5*sin(t)^2 + t*cos(t)^2',[-2*pi,2*pi])
title('5*sin(t)^2 +
t*cos(t)^2')
xlabel('x-axis')
ylabel('y-axis')
3. fplot('t*exp(t)',[0,10])
title('t*exp(t)')
xlabel('x-axis')
ylabel('y-axis')
4. fplot('log(t)+ sin(t)',[0,pi])
title('log(t)+sin(t)')
xlabel('x-axis')
ylabel('y-axis')
부록E 실습문제 해답 23
실습문제 해답 6.1
Store these functions as separate M-files. The name of the function must be
the same as the name of the M-file. You’ll need to call these functions either
from the command window or from a script M-file. You can’t run a function
M-file by itself.
1. function output = quadratic(x)
output = x.^2;
2. function output = one_over(x)
output = exp(1./x);
3. function output = sin_x_squared(x)
output = sin(x.^2);
4. function result = in_to_ft(x)
result = x./12;
5. function result = cal_to_joules(x)
result = 4.2.*x;
6. function output = Watts_to_Btu_per_hour(x)
output = x.*3.412;
7. function output = meters_to_miles(x)
output = x./1000.*.6214;
8. function output = mph_to_fps(x)
output = x.*5280/3600;
실습문제 해답 6.2
Store these functions as separate M-files. The name of the function must be
the same as the name of the M-file.
1. function output = z1(x,y)
% summation of x and y
% the matrix dimensions must agree
output = x+y;
2. function output = z2(a,b,c)
% finds a.*b.^c
% the matrix dimensions must agree
output = a.*b.^c;
3. function output = z3(w,x,y)
% finds w.*exp(x./y)
% the matrix dimensions must agree
24 공학도를 위한 매트랩
output = w.*exp(x./y);
4. function output = z4(p,t)
% finds p./sin(t)
% the matrix dimensions must agree
output = p./sin(t);
5. function [a,b]=f5(x)
a = cos(x);
b = sin(x);
6. function [a,b] = f6(x)
a = 5.*x.^2 + 2;
b = sqrt(5.*x.^2 + 2);
7. function [a,b] = f7(x)
a = exp(x);
b = log(x);
8. function [a,b] = f8(x,y)
a = x+y;
b = x-y;
9. function [a,b] = f9(x,y)
a = y.*exp(x);
b = x.*exp(y);
실습문제 해답 7.1
1. b = input('Enter the length of the base of the triangle: ');
h = input('Enter the height of the triangle: ');
Area = 1/2*b*h
When this file runs, it generates the following interaction in the command
window:
Enter the length of the base of the triangle: 5
Enter the height of the triangle: 4
Area =
10
2. r = input('Enter the radius of the cylinder: ');
h = input('Enter the height of the cylinder: ');
Volume = pi*r.^2*h
When this file runs, it generates the following interaction in the command
window:
Enter the radius of the cylinder: 2
부록E 실습문제 해답 25
실습문제 해답 7.2
1. disp('Inches to Feet Conversion Table')
2. disp(' Inches Feet')
3. inches = 0:10:120;
feet = inches./12;
table = [inches; feet];
fprintf(' %8.0f %8.2f \n',table)
The resulting display in the command window is
Inches to Feet Conversion Table
Inches Feet
0 0.00
10 0.83
20 1.67
26 공학도를 위한 매트랩
… …
… …
… …
100 8.33
110 9.17
120 10.00
실습문제 해답 8.1
Use these arrays in the exercises.
x = [1 10 42 6
5 8 78 23
56 45 9 13
23 22 8 9];
y = [1 2 3; 4 10 12; 7 21 27];
z = [10 22 5 13];
1. elements_x = find(x>10)
elements_y = find(y>10)
elements_z = find(z>10)
2. [rows_x, cols_x] = find(x>10)
[rows_y, cols_y] = find(y>10)
[rows_z, cols_z] = find(z>10)
3. x(elements_x)
y(elements_y)
z(elements_z)
4. elements_x = find(x>10 & x< 40)
elements_y = find(y>10 & y< 40)
elements_z = find(z>10 & z< 40)
5. [rows_x, cols_x] = find(x>10 & x<40)
[rows_y, cols_y] = find(y>10 & y<40)
[rows_z, cols_z] = find(z>10 & z<40)
6. x(elements_x)
y(elements_y)
z(elements_z)
7. elements_x = find((x>0 & x<10) | (x>70 & x<80))
elements_y = find((y>0 & y<10) | (y>70 & y<80))
elements_z = find((z>0 & z<10) | (z>70 & z<80))
8. length_x = length(find((x>0 & x<10) | (x>70 & x<80)))
부록E 실습문제 해답 27
실습문제 해답 8.2
1. function output = drink(x)
if x> = 21
output = 'You can drink';
else
output = 'Wait ''till you"re older';
end
Test your function with the following from the command window or a
script M-file:
drink(22)
drink(18)
2. function output = tall(x)
if x> = 48
output = 'You may ride';
else
output = 'You''re too short';
end
Test your function with the following:
tall(50)
tall(46)
3. function output = spec(x)
if x> = 5.3 & x< = 5.5
output = ' in spec';
else
output = ' out of spec';
end
Test your function with the following
spec(5.6)
spec(5.45)
spec(5.2)
4. function output = metric_spec(x)
if x> = 5.3/2.54 & x< = 5.5/2.54
output = ' in spec';
28 공학도를 위한 매트랩
else
output = ' out of spec';
end
Test your function with the following:
metric_spec(2)
metric_spec(2.2)
metric_spec(2.4)
5. function output = flight(x)
if x> = 0 & x< = 100
output = 'first stage';
elseif x< = 170
output = 'second stage';
elseif x<260
output = 'third stage';
else
output = 'free flight';
end
Test your function with the following:
flight(50)
flight(110)
flight(200)
flight(300)
실습문제 해답 8.3
1. year = input('Enter the name of your year in school: ','s');
switch year
case 'freshman'
day = 'Monday';
case 'sophomore'
day = 'Tuesday';
case 'junior'
day = 'Wednesday';
case 'senior'
day = 'Thursday’;
otherwise
day = 'I don''t know that year';
end
부록E 실습문제 해답 29
실습문제 해답 9.1
1. inches = 0:3:24;
for k = 1:length(inches)
feet(k) = inches(k)/12;
end
table = [inches',feet']
2. x = [ 45,23,17,34,85,33];
count = 0;
30 공학도를 위한 매트랩
for k = 1:length(x)
if x(k)>30
count = count+1;
end
end
fprintf('There are %4.0f values greater than 30 \n',count)
3. num = length(find(x>30));
fprintf('There are %4.0f values greater than 30 \n',num)
4. total = 0;
for k = 1:length(x)
total = total + x(k);
end
disp('The total is: ')
disp(total)
sum(x)
5. for k = 1:10
x(k) = 1/k
end
6. for k = 1:10
x(k)=(-1)^(k+1)/k
end
실습문제 해답 9.2
1. inches = 0:3:24;
k = 1;
while k<=length(inches)
feet(k) = inches(k)/12;
k = k+1;
end
disp(' Inches Feet');
fprintf(' %8.0f %8.2f \n',[inches;feet])
2. x = [ 45,23,17,34,85,33];
k = 1;
count = 0;
while k< = length(x)
if x(k)> = 30;
count = count +1;
부록E 실습문제 해답 31
end
k=k+1;
end
fprintf('There are %4.0f values greater than 30 \n',count)
3. count = length(find(x>30))
4. k = 1;
total = 0;
while k< = length(x)
total = total + x(k);
k = k+1;
end
disp(total)
sum(x)
5. k = 1;
while(k< = 10)
x(k) = 1/k;
k = k+1;
end
x
6. k = 1;
while(k< = 10)
x(k)=(-1)^(k+1)/k
k = k+1;
end
x
실습문제 해답 10.1
1. A = [ 1 2 3 4]
B = [ 12 20 15 7]
dot(A,B)
2. sum(A.*B)
3. price = [0.99, 1.49, 2.50, 0.99, 1.29];
num = [4, 3, 1, 2, 2];
total = dot(price,num)
32 공학도를 위한 매트랩
실습문제 해답 10.2
1. A = [2 5; 2 9; 6 5];
B = [2 5; 2 9; 6 5];
% These cannot be multiplied because the number of
% columns in A does not equal
% the number of rows in B
2. A = [2 5; 2 9; 6 5];
B = [1 3 12; 5 2 9];
% Since A is a 3 × 2 matrix and B is a 2 × 3 matrix,
% they can be multiplied
A*B
%However, A*B does not equal B*A
B*A
3. A = [5 1 9; 7 2 2];
B = [8 5; 4 2; 8 9];
% Since A is a 2 × 3 matrix and B is a 3 × 2 matrix,
% they can be multiplied
A*B
%However, A*B does not equal B*A
B*A
4. A = [1 9 8; 8 4 7; 2 5 3];
B = [7;1;5]
% Since A is a 3 × 3 matrix and B is a 3 × 1 matrix,
% they can be multiplied
A*B
% However, B*A won't work
실습문제 해답 10.3
1. a. a = magic(3)
inv(magic(3))
magic(3)^-1
b. b = magic(4)
inv(b)
b^-1
c. c = magic(5)
inv(magic(5))
부록E 실습문제 해답 33
magic(5)^-1
2. det(a)
det(b)
det(c)
3. A = [1 2 3;2 4 6;3 6 9]
det(A)
inv(A)
%Notice that the three lines are just multiples of
%each other and therefore do not represent %independent equations
실습문제 해답 11.1
1. A = [1,4,6; 3, 15, 24; 2, 3,4];
B = single(A)
C = int8(A)
D = uint8(A)
2. E = A+B
% The result is a single-precision array
3. x = int8(1)
y = int8(3)
result1 = x./y
% This calculation returns the integer 0
x = int8(2)
result2 = x./y
% This calculation returns the integer 1; it appears
% that MATLAB rounds the answer
4. intmax('int8')
intmax('int16')
intmax('int32')
intmax('int64')
intmax('uint8')
intmax('uint16')
intmax('uint32')
intmax('uint64')
5. intmin('int8')
intmin('int16')
intmin('int32')
intmin('int64')
34 공학도를 위한 매트랩
intmin('uint8')
intmin('uint16')
intmin('uint32')
intmin('uint64')
실습문제 해답 11.2
1. name ='Holly'
2. G = double('g')
fprintf('The decimal equivalent of the letter g is %5.0f \n',G)
3. m = 'MATLAB'
M = char(double(m)-32)
실습문제 해답 11.3
1. a = magic(3)
b = zeros(3)
c = ones(3)
x(:,:,1) = a
x(:,:,2) = b
x(:,:,3) = c
2. x(3,2,1)
3. x(2,3,:)
4. x(:,3,:)
실습문제 해답 11.4
1. names = char('Mercury','Venus','Earth','Mars','Jupiter',
'Saturn','Uranus','Neptune','Pluto')
2. R = 'rocky';
G = 'gas giants';
type = char(R,R,R,R,G,G,G,G,R)
3. space =[' ';' ';' ';' ';' ';' ';' ';' ';' '];
4. table =[names,space,type]
5. %These data were found at
%http://sciencepark.etacude.com/astronomy/pluto.php
%Similar data are found at many websites
부록E 실습문제 해답 35
mercury = 3.303e23; % kg
venus = 4.869e24; % kg
earth = 5.976e24; % kg
mars = 6.421e23; % kg
jupiter = 1.9e27; % kg
saturn = 5.69e26; % kg
uranus = 8.686e25; % kg
neptune = 1.024e26; % kg
pluto = 1.27e22 % kg
mass = [mercury,venus,earth,mars,jupiter, saturn,uranus,neptune,pluto]';
newtable = [table,space,num2str(mass)]
실습문제 해답 12.1
1. syms x a b c d
%or
d = sym('d') %etc
d=
d
2. ex1 = x^2-1
ex1 =
x^2-1
ex2 = (x+1)^2
ex2 =
(x+1)^2
ex3 = a*x^2-1
ex3 =
a*x^2-1
ex4 = a*x^2 + b*x + c
ex4 =
a*x^2+b*x+c
ex5 = a*x^3 + b*x^2 + c*x + d
ex5 =
a*x^3+b*x^2+c*x+d
ex6 = sin(x)
ex6 =
sin(x)
3. EX1 = sym('X^2 - 1 ')
36 공학도를 위한 매트랩
EX1 =
X^2 - 1
EX2 = sym(' (X +1)^2 ')
EX2 =
(X +1)^2
EX3 = sym('A*X ^2 - 1 ')
EX3 =
A*X ^2 - 1
EX4 = sym('A*X ^2 + B*X + C ')
EX4 =
A*X ^2 + B*X + C
EX5 = sym('A*X ^3 + B*X ^2 + C*X + F ')
EX5 =
A*X ^3 + B*X ^2 + C*X + F
EX6 = sym(' sin(X) ')
EX6 =
sin(X)
4. eq1 = sym(' x^2=1 ')
eq1 =
x^2 = 1
eq2 = sym(' (x+1)^2=0 ')
eq2 =
(x+1)^2=0
eq3 = sym(' a*x^2=1 ')
eq3 =
a*x^2=1
eq4 = sym('a*x^2 + b*x + c = 0 ')
eq4 =
a*x^2 + b*x + c = 0
eq5 = sym('a*x^3 + b*x^2 + c*x + d = 0 ')
eq5 =
a*x^3 + b*x^2 + c*x + d = 0
eq6 = sym('sin(x) = 0 ')
eq6 =
sin(x) = 0
5. EQ1 = sym('X^2 = 1 ')
EQ1 =
X^2 = 1
부록E 실습문제 해답 37
실습문제 해답 12.2
1. y1 = ex1*ex2
y1 =
(x^2-1)*(x+1)^2
2. y2 = ex1/ex2
y2 =
(x^2-1)/(x+1)^2
3. [num1,den1] = numden(y1)
num1 =
(x^2-1)*(x+1)^2
den1 =
1
[num2,den2] = numden(y2)
num2 =
x-1
den2 =
(x+1)
4. Y1 = EX1*EX2
Y1 =
(X^2-1)*(X+1)^2
5. Y2=EX1/EX2
38 공학도를 위한 매트랩
Y2 =
(X^2-1)/(X+1)^2
6. [NUM1,DEN1] = numden(Y1)
NUM1 =
(X^2-1)*(X+1)^2
DEN1 =
1
[NUM2,DEN2] = numden(Y2)
NUM2 =
X-1
DEN2 =
(X+1)
7. %numden(EQ4)
%The numden function does not apply to equations,
%only to expressions
8. a. factor(y1)
ans =
(x-1)*(x+1)^3
expand(y1)
ans =
x^4+2*x^3-2*x-1
collect(y1)
ans =
x^4+2*x^3-2*x-1
simplify(y1)
ans =
(x^2 - 1)*(x + 1)^2
b. factor(y2)
ans =
(x-1)/(x+1)
expand(y2)
ans =
x^2/(x^2 + 2*x + 1) - 1/(x^2 + 2*x + 1)
collect(y2)
ans =
(x - 1)/(x + 1)
simplify(y2)
ans =
부록E 실습문제 해답 39
(x - 1)/(x + 1)
c. factor(Y1)
ans =
(X-1)*(X+1)^3
expand(Y1)
ans =
X^4+2*X^3-2*X-1
collect(Y1)
ans =
X^4+2*X^3-2*X-1
simplify(Y1)
ans =
(X^2 - 1)*(X + 1)^2
d. factor(Y2)
ans =
(X-1)/(X+1)
expand(Y2)
ans =
X^2/(X^2 + 2*X + 1) - 1/(X^2 + 2*X + 1)
collect(Y2)
ans =
(X - 1)/(X + 1)
simplify(Y2)
ans =
(X - 1)/(X + 1)
9. factor(EX1)
ans =
(X-1)*(X+1)
expand(EX1)
ans =
X^2-1
collect(EX1)
ans =
X^2-1
simplify(EX1)
ans =
X^2 - 1
factor(EQ1)
40 공학도를 위한 매트랩
ans =
X^2 == 1
expand(EQ1)
ans =
X^2 == 1
collect(EQ1)
ans =
X^2 == 1
simplify(EQ1)
ans =
X^2 == 1
%
factor(EX2)
ans =
(X+1)^2
expand(EX2)
ans =
X^2+2*X+1
collect(EX2)
ans =
X^2+2*X+1
simplify(EX2)
ans =
(X + 1)^2
factor(EQ2)
ans =
(X+1)^2 == 0
expand(EQ2)
ans =
X^2+2*X+1 == 0
collect(EQ2)
ans =
X^2+2*X+1 = 0
simplify(EQ2)
ans =
X == -1
부록E 실습문제 해답 41
실습문제 해답 12.3
1. solve(ex1)
ans =
1
-1
solve(EX1)
ans =
1
-1
solve(eq1)
ans =
1
-1
solve(EQ1)
ans =
1
-1
2. solve(ex2)
ans =
-1
-1
solve(EX2)
ans =
-1
-1
solve(eq2)
ans =
-1
-1
solve(EQ2)
ans =
-1
-1
3. a. solve(ex3,x)
ans =
1/a^(1/2)
-1/a^(1/2)
solve(eq3,a)
42 공학도를 위한 매트랩
ans =
1/x^2
b. solve(eq3,x)
ans =
1/a^(1/2)
-1/a^(1/2)
solve(ex3,a)
ans =
1/x^2
4. a. solve(EX3,'X')
ans =
1/A^(1/2)
-1/A^(1/2)
solve(EX3,'A')
ans =
1/X^2
b. solve(EQ3,'X')
ans =
1/A^(1/2)
-1/A^(1/2)
solve(EQ3,'A')
ans =
1/X^2
5. a. solve(ex4,x)
ans =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
solve(ex4,a)
ans =
-(c + b*x)/x^2
a. solve(eq4,x)
ans =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
solve(eq4,a)
ans =
-(c + b*x)/x^2
6. a. solve(EX4,'X')
부록E 실습문제 해답 43
ans =
-(B + (B^2 - 4*A*C)^(1/2))/(2*A)
-(B - (B^2 - 4*A*C)^(1/2))/(2*A)
solve(EX4,'A')
ans =
-(C + B*X)/X^2
b. . solve(EQ4,'X')
ans =
-(B + (B^2 - 4*A*C)^(1/2))/(2*A)
-(B - (B^2 - 4*A*C)^(1/2))/(2*A)
solve(EQ4,'A')
ans =
-(C + B*X)/X^2
7. solve(ex5,x)
ans =
1/6/a*(36*c*b*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*c^3*a-c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-2/3*(3*c*a-b^2)/a/
(36*c*b*a-
108*d*a^2-8*b^3+12*3^(1/2)*(4*c^3*a-c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-1/3*b/a -1/12/
a*(36*c*b*a-
108*d*a^2-8*b^3+12*3^(1/2)*(4*c^3*a-c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+1/3*(3*c*a-b^2)/a/
(36*c*b*a-
108*d*a^2-8*b^3+12*3^(1/2)*(4*c^3*a-c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-
1/3*b/a+1/2*i*3^(1/2)*(1/6/a*(36*c*b*a-108*d*a^2-
8*b^3+12*3^(1/2)*(4*c^3*ac^
2*b^2-18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+2/3*(3*c*ab^
2)/a/(36*c*b*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*c^3*a-c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3))-1/12/a*(36*c*b*a-
108*d*a^2-
8*b^3+12*3^(1/2)*(4*c^3*a-c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+1/3*(3*c*a-b^2)/a/
(36*c*b*a-
108*d*a^2-8*b^3+12*3^(1/2)*(4*c^3*a-c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-1/3*b/a-
1/2*i*3^(1/2)*(1/6/a*(36*c*b*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*c^3*a-
44 공학도를 위한 매트랩
c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+2/3*(3*c*a-b^2)/a/
(36*c*b*a60
108*d*a^2-8*b^3+12*3^(1/2)*(4*c^3*a-c^2*b^2-
18*c*b*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3))
% Clearly this is too complicated to memorize
8. solve(ex6)
ans =
0
solve(EX6)
ans =
0
solve(eq6)
ans =
0
solve(EQ6)
ans =
0
실습문제 해답 12.4
1. coef = [5 6 -3; 3 -3 2; 2 -4 -12];
result =[10; 14; 24];
x = inv(coef)*result
% or
x = coef\result
x=
3.5314
-1.6987
-0.8452
2. A1 = sym('5*x + 6*y - 3*z = 10');
A2 = sym('3*x - 3*y + 2*z = 14');
A3 = sym('2*x - 4*y -12*z = 24');
A = solve(A1,A2,A3)
A=
x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]
부록E 실습문제 해답 45
3. A.x
ans =
844/239
A.y
ans =
-406/239
A.z
ans =
-202/239
double(A.x)
ans =
3.5314
double(A.y)
ans =
-1.6987
double(A.z)
ans =
-0.8452
4. [x,y,z] = solve(A1,A2,A3)
x=
844/239
y=
-406/239
z=
-202/239
5. A1 = sym('5.0*x + 6.0*y - 3.0*z = 10.0');
A2 = sym('3.0*x - 3.0*y + 2.0*z = 14.0');
A3 = sym('2.0*x - 4.0*y -12.0*z = 24.0');
A = solve(A1,A2,A3)
A=
x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]
A.x
ans =
3.5313807531380753138075313807531
A.y
ans =
46 공학도를 위한 매트랩
-1.6987447698744769874476987447699
A.z
ans =
-.84518828451882845188284518828452
6. A = sym('x^2 +5*y -3*z^3=15');
B = sym('4*x + y^2 -z = 10');
C = sym('x + y + z =15’);
[X,Y,Z]=solve(A,B,C)
X=
11.560291920108418818149999909102-
11.183481663794727000635376340336*i
… lots more numbers-
Y=
3.5094002752389020636845577121798+6.9732883324603664143501389722123*i
… lots more numbers
Z=
-.696921953473208818345576212814e-
1+4.2101933313343605862852373681236
*i
… lots more numbers
double(X)
ans =
11.5603 -11.1835i
10.2173 - 4.7227i
16.8891 - 4.2178i
16.8891 + 4.2178i
10.2173 + 4.7227i
11.5603 +11.1835i
double(Y)
ans =
3.5094 + 6.9733i
1.6407 + 5.5153i
0.8499 + 7.8114i
0.8499 - 7.8114i
1.6407 - 5.5153i
3.5094 - 6.9733i
double(Z)
ans =
부록E 실습문제 해답 47
-0.0697 + 4.2102i
3.1420 - 0.7926i
-2.7390 - 3.5936i
-2.7390 + 3.5936i
3.1420 + 0.7926i
-0.0697 - 4.2102i
실습문제 해답 12.5
1. eq1
eq1 =
x^2 == 1
subs(eq1,x,4)
ans =
16 == 1
ex1
ex1 =
x^2-1
subs(ex1,x,4)
ans =
15
EQ1
EQ1 =
X^2 == 1
subs(EQ1,'X',4)
ans =
16 == 1
EX1
EX1 =
X^2 - 1
subs(EX1,'X',4)
ans =
15
% etc
2. g = symfun(sym('x^2 + sin(x)*x'),sym('x'))
g(x) =
x*sin(x) + x^2
g('a')
48 공학도를 위한 매트랩
ans =
a*sin(a) + a^2
g(3)
ans =
3*sin(3) + 9
g([1:5])
ans =
[ sin(1) + 1, 2*sin(2) + 4, 3*sin(3) + 9, 4*sin(4) + 16, 5*sin(5) + 25]
실습문제 해답 12.6
1. ezplot(ex1)
title('Problem 1')
xlabel('x')
ylabel('y')
2. ezplot(EX1)
title('Problem 2')
xlabel('x')
ylabel('y')
3. ezplot(ex2,[-10,10])
title('Problem 3')
xlabel('x')
ylabel('y')
4. ezplot(EX2,[-10,10])
title('Problem 4')
xlabel('x')
ylabel('y')
5. Equations
with only one variable have a single valid value of x; there are
no x-y pairs.
6. ezplot(ex6)
title('Problem 6')
xlabel('x')
ylabel('y')
7. ezplot('cos(x)')
title('Problem 7')
xlabel('x')
ylabel('y')
8. ezplot('x^2-y^4 = 5')
부록E 실습문제 해답 49
title('Problem 8')
xlabel('x')
ylabel('y')
9. ezplot('sin(x)')
hold on
ezplot('cos(x)')
hold off
title('Problem 9')
xlabel('x')
ylabel('y')
10. ezplot('sin(t)', '3*cos(t)')
axis equal
title('Problem 10')
xlabel('x')
ylabel('y')
실습문제 해답 12.7
Z=sym('sin(sqrt(X^2+Y^2))')
Z=
sin(sqrt(X^2+Y^2))
1. ezmesh(Z)
title('Problem 1')
xlabel('x')
ylabel('y')
zlabel('z')
2. ezmeshc(Z)
title('Problem 2')
xlabel('x')
ylabel('y')
zlabel('z')
3. ezsurf(Z)
title('Problem 3')
xlabel('x')
ylabel('y')
zlabel('z')
4. ezsurfc(Z)
title('Problem 4')
50 공학도를 위한 매트랩
xlabel('x')
ylabel('y')
zlabel('z')
5. ezcontour(Z)
title('Problem 5')
xlabel('x')
ylabel('y')
zlabel('z')
6. ezcontourf(Z)
title('Problem 6')
xlabel('x')
ylabel('y')
zlabel('z')
7. figure(7)
ezpolar('x*sin(x)')
title('Problem 7')
8. t = sym('t');
x = t;
y = sin(t);
z = cos(t);
ezplot3(x,y,z,[0,30])
title('Problem 8')
xlabel('x')
ylabel('y')
zlabel('z')
실습문제 해답 12.8
1. diff(sym('x^2+x+1'))
ans =
2*x+1
diff(sym('sin(x)'))
ans =
cos(x)
% or define x as symbolic
x = sym('x')
x=
x
부록E 실습문제 해답 51
diff(tan(x))
ans =
1+tan(x)^2
diff(log(x))
ans =
1/x
2. diff(sym('a*x^2 + b*x + c'))
ans =
2*a*x+b
diff(sym('x^0.5 - 3*y'))
ans =
.5/x^.5
diff(sym('tan(x+y)'))
ans =
1+tan(x+y)^2
diff(sym('3*x + 4*y - 3*x*y'))
ans =
3-3*y
3. % There are several different approaches
diff(diff(sym('a*x^2 + b*x + c')))
ans =
2*a
diff(sym('x^0.5 - 3*y'),2)
ans =
-.25/x^1.5
diff(sym('tan(x + y)'),'x',2)
ans =
2*tan(x+y)*(1+tan(x+y)^2)
diff(diff(sym('3*x + 4*y - 3*x*y'),'x'))
ans =
-3
4. diff(sym('y^2 - 1'),'y')
ans =
2*y
% or, since there is only one variable
diff(sym('y^2 - 1'))
ans =
2*y
52 공학도를 위한 매트랩
%
diff(sym('2*y + 3*x^2'),'y')
ans =
2
diff(sym('a*y + b*x + c*x'),'y')
ans =
a
5. diff(sym('y^2-1'),'y',2)
ans =
2
% or, since there is only one variable
diff(sym('y^2-1'),2)
ans =
2
%
diff(diff(sym('2*y + 3*x^2'),'y'),'y')
ans =
0
diff(sym('a*y + b*x + c*x'),'y',2)
ans =
0
실습문제 해답 12.9
1. int(sym('x^2 + x + 1'))
ans =
(x*(2*x^2 + 3*x + 6))/6
% or define x as symbolic
x = sym('x')
x=
x
int(x^2 + x + 1)
ans =
(x*(2*x^2 + 3*x + 6))/6
int(sin(x))
ans =
-cos(x)
int(tan(x))
부록E 실습문제 해답 53
ans =
-log(cos(x))
int(log(x))
ans =
x*(log(x) - 1)
2. % you don’t need to specify that integration is with
% respect to x, because it is the default
int(sym('a*x^2 + b*x + c'))
ans =
(a*x^3)/3 + (b*x^2)/2 + c*x
int(sym('x^0.5 - 3*y'))
ans =
.66666666666666666666666666666667*x^(3/2)-3.*x*y
int(sym('tan(x+y)'))
ans =
-log(cos(x + y))
int(sym('3*x + 4*y - 3*x*y'))
ans =
(3/2 - (3*y)/2)*x^2 + 4*y*x
3. int(int(x^2 + x + 1))
ans =
(x^2*(x^2 + 2*x + 6))/12
int(int(sin(x)))
ans =
-sin(x)
int(int(tan(x)))
ans =
- (polylog(2, -exp(x*2*i))*i)/2 - (x*(x + log(exp(x*2*i) + 1)*2*i)*i)/2
- x*log(cos(x))
int(int(log(x)))
ans =
(x^2*(2*log(x) - 3))/4
int(int(sym('a*x^2 + b*x + c')))
ans =
(x^2*(a*x^2 + 2*b*x + 6*c))/12
int(int(sym('x^0.5 - 3*y')))
ans =
.26666666666666666666666666666667*x^(5/2)-
54 공학도를 위한 매트랩
1.5000000000000000000000000000000*y*x^2
int(int(sym('tan(x+y)')))
ans =
- (polylog(2, -exp(x*2*i + y*2*i))*i)/2
- ((x + y)*(x + y + log(exp(x*2*i + y*2*i) + 1)*2*i)*i)/2
- log(cos(x + y))*(x + y)
int(int(sym('3*x + 4*y -3*x*y')))
ans =
(1/2 - y/2)*x^3 + 2*y*x^2
4. int(sym('y^2-1'))
ans =
(y*(y^2 - 3))/3
int(sym('2*y+3*x^2'),'y')
ans =
y*(3*x^2 + y)
int(sym('a*y + b*x + c*z'),'y')
ans =
(a*y^2)/2 + (b*x + c*z)*y
5. int(int(sym('y^2-1')))
ans =
(y^2*(y^2 - 6))/12
int(int(sym('2*y+3*x^2'),'y'),'y')
ans =
(y^2*(9*x^2 + 2*y))/6
int(int(sym('a*y + b*x + c*z'),'y'),'y')
ans =
(a*y^3)/6 + ((b*x)/2 + (c*z)/2)*y^2
6. int(x^2 + x + 1,0,5)
ans =
355/6
int(sin(x),0,5)
ans =
1-cos(5)
int(tan(x),0,5)
ans =
NaN
int(log(x),0,5)
ans =
5*log(5)-5
부록E 실습문제 해답 55
실습문제 해답 13.1
1. plot(x,y,'-o')
title('Problem 1')
xlabel('x-data')
ylabel('y-data')
grid on
2. interp1(x,y,15)
ans =
34
3. interp1(x,y,15,'spline')
ans =
35.9547
4. interp1(y,x,80)
ans =
39.0909
5. interp1(y,x,80,'spline')
ans =
39.2238
6. new_x = 10:2:100;
new_y = interp1(x,y,new_x,'spline');
figure(2)
7. plot(x,y,'o',new_x,new_y)
legend('measured data','spline interpolation')
title('Problem 6 and 7')
xlabel('x-data')
ylabel('y-data')
실습문제 해답 13.2
y = 10:10:100';
x = [15, 30];
z = [23 33
45 55
60 70
82 92
111 121
140 150
56 공학도를 위한 매트랩
167 177
198 198
200 210
220 230];
1. plot(y,z,'-o')
title('Problem 1')
xlabel('y-data')
ylabel('z-data')
legend('x=15','x=30')
2. new_z = interp2(x,y,z,15,20)
new_z =
45
3. new_z = interp2(x,y,z,15,20,'spline')
new_z =
45
4. new_z = interp2(x,y,z,[20,25],y')
new_z =
26.3333 29.6667
48.3333 51.6667
63.3333 66.6667
85.3333 88.6667
114.3333 117.6667
143.3333 146.6667
170.3333 173.6667
198.0000 198.0000
203.3333 206.6667
223.3333 226.6667
실습문제 해답 13.3
x = [10:10:100];
y= [23 33
45 55
60 70
82 92
111 121
140 150
167 177
부록E 실습문제 해답 57
198 198
200 210
220 230]';
1. coef = polyfit(x,y(1,:),1)
coef =
2.3224 -3.1333
2. new_x = 10:2:100;
new_y = polyval(coef,new_x)
new_y =
Columns 1 through 6
20.0909 24.7358 29.3806 34.0255 38.6703 43.3152
Columns 7 through 12
47.9600 52.6048 57.2497 61.8945 66.5394 71.1842
Columns 13 through 18
75.8291 80.4739 85.1188 89.7636 94.4085 99.0533
Columns 19 through 24
103.6982 108.3430 112.9879 117.6327 122.2776 126.9224
Columns 25 through 30
131.5673 136.2121 140.8570 145.5018 150.1467 154.7915
Columns 31 through 36
159.4364 164.0812 168.7261 173.3709 178.0158 182.6606
Columns 37 through 42
187.3055 191.9503 196.5952 201.2400 205.8848 210.5297
Columns 43 through 46
215.1745 219.8194 224.4642 229.1091
3. figure(1)
plot(x,y(1,:),'o',new_x,new_y)
title('Problem 3 - Linear Regression Model - z = 15')
xlabel('x-axis')
ylabel('y-axis')
4. figure(2)
coef2 = polyfit(x,y(2,:),1)
coef2 =
2.2921 7.5333
new_y2 = polyval(coef2,new_x);
plot(x,y(2,:),'o',new_x,new_y2)
title('Problem 4 - Linear Regression Model -z = 30')
xlabel('x-axis')
ylabel('y-axis')
58 공학도를 위한 매트랩
실습문제 해답 13.4
1. x = -5:1:5;
y1 = x.^3 + 2.*x.^2 - x + 3;
dy_dx1 = diff(y1)./diff(x)
dy_dx =
42 22 8 0 -2 2 12 28 50 78
dy_dx_analytical1=3*x.^2 + 4*x -1
dy_dx_analytical =
54 31 14 3 -2 -1 6 19 38 63 94
table = [[dy_dx1,NaN]',dy_dx_analytical1']
table =
42 54
22 31
8 14
0 3
-2 -2
2 -1
12 6
28 19
50 38
78 63
NaN 94
% We added NaN to the dy_dx vector so that the length
% of each vector would be the same
2. a. x = -5:1:5;
y2a = sin(x);
dy_dx2a = diff(y2a)./diff(x);
dy_dx_analytical2a=cos(x);
table = [[dy_dx2a,NaN]',dy_dx_analytical2a']
table =
-0.2021 0.2837
-0.8979 -0.6536
-0.7682 -0.9900
0.0678 -0.4161
0.8415 0.5403
0.8415 1.0000
0.0678 0.5403
-0.7682 -0.4161
부록E 실습문제 해답 59
-0.7682 -0.4161
-0.8979 -0.9900
-0.2021 -0.6536
NaN 0.2837
b. x = -5:1:5;
y2b = x.^5-1;
dy_dx=diff(y2b)./diff(x);
dy_dx_analytical2b = 5*x.^4;
table = [[dy_dx2b,NaN]',dy_dx_analytical2b']
table =
2101 3125
781 1280
211 405
31 80
1 5
1 0
31 5
211 80
781 405
2101 1280
NaN 3125
c. x = -5:1:5;
y2c = 5*x.*exp(x);
dy_dx2c = diff(y2c)./diff(x);
dy_dx_analytical2c=5*exp(x) + 5*x.*exp(x);
table = [[dy_dx2c,NaN]',dy_dx_analytical2c']
table =
1.0e+003 *
-0.0002 -0.0001
-0.0004 -0.0003
-0.0006 -0.0005
-0.0005 -0.0007
0.0018 0
0.0136 0.0050
0.0603 0.0272
0.2274 0.1108
0.7907 0.4017
2.6184 1.3650
NaN 4.4524
60 공학도를 위한 매트랩
3. dy_dx31=gradient(y1)
dy_dx31 =
42 32 15 4 -1 0 7 20 39 64 78
dy_dx3a=gradient(y2a)
dy_dx3a =
Columns 1 through 6
-0.2021 -0.5500 -0.8330 -0.3502 0.4546 0.8415
Columns 7 through 11
0.4546 -0.3502 -0.8330 -0.5500 -0.2021
dy_dx3b=gradient(y2b)
dy_dx3b =
Columns 1 through 5
2101 1441 496 121 16
Columns 6 through 11
1 16 121 496 1441 2101
dy_dx3c=gradient(y2c)
dy_dx3c =
1.0e+003 *
Columns 1 through 6
-0.0002 -0.0003 -0.0005 -0.0005 0.0007 0.0077
Columns 7 through 11
0.0369 0.1438 0.5090 1.7045 2.6184
4. subplot(2,2,1)
plot(x',[[dy_dx1,NaN]',dy_dx_analytical1',dy_dx31'])
title('x^3+2x^2-x+3')
ylabel('derivative')
subplot(2,2,2)
plot(x',[[dy_dx2a,NaN]',dy_dx_analytical2a',dy_dx3a'])
title('sin(x)')
legend('forward difference','analytical','gradient')
subplot(2,2,3)
plot(x',[[dy_dx2b,NaN]',dy_dx_analytical2b',dy_dx3b'])
title('x^5-1')
xlabel('x')
ylabel('derivative')
subplot(2,2,4)
plot(x',[[dy_dx2c,NaN]',dy_dx_analytical2c',dy_dx3c'])
title('5xe^x')
부록E 실습문제 해답 61
xlabel('x')
실습문제 해답 13.5
1. x=linspace(-1,1,11);
y = x.^3 + 2*x.^2 - x + 3;
trapz(x,y)
ans =
7.36
quad('x.^3+2*x.^2 -x + 3',-1,1)
ans =
7.3333
quadl('x.^3+2*x.^2 -x + 3',-1,1)
ans =
7.3333
double(int(sym('x^3+2*x^2 -x + 3'),-1,1))
ans =
7.3333
a = -1;
62 공학도를 위한 매트랩
b = 1;
1/4*(b^4-a^4)+2/3*(b^3-a^3)-1/2*(b^2-a^2)+3*(b-a)
ans =
7.3333
2. a. y= sin(x);
trapz(x,y)
ans =
2.7756e-17
quad('sin(x)',-1,1)
ans =
0
quadl('sin(x)',-1,1)
ans =
0
double(int(sym('sin(x)'),-1,1))
ans =
0
a = -1;
b = 1;
cos(b)-cos(a)
ans =
0
b. y= x.^5-1;
trapz(x,y)
ans =
-2
quad('x.^5-1',-1,1)
ans =
-2
quadl('x.^5-1',-1,1)
ans =
-2.0000
double(int(sym('x^5-1'),-1,1))
ans =
-2
a = -1;
b = 1;
(b^6-a^6)/6-(b-a)
부록E 실습문제 해답 63
ans =
-2
c. y = 5*x.*exp(x);
trapz(x,y)
ans =
3.7693
quad('5*x.*exp(x)',-1,1)
ans =
3.6788
quadl('5*x.*exp(x)',-1,1)
ans =
3.6788
double(int(sym('5*x*exp(x)'),-1,1))
ans =
3.6788
a = -1;
b = 1;
-5*(exp(b)-exp(a)) + 5*(b*exp(b)-a*exp(a))
ans =
3.6788