Code covered by the BSD License  

Highlights from
Gravity

image thumbnail
from Gravity by link2358 noname
Computes position of several bodies under the action of gravitatory forces.

findv(inibod,endbod,inid,dt2,init,inidir,tol,tol2,miter)
function [iniv,iiter,error] = findv(inibod,endbod,inid,dt2,init,inidir,tol,tol2,miter)

close all
%once gravity 2 has been run, and given the entry parameters (starting body, time and direction,
%target body) , it computes the necessary velocity to impact the target
%body

%inibod: starting body
%endbod: target body
%inid: distance from the initial body from where the rocket is launched
%dt2: time step size
%init: launching time
%inidir: launching direction [cos(alpha; sin(alpha)]
%tol and tol2: tolerances for numerical iterative processes
%miter: maximum number of iterations iterative processes

global g mass;

ve = sqrt(2*g*mass(inibod)/inid); %initial guess, escape velocity
x0 = ve;
x_1 = 1.01*x0;

%Secant method
[mdist] = mindist(inibod,inid,dt2,init,inidir,x0,tol2,miter,Inf);
fx0 = mdist(endbod);
[mdist] = mindist(inibod,inid,dt2,init,inidir,x_1,tol2,miter,Inf);
fx_1 = mdist(endbod);
error = 1;
iiter = 0;

while error > tol && iiter < miter
    iiter = iiter+1;
    fprima = (fx0-fx_1)/(x0-x_1);
    flag = -1;
    while flag ~= 0 && iiter < miter
        x1 = x0 - fx0/fprima;
        if x1 < ve; x1 = (x0+ve)/2;end
        [mdist,flag] = mindist(inibod,inid,dt2,init,inidir,x1,tol2,miter,Inf);
        if flag == endbod
            break;
        elseif flag ~= 0
            fprima = fprima*2;
            iiter = iiter+1;
        end
    end
    fx1 = mdist(endbod);
    error = abs(fx1);
    if flag == endbod
        break;
    end
    x_1 = x0;
    fx_1 = fx0;
    x0 = x1;
    fx0 = fx1;
end
iniv = x1;
%if iniv < ve; iniv = Inf; end
[mdist] = mindist(inibod,inid,dt2,init,inidir,x1,tol2,miter,1);

Contact us at files@mathworks.com