Math 248: Computers and Numerical Algorithms Homework, Spring 2020
1. Change your working directory to your Flash drive, and enter the following Matlab program (call it adding.m):
x = input(’Enter a value for x \n’);
y = input(’Enter a value for y \n’);
z=x+y; % computes the sum
fprintf(’The sum of x and y is %4.1f \n’,z)
• Run this program with x = 1.234 and y = 5.5.
• What is the answer? What would happen if x and y previously held values?
Call me over when you have finished this part.
2. Experiment with the fprintf statement. Re-run your program with the %4.1f changed to %5.5f. Repeat with %4.2e, %4.3E, %d, %g. Run the Matlab command doc fprintf and click on the blue “formatSpec” to find out what the different formatting specifications mean.
3. Experiment by removing semicolons (;). Does every executable statement in Matlab need a semicolon? What does the semicolon do?
4. Write a Matlab program (as simple as in question 1) called ctof.m to convert a temperature from Celsius to Fahrenheit: the conversion formula is F = 9C/5 + 32. Don’t worry about detailed commenting, there should just be an input, a formula, and an output.
Call me over when you have finished this part.
5. Write a Matlab program called fordiff1.m that asks the user to input an x value and a small h value. The program should calculate the square root of x, approximate the derivative of the
√
For the approximate derivative, the definition of derivative suggests f′(x) ≈ 1 (f(x+h)−f(x))
function
x and print them both to the screen. You should be descriptive in your output.
√
h
for given x and small h, with f(x) =
Make sure you follow the guidelines with regard to commenting, preamble, and nicely for-
x. matted output. Submit your Matlab file.
6. Write Matlab code that takes as input an integer n, and returns 15 +25 +35 +···+n5. As a check, with input 100, the output should be 171708332500.
Call me over when you have finished this part.
7. Write Matlab code called big.m that takes as input a list of positive numbers one at a time, stopping when zero is entered. It should output the number of numbers entered (before the zero), as well as the largest number in the list. For example, if the input numbers (one per line) were 1, 5, 2, 7, 3, 0, then the output would be that there are 5 numbers, and the maximum is 7. I expect you will use a loop with a conditional statement inside it.
Make sure you follow the guidelines with regard to commenting, preamble, and nicely for- matted output. Submit your Matlab file.
8. With n = 2, replace h by 2h in Taylor’s theorem. Combine the equations for f (x + h) and f(x + 2h) and eliminate the f′′(x) term to verify that
′ 1 h2 ′′′ ′′′
2f (c2)−f (c1). This is the second order forward difference formula for the derivative.
f (x)= (−3f(x)+4f(x+h)−f(x+2h))+ 2h 3
I don’t want you to hand this in, but the exercise will be very useful for future potential exam questions…
9. Starting with your fordiff.m code, write code called testdiff.m where instead of choosing h, use a loop to output the error for h = 10−1, 10−2, · · · , 10−8. You will have to calculate the true derivative at your input x. I recommend a for loop with i=1:8 and use this to set the value of h. Do the output errors agree with the theory – that reducing h by a factor of ten reduces the error by a factor of ten? Add your answer as comment to the code.
Add a second loop to your code that replaces the first order forward difference approximation with the second order forward difference approximation. Do the output errors agree with the theory – that reducing h by a factor of ten reduces the error by a factor of one hundred? Add your answer as comment to the code. We shall revisit this odd behavior later.
Make sure you follow the guidelines with regard to commenting, preamble, and nicely for- matted output. Submit your Matlab file.
10. A Pythagorean triplet is a triple of positive integers (a, b, c) such that a2 + b2 = c2 with a