# channelflow.org

### Site Tools

gibson:teaching:fall-2014:math445:hw1solns

Here's an ideal solution to HW1. Note the following

• I used `format compact` to minimize empty lines and thus save electrons and trees.
• I included as much of the problem statement as possible (dropping hard-to-type mathematical expressions
• I edited out errors, calls to `help` and so on.
• In some cases I showed several ways to accomplish the same task. I don't expect that of you.

My homework set prints in just three pages, if I show just one way of solving each problem!

Please be sure to make your homework solutions as compact and readable as possible, using `format compact` and editing out extraneous material!

```% John F. Gibson
% 2014-09-12
% Math 445 HW1

format compact

% Problem 1: Given two numeric variables x and y, write a Matlab expression that
% evaluates to true (1) if  and  have opposite signs and false (0) otherwise.
% Test the expression by evaluating it with the following pairs of numbers
% (x,y) = (-5, 4), (5,4), (5,-4), (0,-2), and (3,0).

% There are many ways to answer this question, ranging from the most obvious and
% literal to the most readable and efficiently executed. Here's the obvious and
% literal way
x=-5; y=4;  ((x<0) && (y>0)) || ((x>0) && (y<0))
ans =
1

x=5; y=4;   ((x<0) && (y>0)) || ((x>0) && (y<0))
ans =
0

x=5; y=-4;  ((x<0) && (y>0)) || ((x>0) && (y<0))
ans =
1

x=0; y=-2;  ((x<0) && (y>0)) || ((x>0) && (y<0))
ans =
0

x=3; y=0;   ((x<0) && (y>0)) || ((x>0) && (y<0))
ans =
0

% Here's a less complicated expression that accomplishes the same
x=-5; y=4;  x*y < 0
ans =
1

% and another
x=-5; y=4;  sign(x)*sign(y) == -1
ans =
1

% Problem 2: Find a matlab expression to computer the combined resistance RT of
% three resistors R1,R2, R3.

% The simple, straightforward way
R1=5; R2=3; R3=4;
RT = 1/(1/R1 + 1/R2 + 1/R3)
RT =
1.2766

% Here's a slicker way to do the same
R = [5 3 4];
RT = 1/sum(R.^(-1))
RT =
1.2766

% or even better
RT = 1/sum([5 3 4].^(-1))
RT =
1.2766

% Problem 3:
% (a) Create a row vector x whose elements are the numbers 5, 7, 10, 1.
x = [5 7 10 1]
x =
5     7    10     1

% (b) Create a column vector x whose elements are the numbers 5, 7, 10, 1.
x = [5 ; 7 ; 10 ; 1]
x =
5
7
10
1

x = [5 7 10 1]'
x =
5
7
10
1

% (c) Use colon syntax to create a row vector x whose elements start at 0, end at 1,
% and increase in steps of 0.1.
x = 0:0.1:1
x =
Columns 1 through 10
0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000    0.7000    0.8000    0.9000
Column 11
1.0000

% (d) Determine the dimension of x from (c) and assign the value to the variable d
% (using Matlab, not by counting!).
d = length(x)
d =
11
d = size(x,2)
d =
11

% (e) Use the linspace function to create a 10-dimensional vector of numbers evenly spaced between 0 and 1.
x = linspace(0,1,10)
x =
0    0.1111    0.2222    0.3333    0.4444    0.5556    0.6667    0.7778    0.8889    1.0000

% Problem 4
% (a) Create the given matrix
A = [ 3 9 2 ; -1 4 6 ; 5 -2 0]
A =
3     9     2
-1     4     6
5    -2     0

% (b) Change A(2,3) to 7
A(2,3) = 7
A =
3     9     2
-1     4     7
5    -2     0

% (c) Assign the third column of A to the variable v
v = A(:,3)
v =
2
7
0

% (d) Change the first row of A to 8,1,4
A(1,:) = [8 1 4]
A =
8     1     4
-1     4     7
5    -2     0

% Problem 5. Create the given A matrix and b vector and solve Ax=b
A = [4 2 ; -1 5]
A =
4     2
-1     5

b = [3 4]'
b =
3
4

x = A\b
x =
0.3182
0.8636

% verify solution satisfies Ax=b
A*x
ans =
3
4

A*x-b
ans =
0
0

norm(A*x-b)
ans =
0

% Problem 6: Use Matlab to solve the problem. Nilanjana has 40 coins worth \$6.40.
% They're all quarters and nickels. How many nickels and how many quarters does
A = [1 1 ; 0.10 0.25]
A =
1.0000    1.0000
0.1000    0.2500

b = [40 ; 6.40]
b =
40.0000
6.4000

x = A\b
x =
24.0000
16.0000

% She has 24 dimes and 16 quarters. That sums to 40 coins, and 24 * \$0.10 + 16 * \$0.25 = \$6.40

A*x
ans =
40.0000
6.4000

% Problem 7: Suhasini has 44 coins worth \$7.50. They're all quarter, dimes, and nickels.
% She has twice as many dimes as nickels. How many of each type of coin does she have?
% Find the answer, and then verify that the solution satisfies the problem. The equations
% are
%
% n + d + q = 44
% 0.05 n + 0.10 d + 0.25 q = 7.50
% d - 2n = 0

A = [1 1 1 ; 0.05 0.10 0.25 ; -2 1 0]
A =
1.0000    1.0000    1.0000
0.0500    0.1000    0.2500
-2.0000    1.0000         0

b = [44 7.50 0]'
b =
44.0000
7.5000
0

x = A\b
x =
7
14
23

% She has 7 nickels, 14 dimes, and 23 quarters. Verifying...
n = x(1); d = x(2); q = x(3);

n + d + q

ans =
44

0.05*n + 0.10*d + 0.25*q

ans =
7.5000

d - 2*n

ans =
0

norm(A*x-b)
ans =
0``` 