HW2 Code - Question 2 function Q2 clear all clc intv=[0.5...

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

Question 2 function Q2 clear all ; clc intv=[0.5 0.1 0.01]; for i=1:3 % Define the step size h=intv(1,i); xspan=[0 5]; y0=1; x=[xspan(1):h:xspan(2)]; n=size(x); ye=Euler(x,y0,n,h); figure(i),clf plot(x,ye, 'b.' , 'markersize' ,15),hold on yh=Heun(x,y0,n,h); figure(i) plot(x,yh, 'r^' , 'markersize' ,8),hold on ytk=result(x,n); figure (i) plot(x,yh, 'go' , 'markersize' ,12),hold on erke=Error(x,ye,n); erkh=Error(x,yh,n); max(abs(erke)) max(abs(erkh)) set(gca, 'fontsize' ,16) xlabel( 'x' , 'fontsize' ,16) ylabel( 'y(x)' , 'fontsize' ,16) legend( 'Euler' , 'Heuns' , 'Exact' ) title([ 'Solution to the IVP for h=' ,num2str(h)], 'fontsize' ,10, 'fontweight' , 'bold' ) end end %analytical result function yek=result(x,n) for i=1:(n(2)-1) yek(1,i)=sin(x(1,i))+cos(x(1,i)); end end %Global error function error=Error(yek,y,n) for i=1:(n(2)-1) error(1,i)=yek(1,i)-y(1,i); end end function y=Euler(x,y0,n,h) y(1,1)=y0; for i=1:(n(2)-1) y0=y0+h*yprime(x(1,i),y0); y(1,i+1)=y0; end end function y=Heun(x,y0,n,h) y(1,1)=y0;
Image of page 1

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

for i=1:(n(2)-1) y0=y0+1/2*h*(yprime(x(1,i),y0)+yprime(x(1,i+1),y0+h*yprime(x(1,i),y0))); y(1,i+1)=y0; end end function yprime=yprime(x,y) yprime=-y+2*cos(x); end Question 3C function Q3
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '14
  • United States Census Bureau

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern