8
CMOLER EXERCISES source: Experiments with MATLAB - CMoler - book 1. INTRO and ITERATIONS notes: exp(1)=e=2.718281828459046, log(exp(1))=1, log10(10)=1, Loga(x)=logb(x)/loga(b) lim x(x^n/n^x) lim(ln(x^n)/ln(n^x)) 1/ln(x) 0. pi is not exactly π, pi=355/113=3.14159265358979 _______________________________________ 1. INTRO and ITERATIONS format long q=(1+sqrt(5))/2 = 1.618033988749895 1.1 43^2 = 1849 (-3)^4 = 81 sin(1) = 0.841470984807897 4^(3^2) = 262144 (-3)^(0.25) = 0.930604859102100 + 0.930604859102099i sin(pi/180) = 0.017452406437284 (4^3)^2 = 4096 sin(pi/3) = 0.866025403784439 (32)^0.25 = 2.378414230005442 (-2)^(-4/3) = -0.198425131496025 + 0.343682409249651i arcsin(1)/pi = ??? Undefined function or method 'arcsin' for input arguments of type 'double'. 1.2.a F = 62 C=5/9*(F-32) C = 16.6667 1.2.b C = 100 F=C*9/5+32 F = 212 1.3 pg7 from barn-megaparsec to teaspoon units conversion: 1 megaparsec = 0.61742486 teaspoons. 1.4 phi=(1+sqrt(5))/2 = 1.618033988749895 x=-1:.025:4; plot(x,x,'-',x,sqrt(1+x),'-',phi,phi,'o' ) Crossing on x=pi . Improving graph presentation: set(axes_handle,’XGrid’,’on’); set(axes_handle,’YGrid’,’on’); Grid, box, axes, set 1.5 example FOR: format long for k=1:31 x=sqrt(1+x) end 1.6.- example WHILE: format long x=3 while x ~= sqrt(1+x) x = sqrt(1+x) end things that can be improved: sqrt(1+x) is evaluated twice each loop, and comparing R to Z is ok as long as working floating point as it is the case, otherwise 1.7.- PLOT example phi=(1+sqrt(5))/2 x=-1:.025:4; plot(x,x,'-',x,sqrt(1+x),'-',phi,phi.'o' 1/7

Solutions to C Moler MATLAB Experiments Chapter 1

Embed Size (px)

DESCRIPTION

Solutions to C Moler MATLAB Experiments Chapter 1

Citation preview

CMOLER EXERCISESsource: Experiments with MATLAB - CMoler - book

1. INTRO and ITERATIONS

notes: exp(1)=e=2.718281828459046, log(exp(1))=1, log10(10)=1, Loga(x)=logb(x)/loga(b)lim x(x^n/n^x) lim(ln(x^n)/ln(n^x)) 1/ln(x) 0. pi is not exactly , pi=355/113=3.14159265358979

_______________________________________

1. INTRO and ITERATIONS

format longq=(1+sqrt(5))/2 = 1.6180339887498951.1 43^2 = 1849 (-3)^4 = 81 sin(1) = 0.841470984807897 4^(3^2) = 262144 (-3)^(0.25) = 0.930604859102100 + 0.930604859102099i sin(pi/180) = 0.017452406437284 (4^3)^2 = 4096 sin(pi/3) = 0.866025403784439 (32)^0.25 = 2.378414230005442 (-2)^(-4/3) = -0.198425131496025 + 0.343682409249651i arcsin(1)/pi = ??? Undefined function or method 'arcsin' for input arguments of type 'double'.1.2.a F = 62C=5/9*(F-32)C = 16.6667

1.2.bC = 100F=C*9/5+32F = 212

1.3 pg7 from barn-megaparsec to teaspoon units conversion: 1 megaparsec = 0.61742486 teaspoons.

1.4 phi=(1+sqrt(5))/2 = 1.618033988749895x=-1:.025:4;plot(x,x,'-',x,sqrt(1+x),'-',phi,phi,'o')

Crossing on x=pi . Improving graph presentation: set(axes_handle,XGrid,on); set(axes_handle,YGrid,on); Grid, box, axes, set

1.5 example FOR:format longfor k=1:31 x=sqrt(1+x) end

1.6.- example WHILE:format longx=3while x ~= sqrt(1+x)x = sqrt(1+x)end

things that can be improved: sqrt(1+x) is evaluated twice each loop, and comparing R to Z is ok as long as working floating point as it is the case, otherwise equality may never be reached regardless of how many loops iterated.

example improved WHILE:x = 3y = Inf;while abs(x-y) > eps(x)y = x;x = sqrt(1+x)end1.7.- PLOT examplephi=(1+sqrt(5))/2x=-1:.025:4;plot(x,x,'-',x,sqrt(1+x),'-',phi,phi.'o')

1.8 sq(x)=x+1 solution, phi, approximated with Taylor seriesn=10 % n=10 p/q error < 1.47e-04, n=100 error abs(xn+1-xn)/abs(x1-x0) (f'(x0))^n

x=sqrt(1+x) x0=1.618033988770158f(x)=sqrt(1+x)) f'(x)=-0.5*(1+x)^(-0.5)(f'(x0))^10 = 7.940057378387408e-6(f'(x0))^32 = 4.780085345013851e-17

MSWindows calculator (ver 6.1 build 7600): x=sqrt(1+x) x0=1.618033988770158, abs(x0-sqrt(1+x0)) = 1.4001493531043385682988887303586e-11

1.36%LOTKA Lotka-Volterra predator-prey model.function yp = lotka(t,y)global ALPHA BETAyp = [y(1) - ALPHA*y(1)*y(2); -y(2) + BETA*y(1)*y(2)];% ALPHA = BETA = 0.1 race mode

%file lotka2.mglobal ALPHA BETAALPHA = 0.01BETA = 0.03[t,y] = ode23(@lotka,[0,10],[1; 1]);plot(t,y);

% ALPHA = 1, BETA = 0.03 persecution mode

% ALPHA = BETA = 0.4 , combat modeglobal ALPHA BETAALPHA = 0.4BETA = 0.4[t,y] = ode23(@lotka,[0,10],[1; 1]);plot(t,y);

1/7