% % Math 451 9/19/12 class % % Some discussion of Prob 4.4 and 4.5 on Newton's % method for single functions and systems % % (I will post some notes on systems that I wrote % on the board soon.) % % How to display errors/data y=[10^(-2) 10^(-4) 10^(-6)] y = 0.010000000000000 0.000100000000000 0.000001000000000 plot(1:3,y,'.') plot(1:3,y,'-o') help plot PLOT Linear plot. PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. If X is a scalar and Y is a vector, disconnected line objects are created and plotted as discrete points vertically at X. PLOT(Y) plots the columns of Y versus their index. If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)). In all other uses of PLOT, the imaginary part is ignored. Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a character string made from one element from any or all the following 3 columns: b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star (none) no line y yellow s square k black d diamond w white v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram For example, PLOT(X,Y,'c+:') plots a cyan dotted line with a plus at each data point; PLOT(X,Y,'bd') plots blue diamond at each data point but does not draw any line. PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples, where the X's and Y's are vectors or matrices and the S's are strings. For example, PLOT(X,Y,'y-',X,Y,'go') plots the data twice, with a solid yellow line interpolating green circles at the data points. The PLOT command, if no color is specified, makes automatic use of the colors specified by the axes ColorOrder property. By default, PLOT cycles through the colors in the ColorOrder property. For monochrome systems, PLOT cycles over the axes LineStyleOrder property. Note that RGB colors in the ColorOrder property may differ from similarly-named colors in the (X,Y,S) triples. For example, the second axes ColorOrder property is medium green with RGB [0 .5 0], while PLOT(X,Y,'g') plots a green line with RGB [0 1 0]. If you do not specify a marker type, PLOT uses no marker. If you do not specify a line style, PLOT uses a solid line. PLOT(AX,...) plots into the axes with handle AX. PLOT returns a column vector of handles to lineseries objects, one handle per plotted line. The X,Y pairs, or X,Y,S triples, can be followed by parameter/value pairs to specify additional properties of the lines. For example, PLOT(X,Y,'LineWidth',2,'Color',[.6 0 0]) will create a plot with a dark red line width of 2 points. Example x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10) See also plottools, semilogx, semilogy, loglog, plotyy, plot3, grid, title, xlabel, ylabel, axis, axes, hold, legend, subplot, scatter. Overloaded methods: timeseries/plot Reference page in Help browser doc plot home semilogy(1:3,y,'-o') % Newton's method for f(x)=x^2 - A = 0 % simplify A=2; x=2; x = 0.5*(x+A/x); format long x x = 1.500000000000000 x = 0.5*(x+A/x); x x = 1.416666666666667 sqrt(2) ans = 1.414213562373095 home x = 0.5*(x+A/x) x = 1.414215686274510 sqrt(2) ans = 1.414213562373095 x = 0.5*(x+A/x) x = 1.414213562374690 x = 0.5*(x+A/x) x = 1.414213562373095 clear home f=@(x) x^2-2 f = @(x)x^2-2 fprime = @(x) 2x ??? fprime = @(x) 2x | {Error: Unexpected MATLAB expression. } fprime = @(x) 2*x fprime = @(x)2*x x=2; x=x-f(x)/fprime(x) x = 1.500000000000000 x=x-f(x)/fprime(x) x = 1.416666666666667 x=x-f(x)/fprime(x) x = 1.414215686274510 x=2; for j=1:6 x=x-f(x)/fprime(x) end x = 1.500000000000000 x = 1.416666666666667 x = 1.414215686274510 x = 1.414213562374690 x = 1.414213562373095 x = 1.414213562373095 x=2 x = 2 for j=1:6 xlast=x x=x-f(x)/fprime(x) error=abs(xlast-x) end xlast = 2 x = 1.500000000000000 error = 0.500000000000000 xlast = 1.500000000000000 x = 1.416666666666667 error = 0.083333333333333 xlast = 1.416666666666667 x = 1.414215686274510 error = 0.002450980392157 xlast = 1.414215686274510 x = 1.414213562374690 error = 2.123899820016817e-06 xlast = 1.414213562374690 x = 1.414213562373095 error = 1.594724352571575e-12 xlast = 1.414213562373095 x = 1.414213562373095 error = 2.220446049250313e-16 clear home % Newton for a 2x2 system x=[1; 1.3]; F=[x(1)^2+x(2)^2-1; x(1)^2-x(2)^2]; J=2*[x(1) x(2);x(1) -x(2)]; F F = 1.690000000000000 -0.690000000000000 J J = 2.000000000000000 2.600000000000000 2.000000000000000 -2.600000000000000 x=x - J/F {??? Error using ==> mrdivide Matrix dimensions must agree. } x=x - J\F x = 0.750000000000000 0.842307692307692 F=[x(1)^2+x(2)^2-1; x(1)^2-x(2)^2] F = 0.271982248520710 -0.146982248520710 J=2*[x(1) x(2);x(1) -x(2)]; J J = 1.500000000000000 1.684615384615384 1.500000000000000 -1.684615384615384 x=x - J\F x = 0.708333333333333 0.717957499121883 F=[x(1)^2+x(2)^2-1; x(1)^2-x(2)^2] F = 0.017199081656459 -0.013726859434237 J=2*[x(1) x(2);x(1) -x(2)]; x=x - J\F x = 0.707107843137255 0.707188776346328 sqrt(2)/2 ans = 0.707106781186548 F=[x(1)^2+x(2)^2-1; x(1)^2-x(2)^2] F = 1.0e-03 * 0.117467216437461 -0.114463563996026 J=2*[x(1) x(2);x(1) -x(2)]; J J = 1.414215686274510 1.414377552692656 1.414215686274510 -1.414377552692656 x=x - J\F x = 0.707106781187345 0.707106785940021 sqrt(2)/2 ans = 0.707106781186548 % ongoing bonus homework: put this in mfiles % and clean it up diary off