function [X,Y]=myeuler(x,y,x1,n) %................................... %set initial condition and compute h h=(x1-x)/n; X=x; Y=y; %................................... %compute exact solution and error initially yexact=soln(x,y); Yexact=yexact; yerror=yexact-y; %................................... % Print table: fid = fopen('table.txt','w'); fprintf(fid,' h=%8.4f\n \n',h); fprintf(fid, ' x y yexact error\n \n'); fprintf(fid,'%8.4f %8.4f %8.4f %8.4f\n',x,y,yexact,yerror); %................................... % Euler's method for i=1:n y=y+h*f(x,y); x=x+h; X=[X;x]; Y=[Y;y]; yexact=soln(x,y); Yexact=[Yexact; yexact]; yerror=yexact-y; fprintf(fid,'%8.4f %8.4f %8.4f %8.4f\n',x,y,yexact,yerror); end fclose(fid); %................................... % Plot graph clf plot(X,Y,'o') hold on plot(X,Yexact,'+') myh=legend('Approximate Values ','Exact Values',0) plot(X,Y) plot(X,Yexact,'--') xlabel('x') ylabel('y(x)') Ha_ax=gca;set(Ha_ax,'Fontsize',12); hold off %...................................