Functions commands plot hold on meshc title hold off surf xlabel polar surfc ylabel bar surfl axis barh pcolor grid on pie shading faceted grid off plot3 shading flat legend meshgrid shading interp semilogx contour colormap semilogy contourf getframe loglog mesh movie color marker style line style y =yellow .point o=circle -=solid m=magenta x=x-mark +plus dotted c =cyan s=square d=diamond -.dash-dot r =red *star p=pentagram -dashed g green h=hexagram b =blue v triangle down w =white ^=triangle up k black >triangle right <triangle left tic/toc save/load
Functions & commands plot title xlabel ylabel axis grid on grid off legend semilogx semilogy loglog hold on hold off polar bar barh pie plot3 meshgrid contour contourf mesh meshc surf surfc surfl pcolor shading faceted shading flat shading interp colormap getframe movie tic/toc save/load
Example 1 function revolve(r1,r2,n) theta =0:pi/100:2*pi; x1 r1*cos(theta); y1 r1*sin(theta); range r1+2*r2; tic;号Starts the clock jj=1; for t 0:pi/50:2*pi center2x (r1+r2)*cos(t); center2y =(r1+r2)*sin(t); x2 =r2*cos (theta)+center2x; y2 =r2*sin(theta)+center2y; p1ot(x1,Y1,'r',x2,Y2,'b'); axis equal axis([-range range -range range]);axes always same mov(jj)=getframe; jj=jj+1; end time =toc;$Stores how long the program took to run fprintf('Time for preparation =$g\n',time); tic; movie(mov,n); time toc; fprintf('Time for playing =&g\n',time); return
Example 1 function revolve(r1,r2,n) theta = 0:pi/100:2*pi; x1 = r1*cos(theta); y1 = r1*sin(theta); range = r1+2*r2; tic; % Starts the clock jj = 1; for t = 0:pi/50:2*pi center2x = (r1+r2)*cos(t); center2y = (r1+r2)*sin(t); x2 = r2*cos(theta)+center2x; y2 = r2*sin(theta)+center2y; plot(x1,y1,'r',x2,y2,'b'); axis equal axis([-range range -range range]); % axes always same mov(jj) = getframe; jj = jj+1; end time =toc; % Stores how long the program took to run fprintf('Time for preparation = %g\n', time); tic; movie(mov,n); time = toc; fprintf('Time for playing = %g\n',time); return
Example 2 function cycloid(r,n) 号w=1rad/s theta =0:pi/100:2*pi; center y =r; cyc_x =[] cyc_y [] jj=1; for t 0:pi/30:n*2*pi center x =r*t; x =r*cos(theta)+center x; y =r*sin(theta)+center y; current x center x-r*sin(t); current y center_y-r*cos(t); cyc x [cye x,current x]; cye_y [cyc_y,current y]; plot (x,y,cyc_x,cyc_y,current x,current y,'rd'); axis equal axis ([-r,2*pi*r*n+r,0,3*r]) mov(jj)=getframe; jj=jj+1; end movie (mov) return
Example 2 function cycloid(r,n) % w=1rad/s theta = 0:pi/100:2*pi; center_y = r; cyc_x = []; cyc_y = []; jj = 1; for t = 0:pi/30:n*2*pi center_x = r*t; x = r*cos(theta) + center_x; y = r*sin(theta) + center_y; current_x = center_x-r*sin(t); current_y = center_y-r*cos(t); cyc_x = [cyc_x, current_x]; cyc_y = [cyc_y, current_y]; plot(x,y,cyc_x,cyc_y,current_x,current_y,'rd'); axis equal axis([-r,2*pi*r*n+r,0,3*r]) mov(jj) = getframe; jj = jj+1; end movie(mov) return
Example 3 function out combine(obj,n) if n==1 out obj(:); return end if n==length(obj) out obj; return end out [] for ii=1:length(obj)-n+1 first obj(ii); tail obj(ii+1:length (obj)); tail combinat combine(tail,n-1); loop out [first*ones (size(tail combinat,1),1), tail combinat]; out [out;loop out]; end ★Fibonacci Number ★Tower of Hanoi
Example 3 function out = combine(obj, n) if n==1 out = obj(:); return end if n==length(obj) out = obj; return end out = []; for ii = 1:length(obj)-n+1 first = obj(ii); tail = obj(ii+1:length(obj)); tail_combinat = combine(tail, n-1); loop_out = [first*ones(size(tail_combinat,1), 1), tail_combinat]; out = [out; loop_out]; end ★ Fibonacci Number ★ Tower of Hanoi