Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Scientific Computing Midterm

1124 views
�
�FVc@sQddlZddlZddljZddlmZde	fd��YZ
dS(i����N(tAxes3Dt	AttractorcBs�eZdZdddddddd�Zd	�Zd
�Zd�Zd�Zej	d
ddg�dd�Z
dd�Zd�Z
d�Zd�Zd�Zd�Zd�Zd�ZRS(s� Here we begin by initializing our parameteres and store them as numpy arrays and/or setting 
    their default values. We also use these values to calculate the time increment (self.dt).g$@g<@g @g@ggT@i'cCs�tj|||g�}||_||_||_||_||_||_||_|j|j|j|_	tj
|j|j|j�|_tj
�|_dS(N(tnptarraytstptbtparamststarttendtpointstdttlinspacetttpdt	DataFrametsolution(tselfRRRRR	R
tinarr((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pyt__init__
s							!cCsi|\}}}|j\}}}|||}||||}	||||}
tj||	|
g�S(s< This method was created in hindsight to simplfy code further on in this Attractor class.
            As we can see, this is where we work with our given set of differential equations and
            convert them into terms more suitable for coding purposes and return the resulting numpy
            array.
        (RRR(Rtarrtx0ty0tz0RRRtxtytz((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytgivenscCs||j|�|j}|S(s� The euler method here takes a numpy array of length three as an argument, proceedes to
            calculate the the first order Euler increment of the differential equations from our given
            method, and returns the desired k1 value.
        (RR(RRtk1((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pyteuler%scCsB|j|�}|j|||jd�}|||j}|S(s� Here we have the rk2 method which in large is very similar to the euler method, however,
            this method calculates and returns the second order Runge-Kutta increment.
        g@(RR(RRtk1ftk2ftk2((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytrk2/scCs�|j|�}|j|||jd�}|j|||jd�}|j|||j�}||jd|d|d||}|S(s� Again, we have a near identical method here with the exception of how far we take the
            incrementation. In rk4 we calculate and return the fourth order Runge-Kutta increment.
        g@g@i(RR(RRRRtk3ftk4ftk4((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytrk4;s)g�������?ic
s�|dkr�j}n9|dkr0�j}n!|dkrH�j}n	dGHdS�fd�dD�}tj|��_|}xUt�j�D]D}|\}}}	|�j	|||	g�jj
|<||�}q�W�jS(s� This method, evolve, takes a numpy array of length three and an integer as parameters.
            The initial values (x0, y0, and z0) are given default values of 0.1, 0.0, and 0.0
            respectively. The integer value order may take the quantities of 1, 2, or 4, defaulting 
            to 4, depending on which method is desired for incrementation. In this method we also
            generate our pandas DataFrame and store it and return it as self.solution.
        iiis"
 !!!Order was not 1, 2, or 4!!! 
cs%i|]}tj�j�|�qS((RtzerosR
(t.0R(R(sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pys
<dictcomp>Zs	ttxyzN(RR!R%tNoneRRRtrangeR
Rtloc(
Rtr0tordertatddtxyztiRRR((RsZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytevolveIs #cCsd}|jj|�dS(sa This is a very simple method to save our self.solution DataFrame to a CSV file on disk.
        ssolution.csvN(Rtto_csv(Rtfilename((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytsavedscCsFtjd�tjd�tj|jd|jd�tj�dS(sW This method simply labels axes and plots out t variable by our x(t) variable.
        R
sx(t)RN(tplttxlabeltylabeltplotRtshow(R((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytplotxjs

cCsFtjd�tjd�tj|jd|jd�tj�dS(sW This method simply labels axes and plots out t variable by our y(t) variable.
        R
sy(t)RN(R6R7R8R9RR:(R((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytplotyrs

cCsFtjd�tjd�tj|jd|jd�tj�dS(sW This method simply labels axes and plots out t variable by our z(t) variable.
        R
sz(t)RN(R6R7R8R9RR:(R((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytplotzzs

cCsFtjd�tjd�tj|jd|jd�tj�dS(s� Now we keep on the same plotting track in this method except we plot our results
            against one another. Namely, we're plotting x(t) vs y(t).
        sx(t)sy(t)RRN(R6R7R8R9RR:(R((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytplotxy�s

cCsFtjd�tjd�tj|jd|jd�tj�dS(s) Here we plot y(t) against z(t).
        sy(t)sz(t)RRN(R6R7R8R9RR:(R((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytplotyz�s

cCsFtjd�tjd�tj|jd|jd�tj�dS(s) This method plots x(t) by z(t).
        sx(t)sz(t)RRN(R6R7R8R9RR:(R((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytplotzx�s

cCsHtjdd�}tj|jd|jd|jd�tj�dS(s� And finally we have the plotting method to give a 3D representation of all three,
            x(t), y(t), and z(t), against eachother.
        t
projectiont3dRRRN(R6tgcaR9RR:(Rttd((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pytplot3d�s(N(t__name__t
__module__t__doc__RRRR!R%RRR2R)R5R;R<R=R>R?R@RE(((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pyRs
		
		!							(tnumpyRtpandasRtmatplotlib.pyplottpyplotR6tmpl_toolkits.mplot3dRtobjectR(((sZ/projects/20b744b4-dd84-4163-8ecd-9285794a6d7d/Midterm/CS510-Fall2015-Midterm/attractor.pyt<module>s