Docsity

Numerical Methods HW: Temp Conversion, Matrix Creation, & Polynomial Eval. - Prof. Luke Ol, Assignments of Computer Science

Toggle sidebar
bg1
CS 257: Numerical Methods
Spring 2006
Homework, Set 2 Due Thursday February 2, 2006
(-) Start EACH PROBLEM on a SEPARATE piece of paper (This is important since we may
assign each problem to a different grader).
(-) Put your NETID and HW NUMBER on top of EACH PAGE clearly,
e.g. ā€œnetid: zamani hw2ā€.
(-) Write descriptive solutions. Comment your code!
(-) Include your curves/graphs (and other supporting materials) in your write-up.
(-) Don’t use handwritten code (unless you want to lose points), copy-paste your code into
your write-up or attach a proper print of code to your papers.
(-) Please write everything in a ā€œportraitā€ style (not landscape).
(-) Please number problems according to numbers presented in the homework write-up that
appears on the course page, NOT according to the numbers in the textbook.
(-) Please type your homework or hand-write it legibly (but yet attach a print of your codes
to your handwritten stuff).
(1) (NMM Ch. 3 #12) Write two function m-files, FtoC and CtoF, to convert temperatures from degrees
Farenheit to degrees Celsius and from degrees Celsius to degrees Farenheit. Each function should
have one input parameter and one output parameter. Test your functions with
>> FtoC( CtoF(100) )
>> CtoF( FtoC(32) )
>> FtoC(0:10:100)
>> CtoF(0:10:100)
along with any other tests you think is appropriate.
(2) (NMM Ch. 3 #15) Write a function tridiag(c,d,n) where nis the dimension of a matrix and allow c
and dto be vectors or scalars. The function should create the matrix
D=










c1d10 0 Ā·Ā· Ā· 0
d1c2d20Ā·Ā· Ā· 0
0d2c3d3Ā·Ā· Ā· 0
.
.
..
.
...........
.
.
0 0 .........dnāˆ’1
0 0 0 Ā·Ā· Ā· dnāˆ’1cn










if cand dare vectors, or create the matrix
D=










c d 0 0 Ā·Ā· Ā· 0
d c d 0Ā·Ā·Ā· 0
0d c d Ā· Ā·Ā· 0
.
.
..
.
...........
.
.
0 0 .........d
0 0 0 Ā·Ā· Ā· d c










if cand dare scalars. Note that Dis symmetric, i.e., D=DT. (Hint: Check the size of cand dto
see if these input values are vectors or scalars.)
pf3

Partial preview of the text

Download Numerical Methods HW: Temp Conversion, Matrix Creation, & Polynomial Eval. - Prof. Luke Ol and more Assignments Computer Science in PDF only on Docsity!

CS 257: Numerical Methods

Spring 2006

Homework, Set 2 Due Thursday February 2, 2006

(-) Start EACH PROBLEM on a SEPARATE piece of paper (This is important since we may

assign each problem to a different grader).

(-) Put your NETID and HW NUMBER on top of EACH PAGE clearly,

e.g. ā€œnetid: zamani hw2ā€.

(-) Write descriptive solutions. Comment your code!

(-) Include your curves/graphs (and other supporting materials) in your write-up.

(-) Don’t use handwritten code (unless you want to lose points), copy-paste your code into

your write-up or attach a proper print of code to your papers.

(-) Please write everything in a ā€œportraitā€ style (not landscape).

(-) Please number problems according to numbers presented in the homework write-up that

appears on the course page, NOT according to the numbers in the textbook.

(-) Please type your homework or hand-write it legibly (but yet attach a print of your codes

to your handwritten stuff).

(1) (NMM Ch. 3 #12) Write two function m-files, FtoC and CtoF, to convert temperatures from degrees

Farenheit to degrees Celsius and from degrees Celsius to degrees Farenheit. Each function should

have one input parameter and one output parameter. Test your functions with

FtoC( CtoF(100) )

CtoF( FtoC(32) )

FtoC(0:10:100)

CtoF(0:10:100)

along with any other tests you think is appropriate.

(2) (NMM Ch. 3 #15) Write a function tridiag(c,d,n) where n is the dimension of a matrix and allow c

and d to be vectors or scalars. The function should create the matrix

D =

c 1

d 1

d 1

c 2

d 2

0 d 2

c 3

d 3

. (^) d nāˆ’ 1

0 0 0 Ā· Ā· Ā· d nāˆ’ 1

c n

if c and d are vectors, or create the matrix

D =

c d 0 0 Ā· Ā· Ā· 0

d c d 0 Ā· Ā· Ā· 0

0 d c d Ā· Ā· Ā· 0

. (^) d

0 0 0 Ā· Ā· Ā· d c

if c and d are scalars. Note that D is symmetric, i.e., D = D

T

. (Hint: Check the size of c and d to

see if these input values are vectors or scalars.)

(3) (NMM Ch. 3 #17) Write the MATLAB statements that use a loop and the fprintf function to produce

the following output:

(a)

1^2 = 1

2^2 = 4

3^2 = 9

4^2 = 16

5^2 = 25

(b)

(0.10)^2 = 0.

(0.20)^2 = 0.

(0.30)^2 = 0.

(0.40)^2 = 0.

(0.50)^2 = 0.

(4) (NMM Ch. 3 #18 - #20)

(I) Write a horner function that uses Horner’s rule to evaluate a polynomial of arbitrary degree. (Cf.

Example 3.7 on page 113). Use the function definition

function p = horner(b,x)

where b is the vector of coefficients that define the polynomial. The return value p is the value

of the polynomial at x. Test your function with b = [1 2 -1 0] at x = 1, 2 , 3 , 4 , 5.

(II) Extend the above function so that it returns a vector of polynomial values if the input x is a

vector.

(III) Extend the above function so that it can be called in two ways, viz.,

p = horner(b,x)

and

[p, pp] = horner(b,x)

where p is the value of the polynomial at x and pp (p prime, p

′ = dp/dx) is the value of the first

derivative of p(x) evaluated at x. Make sure that your function returns p and pp as vectors if x

is a vector.

(5) (NMM Ch. 3 #35) Given the x vector

x = [21 22 23 24];

and B matrix

B = ones(3,3);

write the one-line vectorized copy operation that has the same effect as the following scalar loop:

k = 0;

for i=2:

for j=1:

k = k + 1;

B(i,j) = x(k);

end

end