Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Math308 Deadline: April 24, 2015 Programming Assignment: Initial-Value Problems

ID: 649758 • Letter: M

Question

Math308 Deadline: April 24, 2015 Programming Assignment: Initial-Value Problems for Ordinary Differential Equations You may develop code in whatever programming language you prefer, e.g., Matlab, Python, C++, Fortran, however you will only receive credit for the assignment if I can run your code. If you write in a language not listed above, or I cannot compile your code for some other reason, than I may ask you to demonstrate your code in person The sample code is only provided in Matlab You may work with a partner on the code, however each person must turn in their own report. Please include the name of your partner on your report 1. FIRST ORDER PROBLEM Consider the initial-value problem y(t) = f(t,y), y(to) = yo, from t = to to t = tf. 1.1. Euler's Method. The mfile euler.m implements Euler's method to solve the first order initial value problem (1). Verify the code works correctly by comparing the results with Table 8.1.1 on page 445 of the book (Boyce & DiPrima) for problem (1) with f(t, y)-1-t + 4y, yo-1, from t = 0 to t 2 Your code should work for arbitrary choice of the step-size h. Include in your report the output for h 1/ 100 1.2. Improved Euler's Method. Now, implement the improved Euler's methods de- scribed in Chapter 8.2, page 454. Use your code for the improved Euler's method to solve the problem (1) with the following data f(t, y) = 1-t+4y, yo = 1, from t 0 to t=2 Verify your code works by reproducing the results in Table 8.2.1 of the book. Include these results in your repor 1.3. Runge-Kutta Method (RK4). Now, implement the Runge-Kutta method de- scribed in Chapter 8.3, page 459. Use your code for the Runge-Kutta method to solve the problem (1) with the following data f(t, y) = 1-1 + 4y, yo = 1, from t 0 to t 2 Verify your code works by reproducing the results in Table 8.3.1 of the book. Include these results in your report

Explanation / Answer

Yes your question was difficult...and contains lot of questions...

Euler

public class CalCulateEuler {
private static void CalCulateEuler (Call f, double y0, int a, int b, int h) {
int t = a;
double y = y0;
while (t < b) {
System.out.println ("" + t + " " + y);
t += h;
y += h * f.calculate (t, y);
}
System.out.println ("DONE");
}

public static void main (String[] args) {
Call EulerCooling = new EulerCooling ();
int[] steps = {0, 1, 2};
for (int step : steps) {
System.out.println ("Steps : " + step);
CalCulateEuler (EulerCooling, 100.0, 0, 100, step);
}
}
}

// interface
interface Call {
public double calculate (int time, double t);
}

class EulerCooling implements Call {
public double calculate (int time, double t) {
return 1-t;
}
}
---------------------------------------------------------------------------------------------------------------------------------

Improved Euler


function x=x(n,t0,t1,y0) //n->no. of t values,t0,t1 end points,yo initial condition
h=(t1-t0)/n;
t(1)=t0;
x(1)=y0;
for i=1:n //looping as per our our intervals
t(i+1)=t(i)+h;
x(i+1)=x(i)+((ex(t(i),x(i))+ex(t(i+1),x(i)+h*ex(t(i),x(i))))*h)/2; //Here actual calcuation done
end;
IE=[t',x']
plot(t,x) //we are plotting the results
title('Inproved euler') //setting title...which is optional

------------------------------------------------------------------------------------------------------------------

Runge-kutta

x0 = 0;
y0 = 0;
h = 0.05;
T = 0.1;

while x0<T
x1 = x0 + h;
k1 = x0 - y0;
k2 = (x0 + h) - (y0 + h*k1);
k3 = (x0 + 0.5*h) - (y0 + 0.5*h*(k1+k2)*0.5);
y1 = y0 + (k1 + k2 + 4*k3)/6*h;
% Forward Moving
x0 = x1;
y0 = y1;
end
disp('Runge- Gutta Method') //printing program name
disp(['Value at x =' num2str(x0) ' is y(' num2str(x0) ') = ' num2str(y0)])