Homework #08
Due: August 04, 2025
1. Solve the following differential equation

y′ = − x2 y,     y(0) = 1,
by:

    (a) Exactly (analytically).
    (b) Euler's method.
    (c) Runge-Kutta method.
and plot the three results in a single graph. Use 0 ≤ x ≤ 1 and h = 0.1.
Use the following syntax:
plot [0:1] "data1.txt" with line, "data2.txt" w l, sin(x)
where "data1.txt" contains data from the Euler method and "data2.txt" contains data from the R-K method.


2. Numerically solve the following differential equations using the Euler method
du

dt
=
v
(1)
dv

dt
=
k vu3 + B cos t,
(2)
with
k = 0.1,     B = 11.0,
and plot the result using gnuplot. Change the parameters to
k = 0.4,     B = 20.0
and show the graph as well. You can use the initial condition as
u(0) = 1,     v(0) = 1.
Also, use
h = 0.01,     i = 10000.
3. 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:
4. Execute the following program and figure out what it does.
#include <stdio.h>

int main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}





File translated from TEX by TTH, version 4.03.
On 30 Jul 2025, 12:05.