Question

In: Computer Science

Write a Matlab code that simulates three-body problem tridimensional with any given masses, initial positions, and...

Write a Matlab code that simulates three-body problem tridimensional with any given masses, initial positions, and velocities. Plotting the trajectory of all the masses.

Solutions

Expert Solution

#Mass of the Third Star
m3=1.0 #Third Star
#Position of the Third Star
r3=[0,1,0] #m
r3=sci.array(r3,dtype="float64")
#Velocity of the Third Star
v3=[0,-0.01,0]
v3=sci.array(v3,dtype="float64")
#Update COM formula
r_com=(m1*r1+m2*r2+m3*r3)/(m1+m2+m3)
#Update velocity of COM formula
v_com=(m1*v1+m2*v2+m3*v3)/(m1+m2+m3)
def ThreeBodyEquations(w,t,G,m1,m2,m3):
    r1=w[:3]
    r2=w[3:6]
    r3=w[6:9]
    v1=w[9:12]
    v2=w[12:15]
    v3=w[15:18]
    r12=sci.linalg.norm(r2-r1)
    r13=sci.linalg.norm(r3-r1)
    r23=sci.linalg.norm(r3-r2)
    
    dv1bydt=K1*m2*(r2-r1)/r12**3+K1*m3*(r3-r1)/r13**3
    dv2bydt=K1*m1*(r1-r2)/r12**3+K1*m3*(r3-r2)/r23**3
    dv3bydt=K1*m1*(r1-r3)/r13**3+K1*m2*(r2-r3)/r23**3
    dr1bydt=K2*v1
    dr2bydt=K2*v2
    dr3bydt=K2*v3
    r12_derivs=sci.concatenate((dr1bydt,dr2bydt))
    r_derivs=sci.concatenate((r12_derivs,dr3bydt))
    v12_derivs=sci.concatenate((dv1bydt,dv2bydt))
    v_derivs=sci.concatenate((v12_derivs,dv3bydt))
    derivs=sci.concatenate((r_derivs,v_derivs))
    return derivs

#Package initial parameters
init_params=sci.array([r1,r2,r3,v1,v2,v3]) #Initial parameters
init_params=init_params.flatten() #Flatten to make 1D array
time_span=sci.linspace(0,20,500) #20 orbital periods and 500 points
#Run the ODE solver
import scipy.integrate
three_body_sol=sci.integrate.odeint(ThreeBodyEquations,init_params,time_span,args=(G,m1,m2,m3))
r1_sol=three_body_sol[:,:3]
r2_sol=three_body_sol[:,3:6]
r3_sol=three_body_sol[:,6:9]

Related Solutions

Write a Matlab code that simulates three- body problem with any given masses, initial positions and...
Write a Matlab code that simulates three- body problem with any given masses, initial positions and velocities. Also give a set of data that generates an interesting orbit.
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
write a matlab code to find the following: initial position, initial velocity, and acceleration using the...
write a matlab code to find the following: initial position, initial velocity, and acceleration using the algorithm and information below time(seconds). height(m) velocity(m/s) 0. 0.2. 2.95 algorithm: 1. Enter data in to arrays. 2. Fit the height data to a 2nd order polynomial. 3. Evaluate the polynomial at enough points to get a smooth curve. 4. Find the velocity model by taking derivative of the height polynomial. 5. Evaluate the velocity polynomial at enough times to get a smooth curve
Write a MATLAB script file to numerically solve any first order initial value problem using Rulers...
Write a MATLAB script file to numerically solve any first order initial value problem using Rulers method. Once code is working use it to solve the mixing tank problem below. Use a step size of 1 minute, and simulate the solution until the tank contains no more salt. Plot both the Euler approximation and the exact solution on the same set of axes. A tank contains 100 gallons of fresh water. At t=0 minutes, a solution containing 1 lb/gal of...
Problem 4 ..... you can use Matlab Using the same initial code fragment as in Problem...
Problem 4 ..... you can use Matlab Using the same initial code fragment as in Problem 1, add code that calculates and plays y (n)=h(n)?x (n) where h(n) is the impulse response of an IIR bandstop filter with band edge frequencies 750 Hz and 850 Hz and based on a 4th order Butterworth prototype. Name your program p3.sce the below is the Problem 1 initail code .. you can use it Matlab The following cilab code generates a 10-second “chirp”...
using matlab Write a script that simulates a card game that works as follows: A dealer...
using matlab Write a script that simulates a card game that works as follows: A dealer places 5 cards face down on the table and flips the first card. The player goes down the line, one at a time, and guesses if the next card is higher or lower than the card displayed, and then the next card is revealed. In the end, the player is awarded a point for each correct guess. In terms of coding, your script should...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided dice that number of times. The program should keep track of how many times each possible sum comes up using a list. The list's first element should contain how many times a total of 2 was rolled, the second should contain how many times a total of 3 was rolled, and so on all the way through to 12. When all rolls are complete,...
Write and test MatLAB code implementing the mathematical models of the boundary value problem to evaluate...
Write and test MatLAB code implementing the mathematical models of the boundary value problem to evaluate the elastic deflection of the beam based on Euler-Bernoulli and Timoshenko theories with finite difference discretisation (for numerical integration and differentiation). The results must be plotted on a graph with labelled local maxima and minima
determine whether the given function is even, odd, or neither. Please write a code in MatLab...
determine whether the given function is even, odd, or neither. Please write a code in MatLab to solve this problem below: 1.f(x) = sin 3x please only use Matlab to solve this problem
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a...
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 = 3kHz and f2 = 5kHz and then sample it with fs = 10kHz. Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not? If there is aliasing specify which component is casing the aliasing
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT