1.1 Homework Set No. 1: Problem 1
1.1 Homework Set No. 1: Problem 1
1 1
Problem 1
Problem 2:
√
For some values of x, the function f (x) = x2 + 1 − x cannot be accurately computed
by using this formula. Explain and find a way around the difficulty.
Problem 3:
√ √
How can the values of the function f (x) = x+2− x be computed accurately when
x is large?
The goal of this exercise is to get started with Matlab. You will go through:
• simple plots;
p3 (x) = a3 x3 + a2 x2 + a1 x + a0 (1.1)
xi 0 5 10 15
(1.2)
yi 3 8 −2 9
2
Preparations:
a) Show that the coefficients to the interpolating polynomial (1) can be found by
solving
1 0 0 0 a0 3
1 5 25 125 a1 8
= (1.3)
1 10 100 1000 a2 −2
1 15 225 3375 a3 9
Introduction
Find a computer with Matlab installed in it. Start Matlab, by either click on the
appropriate icon(Windows), or by typing in matlab (unix or linux). You should get a
command window on the screen.
Go through the examples in Gockenbach’s notes.
Linear equations
Solve the system of linear equations (3).
The interpolating polynomial should be
Simple plots
Plot the interpolating points and the interpolating polynomial. In addition to the func-
tion plot try also:
grid on/off: add or remove a grid on the plot.
xlabel(’text’): put text under x-axis.
ylabel(’text’): put text next to y-axis.
title(’text’): put text above the plot.
Help!
As we will see later, Matlab has many build-in numerical functions. One among them
is a function that does polynomial interpolation. But what is the name, and how to use
it? Here we will see how we could use lookfor to find the function, and help to get a
description on how to use it.
lookfor keyword: Look after the “keyword” among Matlab functions.
1.1. HOMEWORK SET NO. 1 3
Here X is a vector with the x-values, and Y is a vector with y values from the table in
(2) in our example. N is the order of the polynomial, and in our case we use 3. The
function returns a vector with the coefficients of the polynomial.
Helpdesk
It might happen that you think the information coming from help is a bit creepy. It
would be nice to have some examples or such things. Fortunately Matlab has a web-
based online manual. You can start it by type in
helpdesk
and a web reader window will pop up, with a start-page for Matlab. Go to Matlab
function polyfit, and you get more self-explaining information. You should also check
the function polyval.
Now, use these functions polyfit and polyval to compute and plot the interpolating
polynomial for table (2).
In this problem we will consider the effect of computer arithmetic on the evaluation of
the quadratic formula
√
−b ± b2 − 4ac
r1,2 =
2a
for the roots of p(x) = ax2 + bx + c.
4
a. Write a Matlab function, call it quadroots, that takes a, b, c as input and returns
the two roots as output,like
Use the formula above for the computation. Run it on a few test problems to make sure
that it works before you go ahead with the following problems.
• 2x2 + 6x − 3
• x2 − 14x + 49
• 3x2 − 123454321x + 2
What do you get? Why are the results so bad for the last polynomial?
c. The product of the roots of ax2 + bx + c is of course c/a. Use this fact to improve
your program, call it smartquadroots, to see if you get better results. (Matlab has a
command roots(p) which returns the roots of the polynomial with coefficients in p.
roots is smarter than quadroots and will give accurate answers here. You can use it
to check your results from smartquadroots function.)
What to hand in: Hand in two Matlab function m-files (quadroots.m and smartquad-
roots.m), the results you get, and your comments.