Docsity

Scientific Computation I - Programming Assignment 2 | MATH 661, Assignments of Mathematics

Toggle sidebar
bg1
Programming Homework (due Wednesday Sept 20th)
Note: In case there are some typos found, or I expand on some of the
problem (very likely), download the file again over the weekend. Please
send questions about any aspect of this, or if you want a more concrete
description of what you want to do. Right now it is somewhat loose, and
I urge people to think a little creatively and explore things. The coding
effort in C++ should be minimal. I should be able to answer any matlab or
DataTank questions you have, and you shouldn’t hesitate to ask.
1. Write a function that takes in a set of (x,y) points, and returns a
polynomial in a divided difference form. Something like
DTDoubleArray PolynomialInterpolation(const DTDoubleArray &x,
const DTDoubleArray &y)
{
int N = x.m()-1;
DTMutableDoubleArray returnArray(2,N+1);
// returnArray(0,i) = x(i)
// returnArray(1,i) = corresponding Newton divided difference.
return returnArray;
}
Then another function that takes in the polynomial from the function
above and a list of x values where it should evaluate and returns the
y values.
DTDoubleArray yValues(const DTDoubleArray polynomial,const DTDoubleArray &x)
{
int m = x.m();
DTMutableDoubleArray returnArray(m);
// returnArray(i) = polynomial evaluated at x(i)
return returnArray;
}
Now drive it from matlab or DataTank, and draw the result.
You should test the following cases
•Simple function such as sin(x) on [1,2] and let Nbe 2,3,4,5,6,7,8,9.
Draw both the function as well as the error (i.e. exact-computed
pf2

Partial preview of the text

Download Scientific Computation I - Programming Assignment 2 | MATH 661 and more Assignments Mathematics in PDF only on Docsity!

Programming Homework (due Wednesday Sept 20th)

Note: In case there are some typos found, or I expand on some of the problem (very likely), download the file again over the weekend. Please send questions about any aspect of this, or if you want a more concrete description of what you want to do. Right now it is somewhat loose, and I urge people to think a little creatively and explore things. The coding effort in C++ should be minimal. I should be able to answer any matlab or DataTank questions you have, and you shouldn’t hesitate to ask.

  1. Write a function that takes in a set of (x,y) points, and returns a polynomial in a divided difference form. Something like

DTDoubleArray PolynomialInterpolation(const DTDoubleArray &x, const DTDoubleArray &y) { int N = x.m()-1; DTMutableDoubleArray returnArray(2,N+1); // returnArray(0,i) = x(i) // returnArray(1,i) = corresponding Newton divided difference.

return returnArray; }

Then another function that takes in the polynomial from the function above and a list of x values where it should evaluate and returns the y values.

DTDoubleArray yValues(const DTDoubleArray polynomial,const DTDoubleArray &x) { int m = x.m(); DTMutableDoubleArray returnArray(m); // returnArray(i) = polynomial evaluated at x(i) return returnArray; }

Now drive it from matlab or DataTank, and draw the result. You should test the following cases

  • Simple function such as sin(x) on [1, 2] and let N be 2,3,4,5,6,7,8,9. Draw both the function as well as the error (i.e. exact-computed

as a function of x). Draw the error in [1, 2] as well as outside it, i.e. [0, 3].

  • Try a bad function such as a step function, or abs(x) which has a jump in the first derivative.
  1. Here you are checking the convergence properties of a single stencil, by using it on a uniform mesh, i.e. breaking a large interval into a collection of equal intervals, and letting the interval size go to 0. Implement both a trapezoid rule as well as the scheme that you derived in the last homework. Find the local error scaling by computing the global error and add one. In order to do this, you should compute the error for a variety of step sizes, and plot the error versus step size on a log-log plot and fit it.
  2. Implement an adaptive scheme based on Trapezoid rule. You should implement the method that I wrote down in class

Integrate(a,b,eps) c = (a+b)/2; Total = Single(a,b); Left = Single(a,c); Right = Single(b,c); error = (Left+Right-Total)/(2^(k-1)-1); if (abs(error)<eps) return Left+Right; else return Integrate(a,c,eps/2) + Integrate(c,b,eps/2);

You need to keep track of all of the a,b,c’s that you compute and draw those as lines on top of the plot of the function that you hand in. It is perfectly reasonable to hard code your function in C++, but if you driving things from DataTank you can pass in the function as an argument. Run the following analysis on this:

(a) Run this for a smooth function like exp(x) (b) Try a function some not so smooth like sin(b ∗ x ∗ (1 − x)) (c) What happens if you do a non-smooth function like step func- tions, or the absolute value function.