Servo motor routing according to Matlab x, y, z coordinates - matlab

I work to about missile routing. I calculated throuhout the flight x,y,z coordinates of missile . I have data set about missile x,y,z coordinates. My goal is to move the servo motor according to x, y, z coordinates.
My input is 3-dimensional(x,y,z). I want to simulate in two dimensions. And for this I use vectoral calculation. The servo motor can take values between 0-1. But the result larger than 1 . When the results are reduced at the same rate, result is smaller than 0. But I get still the error
Undefined function 'writePosition' for input arguments of type 'matlab.graphics.chart.primitive.Surface'.
I will be grateful if you could help me.
My data example:
missile_x = 0.015
missile_y = 0.054
missile_z = 0.254
missile_flight = 0.00018794
My flight rotation code:
missile_x = vpa(Xval{id}(k)/10,5)
missile_y = vpa(Yval{id}(k)/10,5)
missile_z = vpa(Zval{id}(k)/10,5)
missile_flight = vpa(0.00555556*(missile_x^2+missile_y^2+missile_z^2)^1/2,5)
writePosition(s, missile_flight);
current_pos = readPosition(s);
current_pos = current_pos*missile_flight;
fprintf('Current motor position is %d degrees\n', current_pos);
pause(2);
Missile X,Y,Z calculation code:
dt = 0.005; %time step
g = 9.81; %gravity
ro = 1.2; %air density
A = pi*(0.2)^2; % reference area
Vmag = 0; % missile velocity vectoral value [m/sn]
t = 0;
T(1) = t;
U(1) = 0; %the missile is initially at rest at t = 0; So the velocity is 0
V(1) = 0;
W(1) = 0;
X(1) = X0;
Y(1) = Y0;
Z(1) = Z0;
n = 1;
h = interp2(x_terrain, y_terrain, h_terrain,X(1), Y(1));
while (Z(n) >= h)
[Thx, Thy, Thz] = thrust(t, Thmag0, theta, phi, Tburn, U(n), V(n), W(n));
Vmag = (U(n)^2 + V(n)^2 + W(n)^2)^(1/2);
Thmag = (Thx^2 + Thy^2 + Thz^2)^(1/2);
m = mass(t, m0, mf, Tburn);
[rho,sound_speed] = atmosphere(Z(n));
Cd = drag_coeff(Vmag,sound_speed);
U(n+1) = U(n) + (Thx/m - (Cd*rho*A/(2*m))*(U(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2)))*dt;
V(n+1) = V(n) + (Thy/m - (Cd*rho*A/(2*m)*(V(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2))))*dt;
W(n+1) = W(n) + (Thz/m - (Cd*rho*A/(2*m))*(W(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2)) - g)*dt;
X(n+1) = X(n) + U(n+1)*dt;
Y(n+1) = Y(n) + V(n+1)*dt;
Z(n+1) = Z(n) + W(n+1)*dt;
h = interp2(x_terrain, y_terrain, h_terrain, ...
X(end), Y(end));
t = t + dt;
T(n+1) = t;
n = n+1 ;
end
Tval = T;
Xval = X;
Yval = Y;
Zval = Z;

Related

Steepest Descent using Armijo rule

