# hw10_p01 - if (fid4 &amp;amp;lt; 0) error('could not open...

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

clc clear c h = input('h: '); xstart = input('x_min: '); xend = input('x_max: '); ystart = input('y at x_min: '); yend = input('y at x_max: '); tolerance = input('Tolerance: '); % int_1 is for making table using 'mod' operator % int_1 = 1; n = 1; n p_initial = input('1 st initial guess: '); p x=xstart:h:xend; x Y1(1,n) = ystart; Y2(1,n) = p_initial; Y for i=2:length(x) [Y1(i,n), Y2(i,n)] = hw10_p01RK4(h,x(i-1),Y1(i-1),Y2(i-1)); end e diff(n) = Y1(length(x),n) - yend; d p_initial = input('2 nd initial guess: '); n=n+1; n Y1(1,n) = ystart; Y2(1,n) = p_initial; Y for i=2:length(x) [Y1(i,n), Y2(i,n)] = hw10_p01RK4(h,x(i-1),Y1(i-1,n),Y2(i-1,n)); end e diff(n) = Y1(length(x),n) - yend; d while ( abs( Y2(1,n) - Y2(1,n-1) ) > tolerance ) p_initial = Y2(1,n) - diff(n)*( Y2(1,n) - Y2(1,n-1) )/( diff(n) - diff(n-1) ); n=n+1; Y1(1,n) = ystart; Y2(1,n) = p_initial; for i=2:length(x) [Y1(i,n), Y2(i,n)] = hw10_p01RK4(h,x(i-1),Y1(i-1,n),Y2(i-1,n)); end diff(n) = Y1(length(x),n) - yend; end e % open file fid4 = fopen('hw10_p01_answer.txt','a'); % 'wt' means "write text"

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (fid4 &lt; 0) error('could not open file &quot;hw10_p01_answer.txt&quot;'); end; fprintf(fid4,'\n\nh = %10.5f\tTolerance = %10.7f\n', h, tolerance); fprintf(fid4,'y(%10.5f)=%10.5f, y(%10.5f)=%10.5f\n', xstart, ystart, xend, yend); fprintf(fid4,'1 st guess = %10.5f, 2 nd guess = %10.5f\n', Y2(1,1), Y2(1,2)); fprintf(fid4,'\t\t\tx\t\t\t\tY1(x)\n'); fprintf(fid4,'iter'); for j = 1:n fprintf(fid4,'%10d',j); end fprintf(fid4,'\n'); for i = 1:length(x) if mod(i-1,int_1) == 0 fprintf(fid4,'%10.5f',x(i)); for j = 1:n fprintf(fid4,'%10.5f', Y1(i,j)); end fprintf(fid4,'\n'); end end fclose(fid4); f n figure(1), plot(x,Y1(:,1),'*', x,Y1(:,3),'d', x,Y1(:,n),'o',x,Y1(:,n)) xlabel('x'); ylabel('Temperature'); legend('1 st', '3 rd', 'Converged'); title('Problem 01 in HW#10: figure'); t figure(2), plot(x,Y1(:,n),'o',x,Y1(:,n)) xlabel('x'); ylabel('Temperature'); legend('Converged'); title('Problem 01 in HW#10: figure for only converged solution');...
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.

### Page1 / 2

hw10_p01 - if (fid4 &amp;amp;lt; 0) error('could not open...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online