% example script using eulerSolve function clc % clear screen clear % clear all variables xMin = 0; xMax = 1; yInitial = 1; Cval = 2/yInitial + xMin*xMin; % step size h=0.1; [xSol,ySol] = eulerSolve(xMin,yInitial,xMax,h); % plot a graph. % To understand 'bo-' etc, see the MATLAB help % (the b is blue, the o is the plot symbol, - is for a line) hold off % make sure that this will be a new graph plot( xSol,ySol, 'bo-', 'Linewidth', 3 ) % make the font nice and big % gca refers to properties of the current fig set(gca,'FontSize',30); % compute exact solution nExact = 100; for i=1:nExact xExact(i) = xMin + i*(xMax-xMin)/nExact; yExact(i) = 2/( Cval - xExact(i)*xExact(i) ); end hold on % add an extra line to the existing graph % k stands for black here plot( xExact,yExact, 'k-', 'Linewidth', 3 ) legend('h=0.1','exact','Location','NorthWest') % smaller step size h=0.02; [xSol2,ySol2] = eulerSolve(xMin,yInitial,xMax,h); % m is "magenta" here hold off % NEW graph, created from scratch % note: the order of these following commands does matter, % we have to do the first "plot" before setting labels, fonts, etc plot( xExact,yExact, 'k-', 'Linewidth', 4 ) xlabel('x') ylabel('y(x)') xlim([ xMin xMax]); set(gca,'FontSize',30) % need to do this again for the new graph hold on %% add the numerical solutions plot( xSol,ySol, 'bo-', 'Linewidth', 3 ) plot( xSol2,ySol2, 'mo-', 'Linewidth', 3 ) legend('exact', 'h=0.1', 'h=0.02', 'Location','NorthWest') hold off % this command will print to a file print -dpdf 'eulerFig.pdf' -bestfit