I want to determine the Steepest descent of the Rosenbruck function using Armijo steplength where x = [-1.2, 1]' (the initial column vector).
The problem is, that the code has been running for a long time. I think there will be an infinite loop created here. But I could not understand where the problem was.
Could anyone help me?
n=input('enter the number of variables n ');
% Armijo stepsize rule parameters
x = [-1.2 1]';
s = 10;
m = 0;
sigma = .1;
beta = .5;
obj=func(x);
g=grad(x);
k_max = 10^5;
k=0; % k = # iterations
nf=1; % nf = # function eval.
x_new = zeros([],1) ; % empty vector which can be filled if length is not known ;
[X,Y]=meshgrid(-2:0.5:2);
fx = 100*(X.^2 - Y).^2 + (X-1).^2;
contour(X, Y, fx, 20)
while (norm(g)>10^(-3)) && (k<k_max)
d = -g./abs(g); % steepest descent direction
s = 1;
newobj = func(x + beta.^m*s*d);
m = m+1;
if obj > newobj - (sigma*beta.^m*s*g'*d)
t = beta^m *s;
x = x + t*d;
m_new = m;
newobj = func(x + t*d);
nf = nf+1;
else
m = m+1;
end
obj=newobj;
g=grad(x);
k = k + 1;
x_new = [x_new, x];
end
% Output x and k
x_new, k, nf
fprintf('Optimal Solution x = [%f, %f]\n', x(1), x(2))
plot(x_new)
function y = func(x)
y = 100*(x(1)^2 - x(2))^2 + (x(1)-1)^2;
end
function y = grad(x)
y(1) = 100*(2*(x(1)^2-x(2))*2*x(1)) + 2*(x(1)-1);
end

Is there a possibility to have a quiver plot with vectors of same length?

I am using a quiver plot in MATLAB to simulate a velocity field. Now I would like the vectors produced by the quiver plot to be all the same length, so that they just indicate the vectors direction. The value of the velocity in each point should be illustrated by different colors then.
Is there a possibility to have quiver plotting vectors of same length?
That's my current code:
%defining parameters:
age = 900;
vis= 15;
turbulences = zeros(9,3);
a = 0.01;
spacing = 1000;
[x,y] = meshgrid(-100000:spacing:100000);%, 0:spacing:10000);
u = a;
v = 0;
n = 0;
for i = 1:4
turbulences(i,1) = -80000 + n;
turbulences(i,2) = 15000;
n = 15000 * i;
end
n = 0;
for i = 5:9
turbulences(i,1) = -15000 + n*5000;
turbulences(i,2) = 4000;
n = n+1;
end
for i = 1:4
turbulences(i,3) = -1000;
end
for i = 5:9
turbulences(i,3) = 800;
end
%compute velocities in x and y direction
for k = 1:9
xc = turbulences(k,1);
yc = turbulences(k,2);
r1 = ((x-xc).^2 + (y-yc).^2);
r2 = ((x-xc).^2 + (y+yc).^2);
u = u + turbulences(k,3)/2*pi * (((y-yc)./r1).*(1-exp(-(r1./(4*vis*age)))) - ((y+yc)./r2).*(1-exp(-(r2./(4*vis*age)))));
v = v - turbulences(k,3)/2*pi* (((x-xc)./r1).*(1-exp(-(r1./(4*vis*age)))) - ((x-xc)./r2).*(1-exp(-(r2./(4*vis*age)))));
end
quiver(x,y,u,v);
grid on;
Thank you for your help!
One way to do this would be to normalize each component of your vectors to +- 1 just to keep their direction.
un = u./abs(u); % normalized u
vn = v./abs(v); % normalized v
quiver(x, y, un, vn)

Finite Element assembly

I'm having serious problems in a simple example of fem assembly.
I just want to assemble the Mass matrix without any coefficient. The geometry is simple:
conn=[1, 2, 3];
p = [0 0; 1 0; 0 1];
I made it like this so that the physical element will be equal to the reference one.
my basis functions:
phi_1 = #(eta) 1 - eta(1) - eta(2);
phi_2 = #(eta) eta(1);
phi_3 = #(eta) eta(2);
phi = {phi_1, phi_2, phi_3};
Jacobian matrix:
J = #(x,y) [x(2) - x(1), x(3) - x(1);
y(2) - y(1), y(3) - y(1)];
The rest of the code:
M = zeros(np,np);
for K = 1:size(conn,1)
l2g = conn(K,:); %local to global mapping
x = p(l2g,1); %node x-coordinate
y = p(l2g,2); %node y-coordinate
jac = J(x,y);
loc_M = localM(jac, phi);
M(l2g, l2g) = M(l2g, l2g) + loc_M; %add element masses to M
end
localM:
function loc_M = localM(J,phi)
d_J = det(J);
loc_M = zeros(3,3);
for i = 1:3
for j = 1:3
loc_M(i,j) = d_J * quadrature(phi{i}, phi{j});
end
end
end
quadrature:
function value = quadrature(phi_i, phi_j)
p = [1/3, 1/3;
0.6, 0.2;
0.2, 0.6;
0.2, 0.2];
w = [-27/96, 25/96, 25/96, 25/96];
res = 0;
for i = 1:size(p,1)
res = res + phi_i(p(i,:)) * phi_j(p(i,:)) * w(i);
end
value = res;
end
For the simple entry (1,1) I obtain 0.833, while computing the integral by hand or on wolfram alpha I get 0.166 (2 times the result of the quadrature).
I tried with different points and weights for quadrature, but really I do not know what I am doing wrong.

