Diese Datei stammt aus Wikimedia Commons und kann von anderen Projekten verwendet werden. Die Beschreibung von deren Dateibeschreibungsseite wird unten angezeigt.
functionmain()% colorsred=[0.8670.060.14];blue=[0129205]/256;green=[020070]/256;black=[000];white=[111]*0.99;cardinal=[1963058]/256;cerulean=[0123167]/256;denim=[2196189]/256;cobalt=[071171]/256;pblue=[04983]/256;teracotta=[22611491]/256;tene=[205870]/256;wall_color=pblue;spring_color=cobalt;mass_color=tene;a=0.65;bmass_color=a*mass_color+(1-a)*black;% linewidth and fontsizelw=2;fs=20;ww=0.5;% wall widthms=0.25;% the size of the mass sw=0.1;% spring widthcurls=8;A=0.2;% the amplitude of spring oscillationsB=-1;% the y coordinate of the base state (the origin is higher, at the wall)% Each of the small lines has length ll=0.05;N=15;% times per oscillation No=1;% number of oscillationsfori=1:N*No% set up the plotting windowfigure(1);clf;holdon;axisequal;axisoff;t=2*pi*(i-1)/(N-0)+pi/2;% current timeH=A*sin(t)+B;% position of the mass% plot the spring from Start to EndStart=[0,0];End=[0,H];[X,Y]=do_plot_spring(Start,End,curls,sw);plot(X,Y,'linewidth',lw,'color',spring_color);% Here we cheat. We modify the point B so that the mass is attached exactly at the end of the% spring. This should not be necessary. I am too lazy to to the exact calculation.K=length(X);End(1)=X(K);End(2)=Y(K);% plot the wall from which the spring is hangingplot_wall(-ww/2,ww/2,l,lw,wall_color);% plot the mass at the end of the springX=[-ms/2ms/2ms/2-ms/2-ms/2ms/2]+End(1);Y=[00-ms-ms00]+End(2);H=fill(X,Y,mass_color,'EdgeColor',bmass_color,'linewidth',lw);% the bounding boxSx=-0.4*ww;Sy=B-A-ms+0.05;Lx=0.4*ww+l;Ly=l;axis([Sx,Lx,Sy,Ly]);plot(Sx,Sy,'*','color',white);% a hack to avoid a saveas to eps bugsaveas(gcf,sprintf('Spring_frame%d.eps',1000+i),'psc2')%save the current framedisp(sprintf('Spring_frame%d',1000+i));%show the frame number we are atpause(0.1);end% The following command was used to create the animated figure. % convert -antialias -loop 10000 -delay 7 -compress LZW Spring_frame10* Simple_harmonic_oscillator.giffunction[X, Y]=do_plot_spring(A, B, curls, sw);% plot a 3D spring, then project it onto 2D. theta controls the angle of projection.% The string starts at A and ends at B% will rotate by theta when projecting from 1D to 2Dtheta=pi/6;Npoints=500;% spring lengthD=sqrt((A(1)-B(1))^2+(A(2)-B(2))^2);X=linspace(0,1,Npoints);XX=linspace(-pi/2,2*pi*curls+pi/2,Npoints);Y=-sw*cos(XX);Z=sw*sin(XX);% b gives the length of the small straight segments at the ends% of the spring (to which the wall and the mass are attached)b=0.05;% stretch the spring in X to make it of length D - 2*bN=length(X);X=(D-2*b)*(X-X(1))/(X(N)-X(1));% shift by b to the right and add the two small segments of length bX=[0,X+bX(N)+2*b];Y=[Y(1)YY(N)];Z=[Z(1)ZZ(N)];% project the 3D spring to 2DM=[cos(theta)sin(theta);-sin(theta)cos(theta)];N=length(X);fori=1:N;V=M*[X(i),Z(i)]';X(i)=V(1);Z(i)=V(2);end% shift the spring to start from 0X=X-X(1);% now that we have the horisontal spring (X, Y) of length D,% rotate and translate it to go from A to BTheta=atan2(B(2)-A(2),B(1)-A(1));M=[cos(Theta)-sin(Theta);sin(Theta)cos(Theta)];N=length(X);fori=1:N;V=M*[X(i),Y(i)]'+A';X(i)=V(1);Y(i)=V(2);endfunctionplot_wall(S, E, l, lw, wall_color)% Plot a wall from S to E.no=20;spacing=(E-S)/(no-1);plot([S,E],[0,0],'linewidth',1.8*lw,'color',wall_color);V=l*(0:0.1:1);fori=0:(no-1)plot(S+i*spacing+V,V,'color',wall_color)end
Public domainPublic domainfalsefalse
Ich, der Urheberrechtsinhaber dieses Werkes, veröffentliche es als gemeinfrei. Dies gilt weltweit. In manchen Staaten könnte dies rechtlich nicht möglich sein. Sofern dies der Fall ist: Ich gewähre jedem das bedingungslose Recht, dieses Werk für jedweden Zweck zu nutzen, es sei denn, Bedingungen sind gesetzlich erforderlich.