1.
Solve the following differential equation
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
with
and plot the result using gnuplot. Change the parameters
to
and show the graph as well.
You can use the initial condition as
Also, use
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
- start (optional): The number to start from. Default is 0.
- stop (required): The number at which to stop (exclusive).
- step (optional): The difference between each number in the sequence. Default is 1.
Examples:
- range(5) 0, 1, 2, 3, 4
- range(2,6) 2, 3, 4, 5
- range(1,10,2) 1, 3, 5, 7, 9
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);
}