hw3_CODE

# hw3_CODE - Problem 3 iii clear all clc dt=0.001...

• Homework Help
• 10
• 100% (3) 3 out of 3 people found this document helpful

This preview shows pages 1–3. Sign up to view the full content.

Problem 3 ( iii ) clear all ; clc dt=0.001; epsilon=0.1; [t,x]=RK4(dt,epsilon); [x1an,x2an]=exact(dt,epsilon); figure(1),clf plot(t,x(1,:), 'bx' ),hold on % x(t) plot(t,x1an, 'r-' ) set(gca, 'fontsize' ,12) xlabel( 't' , 'fontsize' ,12, 'fontweight' , 'bold' ) ylabel( 'x1(t)' , 'fontsize' ,12, 'fontweight' , 'bold' ) legend( 'RK4' , 'Exact' ) title([ 'Solution via the RK4 Method and dt=' ,num2str(dt), '\epsilon=' ,num2str(epsilon)], 'fontsize' ,12, 'fontweight' , 'bo ld' ) figure(2),clf plot(t,x(2,:), 'bx' ),hold on % x'(t) plot(t,x2an, 'r-' ) set(gca, 'fontsize' ,12) xlabel( 't' , 'fontsize' ,12, 'fontweight' , 'bold' ) ylabel( 'x2(t)' , 'fontsize' ,12, 'fontweight' , 'bold' ) legend( 'RK4' , 'Exact' ) title([ 'Solution via the RK4 Method and dt=' ,num2str(dt), '\epsilon=' ,num2str(epsilon)], 'fontsize' ,12, 'fontweight' , 'bo ld' ) clear all ; clc dt=0.001; epsilon=0.01; [t,x]=RK4(dt,epsilon); [x1an,x2an]=exact(dt,epsilon); figure(3),clf plot(t,x(1,:), 'bx' ),hold on % x(t) plot(t,x1an, 'r-' ) set(gca, 'fontsize' ,12) xlabel( 't' , 'fontsize' ,12, 'fontweight' , 'bold' ) ylabel( 'x1(t)' , 'fontsize' ,12, 'fontweight' , 'bold' ) legend( 'RK4' , 'Exact' ) title([ 'Solution via the RK4 Method and dt=' ,num2str(dt), '\epsilon=' ,num2str(epsilon)], 'fontsize' ,12, 'fontweight' , 'bo ld' ) figure(4),clf plot(t,x(2,:), 'bx' ),hold on % x'(t) plot(t,x2an, 'r-' ) set(gca, 'fontsize' ,12) xlabel( 't' , 'fontsize' ,12, 'fontweight' , 'bold' ) ylabel( 'x2(t)' , 'fontsize' ,12, 'fontweight' , 'bold' ) legend( 'RK4' , 'Exact' ) title([ 'Solution via the RK4 Method and dt=' ,num2str(dt), '\epsilon=' ,num2str(epsilon)], 'fontsize' ,12, 'fontweight' , 'bo ld' )

This preview has intentionally blurred sections. Sign up to view the full version.

Runge Kutta Method (RK4) function [t,x]=RK4(dt,epsilon) tspan=[0 5]; % Integrate from t=0 to t=5 x0=[0;1/epsilon]; % Initial conditions x0=[x(0);x'(0)] t0=tspan(1); tfinal=tspan(end); t=[t0:dt:tfinal]; if t(end)~=tfinal t=[t tfinal]; end % Create a solution matrix of the appropriate dimensions n=length(t); x=zeros(2,n); x(:,1)=x0; % Form the Runge-Kutta method h=dt; for k=1:n-1 k1=pendulum(t(:,k),x(:,k),epsilon); k2=pendulum(t(:,k)+h/2,x(:,k)+(h/2)*k1,epsilon); k3=pendulum((t(:,k)+h/2),x(:,k)+h*k2/2,epsilon); k4=pendulum(t(:,k)+h,x(:,k)+h*k3,epsilon); x(:,k+1)=x(:,k)+1/6*h*(k1+2*(k2+k3)+k4); end end xprime function xprime=pendulum(t,x,epsilon) A=[0 1;(-1/epsilon) (-1/epsilon)]; xprime=A*x; end Exact solution function [x1an,x2an]=exact(dt,epsilon) tspan=[0 5]; % Integrate from t=0 to t=5 x0=[0;1/epsilon]; % Initial conditions x0=[x(0);x'(0)] t0=tspan(1); tfinal=tspan(end); t=[t0:dt:tfinal]; n=size(t); if t(end)~=tfinal t=[t tfinal]; end
This is the end of the preview. Sign up to access the rest of the document.
• Spring '14
• ylabel, Runge–Kutta methods, Runge–Kutta–Fehlberg method

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern