Homework #11
Due: April 21, 2025
Problem 1. Solve the following differential equation

y" + 3 y′+ 2 y = 0,     y(0) = 0, y′(0) = 1.
by:

    (a) Exactly (analytically).
    (b) Euler's method.
and plot your numerical solution and the exact result together in a single graph with GNUPlot. Use 0 ≤ x ≤ 2 and h = 0.01.
The data file should look like this where the first column is x, the second column is from Euler's method and the third column is the exact solution,
x        Euler      Exact
---------------------------

0.000000 0.000000 0.000000
0.100000 0.099833 0.091123
.....................
.....................

1.500000 0.730990 0.712344
1.600000 0.862848 0.793431
1.700000 1.036497 1.012222
1.800000 1.257597 1.257597
1.900000 1.532184 1.532184
2.000000 1.866667 1.866667
In GNUPlot, use the following syntax:
plot "data.txt" using 1:2, title "Euler", "data.txt" using 1:3 title "Exact"


Problem 2. Translate the following C code to equivalent Python code.
/* Simpson's rule */
#include <stdio.h>
#include  <math.h>

double f(double x)
 {return 4.0/(1.0+x*x);}

int main()
{
 int i, n ;
 double a=0.0, b=1.0 , h, s1=0.0, s2=0.0, s3=0.0, x;

 printf("Enter number of partitions (must be even) = ");
 scanf("%d", &n) ;

  h = (b-a)/(2.0*n) ;

  s1 = (f(a)+ f(b));

 for (i=1; i<2*n; i=i+2) s2 = s2 + f(a + i*h);

 for (i=2; i<2*n; i=i+2) s3 = s3 + f(a + i*h);

    printf("%f\n", (h/3.0)*(s1+ 4.0*s2 + 2.0*s3)) ;
 return 0;
}
Note the syntax of
range(start, stop, step)

where
Examples:



File translated from TEX by TTH, version 4.03.
On 13 Apr 2025, 21:56.