Answer To: E7 Fall 2019, UC Berkeley Homework Assignment 12 Ordinary Differential Equations The purpose of this...
Abr Writing answered on Dec 11 2021
earth.m
function [udot]= earth(t,u)
m_earth = 5.9742e24; % [kg]
m_sun = 1.98892e30; % [kg]
m_sat = 11110; % [kg]
G = 6.67300e-11; %[(m)^3(kg)^-1(s)^-2];
d_earth_sun = sqrt((u( 7,1)-u(1,1))^2+(u( 8,1)-u(2,1))^2+(u( 9,1)-u(3,1))^2);
d_earth_sat = sqrt((u(13,1)-u(1,1))^2+(u(14,1)-u(2,1))^2+(u(15,1)-u(3,1))^2);
d_sun_sat = sqrt((u(13,1)-u(7,1))^2+(u(14,1)-u(8,1))^2+(u(15,1)-u(9,1))^2);
% Earth motion
udot( 1,1) = u(4,1);
udot( 2,1) = u(5,1);
udot( 3,1) = u(6,1);
udot( 4,1) = m_sun*G*(u(7,1)-u(1,1))/d_earth_sun^3 + m_sat*G*(u(13,1)-u(1,1))/d_earth_sat^3;
udot( 5,1) = m_sun*G*(u(8,1)-u(2,1))/d_earth_sun^3 + m_sat*G*(u(14,1)-u(2,1))/d_earth_sat^3;
udot( 6,1) = m_sun*G*(u(9,1)-u(3,1))/d_earth_sun^3 + m_sat*G*(u(15,1)-u(3,1))/d_earth_sat^3;
% Sun Motion
udot( 7,1) = u(10,1);
udot( 8,1) = u(11,1);
udot( 9,1) = u(12,1);
udot(10,1) = m_earth*G*(u(1,1)-u(7,1))/d_earth_sun^3 + m_sat*G*(u(13,1)-u(7,1))/d_sun_sat^3;
udot(11,1) = m_earth*G*(u(2,1)-u(8,1))/d_earth_sun^3 + m_sat*G*(u(14,1)-u(8,1))/d_sun_sat^3;
udot(12,1) = m_earth*G*(u(3,1)-u(9,1))/d_earth_sun^3 + m_sat*G*(u(15,1)-u(9,1))/d_sun_sat^3;
% Satellite Motion
udot(13,1) = u(16,1);
udot(14,1) = u(17,1);
udot(15,1) = u(18,1);
udot(16,1) = m_earth*G*(u(1,1)-u(13,1))/d_earth_sat^3 + m_sun*G*(u(7,1)-u(13,1))/d_sun_sat^3;
udot(17,1) = m_earth*G*(u(2,1)-u(14,1))/d_earth_sat^3 + m_sun*G*(u(8,1)-u(14,1))/d_sun_sat^3;
udot(18,1) = m_earth*G*(u(3,1)-u(15,1))/d_earth_sat^3 + m_sun*G*(u(9,1)-u(15,1))/d_sun_sat^3;
end
LorenzODE.m
function dotY = LorenzODE(Y, sigma, beta, rho)
dotY = [
sigma*(Y(2)-Y(1))
Y(1).*(rho-Y(3))-Y(2)
Y(1).*Y(2)-beta*Y(3)
];
end
DoublePendulum.m
function I=DoublePendulum(I,t,C)
l1=C(1);
l2=C(2);
m1=C(3);
m2=C(4);
g=C(5);
I_prime=zeros(4,1); %#ok<*NASGU>
a = (m1+m2)*l1 ;
b = m2*l2*cos(I(1)-I(3)) ;
c = m2*l1*cos(I(1)-I(3)) ;
d = m2*l2 ;
e = -m2*l2*I(4)* I(4)*sin(I(1)-I(3))-g*(m1+m2)*sin(I(1)) ;
f = m2*l1*I(2)*I(2)*sin(I(1)-I(3))-m2*g*sin(I(3)) ;
Iprime(1) = I(2);
Iprime(3)= I(4) ;
Iprime(2)= (e*d-b*f)/(a*d-c*b) ;
Iprime(4)= (a*f-c*e)/(a*d-c*b) ;
Iprime=Iprime';
end
RK4.m
function y = RK4(Yinitial, x, h, OrbitFunc)
y = zeros(1,length(x));
y(1) = Yinitial; % initial condition
F_xy = @(t,r) 3.*exp(-t)-0.4*r; % change the function as you desire
for i=1:(length(x)-1) % calculation loop
k_1 = F_xy(x(i),y(i));
k_2 = F_xy(x(i)+0.5*h,y(i)+0.5*h*k_1);
k_3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2));
k_4 = F_xy((x(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h; % main equation
end
end
Euler.m
function y = Euler(Yinitial, t, h, OrbitFunc)
N=10; % number of steps
y(1)=Yinitial;
for n=1:N
y(n+1)= y(n)+h*(-6*y(n));
x(n+1)=n*h;
end
end
HW12_ODE.m
%% Problem 1
sigma = 10;
beta = 8/3;
rho = 14;
figure(1);
Lorenz = @(t,I) LorenzODE(I, sigma, beta, rho);
[~,I] = ode45(Lorenz, [0 20], [0; 1; 1]);
plot3(I(:,1), I(:,2), I(:,3));
hold on ;
[~,I] = ode45(Lorenz, [0 20], [1; 1; 1]);
plot3(I(:,1), I(:,2), I(:,3));
[~,I] = ode45(Lorenz, [0 20], [1; -1; 1]);
plot3(I(:,1), I(:,2), I(:,3));
[~,I] = ode45(Lorenz, [0 20], [1; -1; -1]);
plot3(I(:,1), I(:,2), I(:,3));
[~,I] = ode45(Lorenz, [0 20], [1; -1; -10]);
plot3(I(:,1), I(:,2), I(:,3));
print('Lorenz','-dpng')
hold off;
%% Problem 2
l1 = 1;
l2 = 1;
m1 = 1;
m2 = 1;
g = 9.81;
C = [l1 l2 m1 m2 g];
tspan = 0:1/30:20;
dpend = @(t, I) DoublePendulum(I, t, C);
[t,I]=ode45(dpend, tspan,[ 0.5; 0; 0.5; 0]);
x1=l1*sin(I(:,1));
I1=-l1*cos(I(:,1));
x2=l1*sin(I(:,1))+l2*sin(I(:,3));
I2=-l1*cos(I(:,1))-l2*cos(I(:,3));
Ncount=0;
fram=0;
v = VideoWriter('DoublePendulum.avi');
open(v);
clf;
for i=1:length(I)
Ncount=Ncount+1;
fram=fram+1;
plot(0, 0,'.','markersize',20);
hold on
plot(x1(i),I1(i),'.','markersize',20);
plot(x2(i),I2(i),'.','markersize',20);
hold off
line([0 x1(i)], [0 I1(i)],'Linewidth',2);
axis([-(l1+l2) l1+l2 -(l1+l2) l1+l2]);
line([x1(i) x2(i)], [I1(i) I2(i)],'linewidth',2);
h=gca;
get(h,'fontSize')
set(h,'fontSize',12)
xlabel('X','fontSize',12);
ylabel('I','fontSize',12);
title('Double Pendulum Motion','fontsize',14)
fh = figure(3);
set(fh, 'color', 'white');
F=getframe(gca);
close;
clf(figure);
writeVideo(v,F);
end
% movie(F,fram,20)
close(v);
%% Problem 3
tspan =...