Matlab 3d projectile secant method with vector

How can I use the secant method in order to calculate the angle b(between the positive x-axis and negative y-axis) I have to throw a ball for it to land on the x-axis, with these conditions:
the ball is thrown from origo, at a height z=1.4 m, with the and angle a (for the horizontal plane) 30◦, with a speed of 25 m / s. The air resistance coefficient is c = 0.070 and the wind force is 7 m / s at the ground which increases with the height according to: a(z) = 7 + 0.35z.
The motion of the ball is described with the following:
x''=−qx', y''=−q(y'−a(z)), z''=−9.81−qz', q=c*sqrt(x'^2+(y'−a(z))^2+z'^2)
I did a variable substitution (u) and then used RK4 to calculate the ball's motion, however I can't figure out how to use the secant method to find the angle b. The problem is that when I plot with these start and guessing values is that the ball doesn't land on the x-axis:
clear all, close all, clc
a = pi/3; %start angle
c = 0.07; % air resistance coeff.
v0 = 25; %start velocity, 25 m/s
t = 0; %start time
h = 0.1; % 0.1 second step
b = 0
x0 = 0; xPrim = v0*sin(a)*cosd(b);
y0 = 0; yPrim = v0*sin(a)*sind(b);
z0 = 1.4; zPrim = v0*cos(a);
u = [x0 xPrim y0 yPrim z0 zPrim]';
uVek = u';
% Secant method
b0 = 270; % Start guess nr 1
b1 = 360; % Start guess nr 2
f0 = funk (b0);
db = 1;
while abs(db) > 0.5e-8
f1 = funk (b1);
db = f1 * (b1 - b0) / (f1 - f0);
b0 = b1; % Updates b0
f0 = f1; % Updates f0
b1 = b1 - db % new b
end
while u(5) >= 0 && u(3)<=0
f1 = FRK4(t,u);
f2 = FRK4(t+h/2,u+(h/2)*f1);
f3 = FRK4(t+h/2,u+(h/2)*f2);
f4 = FRK4(t+h,u+h*f3);
f = (f1 + 2*f2 + 2*f3 + f4)/6;
u = u + h*f;
uVek = [uVek; u'];
t = t+h;
end
x = uVek(:,1)'; y = uVek(:,3)'; z = uVek(:,5)';
plot3(x,y,z)
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on
where FRK4 is a function:
function uPrim = FRK4(t,u)
c = 0.07;
az = 7+0.35*(u(5));
q = c*sqrt(u(2)^2 +(u(4)- az)^2 + u(6)^2);
uPrim = [u(2) -q*u(2) u(4) -q*(u(4) - az) u(6) -9.81-q*u(6)]';
end
where funk is a function:
function f = funk(b)
a = pi/3
v0 = 25
x0 = 0; xPrim = v0*sin(a)*cosd(b);
y0 = 0; yPrim = v0*sin(a)*sind(b);
z0 = 1.4; zPrim = v0*cos(a);
f = [x0 xPrim y0 yPrim z0 zPrim]';
end

MATLAB simulation runs to infinity, but not when run step by step in debugger

My code simulates the shock loading of a rocket upon parachute deployment. When the script is run as a whole the value of the shock goes to infinity. However, when the script is run step by step in the debugger mode it works as intended. I am unsure of what is happening or how to fix it. My code is written as follows:
clear all; close all; clc; tic
h_init = 100; %initial altitude, ft
%INPUTS
E = .38e6; %elastic modulus of nylon, psi
A = (1/16)*(1); %cross sectional area of cord, in^2
CD = 2.20; %parachute coefficient of drag
D_p = 144; %parachute diameter, in
W_p = 2; %weight of parachute, lbm
W_r = 105; %dry weight of rocket, lbm
L = 17*3*12; %length of shock cord, in
v_init = -350; %initial upwards velocity of rocket/parachute, ft/s
dt = 0.00001; %simulation time step, s
t_sim = 4; %total simulation time, s
g = 32.2; %gravitational acceleration, ft/s^2
%CALCULATIONS
k = (3900)./L; %spring constant, lbf/in
S_p = pi*D_p.^2/4/144; %parachute cross sectional area, ft^2
m_p = W_p/g; %mass of parachute, slugs
m_r = W_r./g; %mass of rocket, slugs
N = ceil(t_sim/dt);
N_L = length(W_r);
time = dt:dt:t_sim + dt;
Y1 = zeros(N_L,N);
Y2 = zeros(N_L,N);
V1 = zeros(N_L,N);
V2 = zeros(N_L,N);
FS = zeros(N_L,N);
Dr = zeros(N_L,N);
Shock = zeros(N_L,1);
Exten = zeros(N_L,1);
VDiff = zeros(N_L,1);
DrMax = zeros(N_L,1);
TDrop = zeros(N_L,1);
for j = 1:N_L
v1 = v_init; %initial velocity of parachute after deployment, ft/s
y1 = 0; %initial parachute position relative to deployment altitude, ft
v2 = v_init; %initial velocity of rocket after deployment, ft/s
y2 = 0; %initial position of rocket relative to deployment altitude, ft
Y1(j,1) = y1;
Y2(j,1) = y2;
V1(j,1) = v1;
V2(j,1) = v2;
FS(j,1) = 0;
Dr(j,1) = 0;
slack = 1;
for i = 1:N
rho = findDensity(h_init+y1);%air density, slug/ft^3
Q = 144*0.5*rho*v1^2; %dynamic pressure, psi
D = -sign(v1)*(Q/144)*S_p*CD; %drag, lbf
if y1-y2 >= L/12
delta = 12*(y1-y2)-L; %shock cord extension, in
if slack ==1
TDrop(j) = dt*i;
slack = 0;
end
elseif y2-y1 >= L/12
delta = -12*(y1-y2)-L; %shock cord extension, in
else
delta = 0;
end
fs = k*delta; %spring force, lbf
F1 = D - W_p - fs; %net force on parachute, lbf
F2 = fs - W_r(j); %net force on rocket, lbf
a1 = F1/m_p; %acceleration of parachute, ft/s^2
a2 = F2/m_r(j); %acceleration of rocket, ft/s^2
y1 = y1 + v1*dt; %update parachute position, ft
y2 = y2 + v2*dt; %update rocket position, ft
v1 = v1 + a1*dt; %update parachute velocity, ft/s
v2 = v2 + a2*dt; %update rocket velocity, ft/s
Y1(j,i+1) = y1;
Y2(j,i+1) = y2;
V1(j,i+1) = v1;
V2(j,i+1) = v2;
FS(j,i+1) = fs;
Dr(j,i+1) = D;
end
Shock(j) = max(FS(j,:));
Exten(j) = 12*max(Y1(j,:)-Y2(j,:)-L/12);
VDiff(j) = max(abs(V1(j,:)-V2(j,:)));
DrMax(j) = max(Dr(j,:));
end
H1 = h_init+Y1;
H2 = h_init+Y2;
fprintf('Max Shock Load: %.2f lbf', Shock)
toc
function density = findDensity(h)
temp = findTemp(h);
pressure = findPressure(h);
density = pressure / (1718 * (temp + 459.7));
end
function pressure = findPressure(h)
temp = findTemp(h);
if(h > 82345)
pressure = 51.97 * (((temp + 459.7) / 389.98) ^ -11.388);
elseif(h > 36152)
pressure = 473.1 * exp(1.73 - (0.000048 * h));
else
pressure = 2116 * (((temp + 459.7) / 518.6) ^ 5.256);
end
end
function temp = findTemp(h)
if(h > 82345)
temp = (0.00164 * h) - 205.5;
elseif(h > 36152)
temp = -70;
else
temp = 59 - (0.00356 * h);
end
end