CH ENGR 109 Study Guide - Quiz Guide: Matlab, Junkers J 1, Euler Method

35 views15 pages
9 Jan 2019
School
Professor

Document Summary

Cbe 109 - numerical and mathematical methods in chemical and biological engineering. Given the following de nitions of variables dx dt (0) = 1. The second-order ode can be rewritten as x1 = x x2 = dx dt dx1 dt dx2 dt. Then the problem can be solved using fourth-order runge-kutta. The solution at t = 10 is x(10) = 3. 6793. Sample code: clear all; x(1,:)=[1,1]; t(1)=0; t_end=10; h=0. 1; coef=[1/6,1/3,1/3,1/6]; for i=1:t_end/h t(i+1)=t(i)+h; k_1=func_nonlinsys(x(i,:),t(i)); k_2=func_nonlinsys(x(i,:)+h/2*k_1,t(i)+h/2); k_3=func_nonlinsys(x(i,:)+h/2*k_2,t(i)+h/2); k_4=func_nonlinsys(x(i,:)+h*k_3,t(i)+h); x(i+1,:)=x(i,:)+h*coef*[k_1;k_2;k_3;k_4]; 1 end x(end,1) where the function f unc nonlinsys is de ned as follows: function [dx] = func_nonlinsys(x,t) dx = zeros(1,2); dx(1,1) = x(1,2); dx(1,2)= -x(1,1)^2 + t + x(1,1) - x(1,2); end. If it is desired to plot the solution x(t) of the second order ode from t = 0 to t = 10, add the following to the end of the code above: figure(1); plot(t,x(:,1),"-ob"); xlabel("t"); ylabel("x"); title("fourth-order runge-kutta");