hw08_p03 - y(i)=hw08_p03AB4(i,h,y(i-1),fprime);

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
% File name: hw08_p03.m clc clear x(1) = input('x1: '); y(1) = input('y1: '); h = input('h: '); % int_1 is for making table using 'mod' operator if h == 1/4 int_1 = 1; elseif h == 1/8 int_1 = 2; elseif h == 1/16 int_1 = 4; else int_1 = 1; end x=x(1):h:2; %For fourth-order Adams-Bashforth, we need previous 4 t,y pairs. We have %one ( given pair 0,2 ) so we need to generate 3 more using Runge-Kutta: for i=2:4 y(i)=hw08_p03RK4(x(i-1),y(i-1),h); end %Get the values of f' for the points we've already calculated or been %given (so we avoid recalculating these when doing Adams-Bashforth): for i=1:4 fprime(i)=hw08_p03deriv(x(i),y(i)); end for i=5:length(x) %Fourth-order Adams-Bashforth (followed by calculating the new value of %fprime needed to make the next calculation):
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: y(i)=hw08_p03AB4(i,h,y(i-1),fprime); fprime(i)=hw08_p03deriv(x(i),y(i)); %AM4 corrector y(i) = hw08_p03AM4(i,h,y(i-1),fprime); fprime(i)=hw08_p03deriv(x(i),y(i)); end %Display integrated function values and calculated values: % ytrue = x./(1.0+x.^2); % (a) ytrue = 1.0/4.0*(3.0*exp(-4.0*x) + 1.0); % (b) % open file fid4 = fopen('hw08_p03_answer.txt','a'); % 'wt' means "write text" if (fid4 < 0) error('could not open file "hw08_p02_answer.txt"'); end; fprintf(fid4,'\n\nh = %10.5f\n', h); fprintf(fid4,'x\t\t\t\ty(x)\t\t\t\tY(x)\t\t\t\terror\n'); for i = 1:length(x) if mod(i-1,int_1) == 0 fprintf(fid4,'%15.5f%15.5f%15.5f%15.5f\n', . .. x(i),y(i),ytrue(i), y(i)-ytrue(i)); end end fclose(fid4);...
View Full Document

This note was uploaded on 02/22/2010 for the course CHE 348 taught by Professor Chelikowsky during the Spring '08 term at University of Texas at Austin.

Ask a homework question - tutors are online