Programming in C and then plotting in Octave or Matlab
Homework #3 (Key Assignment with six problems) Due: Wednesday, 12/02/2020 This course specifically assesses the ability to do programming in engineering related problems. This HW #3 assignment is designated as key assignment. If its score is less than 49 points, which is 70% of its maximum possible score (70 points, with each problem worth 10 points), one will not pass the course even if the one scores perfectly on all exams and other assignments. Problem 1. Solve the following differential equation y¢ = y, y(0) = 1, by: · Exactly (analytically). · Euler's method. · Runge-Kutta method. Plot their results in a single graph in GnuPlot. Use 0≤t≤1.0 and h = 0.1. Use the following syntax in GnuPlot: plot "data1.txt", "data2.txt", exp(x) where "data1.txt" contains data from the Euler method, "data2.txt" contains data from the R-K method. As shown below, you can also plot their results in a single graph using Octave (see Pages 7-11 of Lecture notes Part IV in “Modules”), which is optional. · Save data1.txt and data2.txt in working folder of Octave · Use the following codes in Command Window of Octave: x=[0:0.1:1] dataA = load ("data1.txt"); dataB = load ("data2.txt"); plot (dataA (:,1), dataA (:,2), dataB (:,1), dataB (:,2), x, exp(x)); Problem 2. a). Numerically solve the following differential equations using the Euler method du dt = v (1) dv dt = - k v - u3 + 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. b). Solve Eqs. (1) and (2) using Octave, and plot the result using GnuPlot. Do you get similar results using Euler’s method and Octave? Problem 3. · Solve the following two simultaneous equations using Octave: æ ç ç ç è 1 4 8 1 ö ÷ ÷ ÷ ø æ ç ç ç è x y ö ÷ ÷ ÷ ø = æ ç ç ç è 4 7 ö ÷ ÷ ÷ ø · Calculate using quadv and trapz, respectively. Please submit your (interactive or a script m-file) codes and output of Octave. Problem 4. Provide the plots generated by the following highlighted commands: x=[0:0.1:10]; %initial value, increment, final value y=sin(x); plot(x, sin(x)); %calls software Gnuplot to plot x=linspace(0,2, 20) % between 0 and 2 with 20 divisions plot(x, sin(x)) %%%%%%%%%%%%%%%%%% x=[0: 0.2 : 10]; y=1/2 * sin(2*x) ./ x; xlabel('X-axis'); ylabel('sin(2x)/x'); plot(x, y); close; %%%%%%%%%%%%%%%%% t=[0: 0.02: 2*pi]; plot(cos(3*t), sin(2*t)) %%%%%%%%%%%%%%%%%%%% x=[0: 0.01: 2*pi]; y1=sin(x); y2=sin(2*x); y3=sin(3*x); plot(x, y1, x, y2,x, y3); %%%%%%%%%%%%%% xx=[-10:0.4:10]; yy=xx; [x,y]=meshgrid(xx,yy); z=(x .^2+y.^2).*sin(y)./y; mesh(x,y,z) surfc(x,y,z) contour(x,y,z) close Problem 5. i) Translate the following C code to equivalent Octave m-file, and ii). run this m-file in Octave. Please submit the m-file and its output. #include
int main() { double x, y, z; int i,n; x=y=z=0.0; printf("Enter # of iteration = "); scanf("%d", &n); for (i=0; i #include 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;">2*n;><2*n; i="i+2)" s3="s3" +="" f(a="" +="" i*h);="" printf("%lf\n",="" (h/3.0)*(s1+="" 4.0*s2="" +="" 2.0*s3))="" ;="" return="" 0;="" }="" problem="" 7.="" translate="" the="" following="" c="" code="" to="" a="" fortran="" code:="" #include="">2*n;> #include double f(double t, double y) {return y;} main() { double h=0.1, t, y, k1,k2,k3,k4; int i; /* initial value */ t=0.0; y=1.0; for (i=0; i<=10; i++) { printf("t= %lf rk= %lf exact=%lf\n", t, y, exp(t)); k1=h*f(t,y); k2=h*f(t+h/2, y+k1/2.0); k3=h*f(t+h/2, y+k2/2.0); k4=h*f(t+h, y+k3); y= y+(k1+2.0*k2+2.0*k3+k4)/6.0; t=t+h; } return 0; } show your fortran code and example run. instruction for submission template for hw 1. hw 03 mm/dd/year (submission date) section 002 (or 3) doe, john (last name, first name) 1325 (last 4 digits of your student id) problem 1 problem 2 problem 3 i++)="" {="" printf("t="%lf" rk="%lf" exact="%lf\n"," t,="" y,="" exp(t));="" k1="h*f(t,y);" k2="h*f(t+h/2," y+k1/2.0);="" k3="h*f(t+h/2," y+k2/2.0);="" k4="h*f(t+h," y+k3);="" y="y+(k1+2.0*k2+2.0*k3+k4)/6.0;" t="t+h;" }="" return="" 0;="" }="" show="" your="" fortran="" code="" and="" example="" run.="" instruction="" for="" submission="" template="" for="" hw="" 1.="" hw="" 03="" mm/dd/year="" (submission="" date)="" section="" 002="" (or="" 3)="" doe,="" john="" (last="" name,="" first="" name)="" 1325="" (last="" 4="" digits="" of="" your="" student="" id)="" problem="" 1="" problem="" 2="" problem="">=10; i++) { printf("t= %lf rk= %lf exact=%lf\n", t, y, exp(t)); k1=h*f(t,y); k2=h*f(t+h/2, y+k1/2.0); k3=h*f(t+h/2, y+k2/2.0); k4=h*f(t+h, y+k3); y= y+(k1+2.0*k2+2.0*k3+k4)/6.0; t=t+h; } return 0; } show your fortran code and example run. instruction for submission template for hw 1. hw 03 mm/dd/year (submission date) section 002 (or 3) doe, john (last name, first name) 1325 (last 4 digits of your student id) problem 1 problem 2 problem 3>